Application started by user vs. from a script

I am developing an AppleScript Studio application that should be able to operate in two different ways:

  1. If the application is opened by the user, it should show its main window and stay open for user action.
  2. If a handler of this application is called from another script (and the application is not currently open), it should not show its main window, but just do its stuff and quit.

For the case number 2, the external script I am talking about could look something like this:

tell application "test"
	launch
	activate
	tell button "hiddenbutton" to perform action
end tell

The problem here is how can I figure out whether the application was launched by this external script (and shouldn’t display the main window), or whether it was launched by the user (and should display the window)?

The closest I got so far, was to put the show window “mainwindow” command inside an open untitled or should open untitled handler. This handler gets called when the application is launched by the user, but not when it is launched by a tell application “test” to launch.

The only problem here is that if the application is launched by the script, and while it is doing its stuff, the user clicks on the application icon in the Dock. This will also call the (should) open untitled handler, which will result in the main window being shown.

I guess a trick solution would be to set the value of some global variable when I am calling the application from the external script. The should open untitled handler could then first check the value of this variable, and only allow the showing of the main window if this variable wasn’t set.

However, I think that there is a better way (the right way) to solve this problem, but I’m just not getting it. Anyone can think of the correct way to do it?

Hi,

What I did was use the defaults. Here’s an example:

property userRun : missing value

on awake from nib theObject
	tell user defaults
		make new default entry at end of default entries with properties {name:"userRun", contents:"true"}
		tell default entry "userRun"
			log (contents as string)
		end tell
	end tell
end awake from nib

on launched theObject
	set open_window to (contents of default entry "userRun" of user defaults) as Unicode text
	set userRun to (getPlainText(open_window) as boolean)
	tell user defaults
		-- reset the default entry for next run
		tell default entry "userRun"
			set contents to "true"
			log (contents as string)
		end tell
	end tell
end launched

on activated theObject
	if userRun then
		show window "main"
	end if
end activated

on getPlainText(fromUnicodeString)
	set styledText to fromUnicodeString as string
	set styledRecord to styledText as record
	return «class ktxt» of styledRecord
end getPlainText

Then, if you run the app from script, you write to the defaults. I accidently named the app "LunchTester3". :) So the preference file came out as "LunchTester3.plist". Script example:

set pref_path to (path to preferences folder from user domain) as string
set app_pref to quoted form of POSIX path of (pref_path & "LunchTester3")
do shell script "defaults write " & app_pref & " userRun 'false'"
do shell script "defaults read " & app_pref & " userRun"
tell application "LunchTester3"
	activate
	-- do whatever
end tell

I just started learning about defaults and unix so this probably can be cleaned up.

gl,