I am developing an AppleScript Studio application that should be able to operate in two different ways:
- If the application is opened by the user, it should show its main window and stay open for user action.
- 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?