Passing an argument to a 'stay-open' AppleScript app on launch

I apologise if this has been beaten to death already, but I did search the forums for this first. I am very new to AppleScript. I am trying to pass an argument to a stay open AppleScript app, but I am not sure how it can be done. My stay open app is a simple “on idle” loop which keeps running until a certain condition is met (at which point it quits). However, I need to pass it a single argument. Previously, I have used “on run argv” to pass arguments using osascript, but it seems this will not work in this case, because the “on idle” loop cannot be placed within the “on run argv” (please correct if I am wrong).

As an alternative to “on run argv”, I tried to set an environment variable in the terminal, and then use “system attribute” to obtain the value as shown below:

myVar=varValue
export myVar

After exporting the new environment variable, I can check it by typing in the terminal:

set

This confirms that the variable has been set, as I can see “myVar=varValue” in the list of environment variables. However, when I try to fetch this variable using

return system attribute "myVar"

it returns “”, indicating a null value.

I am open to any suggestions regarding

a) alternatives to the methods described above or
b) corrections to my current approach(es)

Many thanks in advance for your help.
Regards
Lars Henning
lars@focusrite.com

Model: Intel Mac Mini
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Lars,

system attribute returns the value of a gestalt selector like

system attribute "sysv" -- system version e.g. 4169 for System 10.4.9

nothing else.
Can you describe more detailed, which argument you want to pass

Stefan

Thanks for your reply. This stay open app is part of a Mac kiosk. Basically, I need a way to detect when the user has quit an application. There are 4 possible applications on the kiosk, so I want to pass the current application to the stay open app. I was planning to launch this stay open app when the user chooses 1 of the 4 applications. I want to reset the kiosk if they quit the current application, so the stay open app is basically a watchdog waiting for the current app to quit (but I think it needs to know which app wants to be quit). Here is what I was trying:


on idle
	tell application "System Events" to set AppQuit to not (application process appName exists)
	if AppQuit then
		tell application "Finder"
			display dialog "app quit"
		end tell
	end if
	return 2
end idle

This works, but I can’t figure out a means of passing “appName”. Also, it seems like overkill to poll every 2 seconds for the existence of a process. Please tell me there is a better way to detect a quit application (where the application is dynamic). I am open to suggestions.

Many Thanks,
Lars

this is quite difficult, the standard Scripting Additions have a command path to frontmost application,
but when you run your (application) script, the script itself is the frontmost application.
Unfortunately I’ve no idea at the moment, sorry

Maybe I don’t understand, but it seems easy to get the name of the applicaton that quits in your case. You don’t need to know the name of the current application. If you have 4 apps that run on he kiosk then you set up a list of them and check the list…

set appList to {"Safari", "iTunes", "Mail", "iCal"}
repeat with anApp in appList
	tell application "System Events"
		if not (exists process anApp) then
			display dialog "The application " & anApp & " has quit"
			set the_quit_app to anApp
		end if
	end tell
end repeat

My first script assumed that all 4 applications were running. If none of them were running or only one of them were running when you launched your stay-open script then something like this could be used.

If any of the 4 applications could launched or quit at any time then just make more “missing value” variables and adjust the if/then statement in the on idle handler to account for them. Basically, as long as you have a “known” set of applications that you want to watch then watching them isn’t hard.

property running_app : missing value
property appList : {"Safari", "iTunes", "Mail", "iCal"}

on run
	-- when you first launch this stay-open application it will check if one of the apps is running
	repeat with anApp in appList
		tell application "System Events"
			if (exists process anApp) then
				set running_app to anApp
				exit repeat
			end if
		end tell
	end repeat
end run

on idle
	if running_app is not missing value then
		-- if one of the known apps is running then we check for when it quits
		tell application "System Events"
			if not (exists process running_app) then
				set frontApp to displayed name of (info for (path to frontmost application))
				tell application frontApp to display dialog "The application " & running_app & " has quit"
				set running_app to missing value
			end if
		end tell
	else
		-- if none of the apps are running then we check for one to start running
		repeat with anApp in appList
			tell application "System Events"
				if (exists process anApp) then
					set running_app to anApp
					exit repeat
				end if
			end tell
		end repeat
	end if
	return 2
end idle

Here, this will watch all 4 apps and tell you when one is quit. Just change the list of apps to the apps you want to watch.

property appList : {"Safari", "iTunes", "Mail", "iCal"}
property app1_running : false
property app2_running : false
property app3_running : false
property app4_running : false

on run
	tell application "System Events"
		if (exists process (item 1 of appList)) then set app1_running to true
		if (exists process (item 2 of appList)) then set app2_running to true
		if (exists process (item 3 of appList)) then set app3_running to true
		if (exists process (item 4 of appList)) then set app4_running to true
	end tell
end run

on idle
	if app1_running then
		tell application "System Events"
			if not (exists process (item 1 of appList)) then
				display dialog "The application " & (item 1 of appList) & " has quit"
				set app1_running to false
			end if
		end tell
	end if
	
	if app2_running then
		tell application "System Events"
			if not (exists process (item 2 of appList)) then
				display dialog "The application " & (item 2 of appList) & " has quit"
				set app2_running to false
			end if
		end tell
	end if
	
	if app3_running then
		tell application "System Events"
			if not (exists process (item 3 of appList)) then
				display dialog "The application " & (item 3 of appList) & " has quit"
				set app3_running to false
			end if
		end tell
	end if
	
	if app4_running then
		tell application "System Events"
			if not (exists process (item 4 of appList)) then
				display dialog "The application " & (item 4 of appList) & " has quit"
				set app4_running to false
			end if
		end tell
	end if
	
	tell application "System Events"
		if not app1_running then
			if (exists process (item 1 of appList)) then set app1_running to true
		end if
		if not app2_running then
			if (exists process (item 2 of appList)) then set app2_running to true
		end if
		if not app3_running then
			if (exists process (item 3 of appList)) then set app3_running to true
		end if
		if not app4_running then
			if (exists process (item 4 of appList)) then set app4_running to true
		end if
	end tell
	
	return 2
end idle

Your stay-open script could look like this:

global appName

on run
	-- Initialise appName.
	set appName to ""
end run

on idle
	-- If appName is not "" and application process appName doesn't exist then
	-- display a message that the application's quit and reinitialise appName.
	if (appName is not "") then
		tell application "System Events" to set AppQuit to not (application process appName exists)
		if AppQuit then
			tell application (path to frontmost application as Unicode text)
				display dialog "app quit"
			end tell
			set appName to ""
		end if
	end if
	
	return 2
end idle

If it’s saved as a stay-open applet, say with the name “Test.app”, this will implicitly run it (if it’s not running already) and set it to watch out for “TextEdit”:

tell application "Test" to set its appName to "TextEdit"

Nigel Garvey

Thanks for your help. This is exactly what I need. I am very impressed by the expertise on the MacScripter BBS. I will do my best to contribute!

Cheers
Lars