if...then in appleScript?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

if...then in appleScript?

Post by gyroscope » Mon Oct 26, 2009 11:03 pm

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.

:)

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Tue Oct 27, 2009 12:22 am

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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Tue Oct 27, 2009 12:01 pm

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.

:)

Post Reply