I’ll try to explain better what my situation is, so I can figure out what I’m missing.
I am writing an AppleScript Studio application. The application has a button within its window. I have a “on clicked” handler for that button. When the button is pressed, the “on clicked” handler is called. Within that handler, my application launches another application, then reads the delay value previously set by the user. It then delays for 1 second, checks to see whether it (my application) is frontmost or not, then delays another second, and so on until the total delay value time has elapsed. So, for example, if the delay value was set to 20, it would delay for 1 second then check for foreground 20 times.
When it checks for frontmost status, if it finds that it is indeed frontmost, then it immedialy breaks out of the delay loop and nothing else happens. If it remains not frontmost for the entire delay time, then it takes a screenshot.
The desired effect is that if the user switches back to my application from the launched application, my application should stop the delay loop and go back to business as usual. If the user keeps using the launched application, then my application should take a screenshot.
Thanks to help from Kel and Nigel in the Applescript forum next door, I’m able to make sure that launched application is running. The bit of code I’m seeking help with here will let me make sure that even if it is running, it hasn’t been switched to the background.
Which bring me to where I had this backward. I don’t want to find out if MY application is foreground; I want to find out if the launched app is foreground. I only want a screenshot of /that/ app.
So, now I’m working on trying to turn the return value from “default application of (info for f)” - where ‘f’ is an alias - into something that works with the statement ‘frontmost of application f’.
The script I developed below doesn’t work. At the display dialog phase, if I click on, say, a Script Editor file, it will display “Script Editor”. And, if I replace “the_app” in the “if frontmost of application the_app is false then” statement with literal text “Script Editor”, it works fine. But if I use the variable the_app, it says, “System Events got an error: Can’t make frontmost of application “Script Editor” into type reference.”
So I obviously have the wrong type of data in the end. Although I’ve been programming as a hobbyist and sometimes as a professional since 1983, I’ve only been using Applescript since last Wednesday, so forgive me if the solution to this problem seems blindingly obvious to everyone else here.
Also, note that in my actual applicaiton, the name of the application being run is initially derived through a " set default_app_path to default application of (info for ((currentFolder & “:” & selectedGame) as alias))" statement. It fails the same way in my app as with my script editor script below.
set f to choose file
set default_app_path to default application of (info for f)
set default_app_path_as_text to "" & default_app_path
set AppleScript's text item delimiters to ":"
set path_as_list to every text item of default_app_path_as_text
set the_app_and_extension to item ((count path_as_list) - 1) of path_as_list
set AppleScript's text item delimiters to "."
set the_app_and_extension_list to every text item of the_app_and_extension
set the_app to first item of the_app_and_extension_list
display dialog "" & the_app
delay 5 -- Take time to move Script Editor to foreground or background.
tell application "System Events"
if frontmost of application the_app is false then
display dialog "Not frontmost"
else
display dialog "frontmost"
end if
end tell