----------------
set appNameReportText to ""
tell application "Finder"
activate
set thisAppName to my getFrontmostAppName()
end tell
set appNameReportText to appNameReportText & thisAppName & return
tell application "Mail"
activate
set thisAppName to my getFrontmostAppName()
end tell
set appNameReportText to appNameReportText & thisAppName & return
tell me to activate
tell current application
activate
set thisAppName to my getFrontmostAppName()
set appNameReportText to appNameReportText & thisAppName & return
display dialog appNameReportText
end tell
on getFrontmostAppName()
tell application "System Events"
set frontmostName to item 1 of (get name of processes whose frontmost is true)
end tell
return frontmostName
end getFrontmostAppName
----------------
Yes, there is some redundancy with “activate" at the end, but I’m trying to activate the script app itself before displaying the final dialog.
When saving the above as an application, if I run it in Mavericks I reliably get what is expected. The dialog reports…
Finder
Mail
<name of applet>
The script ends with the applet itself frontmost and the dialog displaying properly.
Running the same thing in Yosemite reports inconsistent results: sometimes “Finder Finder Finder” or the name of the applet repeated three times. And before anyone thinks Mail could be causing the problem (a worthy suspicion) this still happens when addressing other applications.
Adding delays helps *some* in actually bringing other apps frontmost, but still does not work reliably. And regardless of whether there are delays, I alway get the bouncing dock icon at the end and have to click that to bring the applet to the front and see the dialog.
I asked another developer running the latest developer preview to test the above. No luck—he got the same behavior.
After reading some previous messages here, I’ve resorted to activating applications via System Events as a workaround…
----------------
set appNameReportText to ""
set scriptAppName to name of me
my activateApp("Finder")
set thisAppName to my getFrontmostAppName()
set appNameReportText to appNameReportText & thisAppName & return
my activateApp("Mail")
set thisAppName to my getFrontmostAppName()
set appNameReportText to appNameReportText & thisAppName & return
my activateApp(scriptAppName)
set thisAppName to my getFrontmostAppName()
set appNameReportText to appNameReportText & thisAppName & return
tell current application
display dialog appNameReportText
end tell
on activateApp(appName)
tell application "System Events" to tell process appName
set frontmost to true
end tell
end activateApp
on getFrontmostAppName()
tell application "System Events"
set frontmostName to item 1 of (get name of processes whose frontmost is true)
end tell
return frontmostName
end getFrontmostAppName
----------------
So far, the above appears to work reliably. Obviously, abandoning the activate command altogether is not desirable.
I’d appreciate anyone else verifying the same results in script applications under Yosemite. Other approaches for getting around this issue are certainly welcome.
Ray Robertson