Page 1 of 1

if...then in appleScript?

Posted: Mon Oct 26, 2009 11:03 pm
by gyroscope
Hi, I'm back to one of my other projects today. In it, I've a script which includes:

Code: Select all

do fld "A" as appleScript 
and the text in fld A is:

tell application "Safari"
set myURL to URL of document 1
return myURL
end tell

The only prob is that if the button is pressed and fld A script is run without a browser window open, it causes an error.

So my question is: how do I script using Applescript an if then statement :?:

i.e (pseudocode included)
tell application "Safari"
if document 1 is open then
set myURL to URL of document 1
return myURL
else
exit tell application
end if
end tell


And I've a second question: how do I script in the above text-that-will-be-code:
tell application "Safari" or "FireFox" or "Opera" or Netscape Navigator" or "Camino" whichever is open ----(psedocode, of course!) :?:

Any help appreciated, please.

:)

Posted: Tue Oct 27, 2009 12:22 am
by bn
Hi Gyroscope,

Code: Select all

tell application "Safari"
	if (count of windows) > 0 then
		return URL of document 1
	else
		return "no window open"
	end if
end tell

Code: Select all

tell application "Finder"
	set theBrowsers to {"Safari", "firefox-bin"}
	set processNames to the name of the processes
	set allOpenBrowsers to ""
	repeat with i from 1 to the number of items of theBrowsers
		if processNames contains (item i of theBrowsers) then set allOpenBrowsers to (allOpenBrowsers & item i of theBrowsers) & return
	end repeat
	set allOpenBrowsers to characters 1 through -2 of allOpenBrowsers as text -- get rid of last return
	return allOpenBrowsers
end tell
this returns the open browsers, I dont know how opera or navigator show up in the processes, you would have to try that. Put

Code: Select all

tell application "Finder"
	set processNames to the name of the processes
end tell
into the script editor when the browsers are open.
regards
Bernd

Posted: Tue Oct 27, 2009 12:01 pm
by gyroscope
That's excellent Bernd, thank you! I see that AppleScript has the if...then...else structure as well, and of course it would have to, although I thought it might be put a different way. Anyway, I've learned there. I'm off to try the scripts out now.

Thanks again.

:)