I have made a script that I wish to run only if Safari is frontmost. The problem is that when I run the script/app (as an application), the script is frontmost, even if I run the script/app (via the mouse) from Safari.
If I try to make the script/app not be frontmost, then the script/app fails.
This scriplet will make Safari frontmost if it is already running, and quit if it isn’t.
tell application "System Events" to set safariActive to (exists process "Safari")
if safariActive then
tell application "Safari" to activate
display alert "This script can only be run if Safari is open."
error number -128
Otherwise, you can use this to get the name of the frontmost process:
tell application "System Events" to get name of some process whose frontmost is true
I don’t mean to convey that it isn’t useful, but the command’s name is poor. It’s bad language as far as API design goes because it could mean two things. “Some” means both random and a group. E.g., “give me some [random piece of] junk” and “give me some [random amount of] junk”.
If they’d wanted to convey “random” when designing that feature, why didn’t they implement it with a less ambiguous name in the first place? Having commands whose language requires context is kinda sketchy API design, especially when it isn’t the only option.
It looks comical because, well, it kinda is, despite its usefulness.
The best way to run it is from the system’s own Script Menu (which can be activated with the “AppleScript Utility” application) or a third-party menu like FastScripts. If you create a folder for Safari scripts in an “Applications” folder in your user’s Scripts folder and put your script in that, its name will only be visible in the menu when Safari’s the frontmost app.
-- Create and open a folder for scripts to be used from Script Menu when Safari is frontmost.
set SafariScriptsPath to (path to scripts folder as Unicode text) & "Applications:Safari:"
do shell script ("mkdir -p " & quoted form of POSIX path of SafariScriptsPath)
tell application "Finder"
make new Finder window to folder SafariScriptsPath
If that’s not reasonable for your purposes, you could set the ‘visible’ of your script application’s application process to false in System Events; the frontmost application would then be the one that was frontmost before you ran the script. However, if you ran the script by double-clicking on its icon, it’s very likely that you’ll have activated the Finder in the process, so you’d need to make that temporarily invisible too.
If you’re running a script from FastScripts or Script Menu, it doesn’t need to be an application. You can save it as an ordinary, compiled script. This discussion would then be academic because, with FastScripts, the script wouldn’t need to get out of the way. The application that was formerly frontmost would still be frontmost until something else happened to change that. The script would also kick in more quickly because it wouldn’t have to be launched as an application.
Getting out a copy I have of FastScripts Lite 2.3, it seems that keyboard shortcuts to trigger scripts only work when the menus showing those scripts are active. If your script’s in a folder called “Safari” in a folder called “Applications” in the “Scripts” folder of your user “Library” folder, the menu showing it will only be active when Safari’s the frontmost application, so the shortcut will only work when Safari’s frontmost anyway. This too renders the current discussion redundant.
Not to deny you the knowledge of how to make the frontmost application invisible, you’d need something like this:
tell application "System Events"
-- Make the current frontmost application invisible.
set visible of first application process whose frontmost is true to false
-- Check if the next frontmost application is Safari.
set SafariNext to ((name of first application process whose frontmost is true) is "Safari")
tell application (path to frontmost application as Unicode text)
if (SafariNext) then
display dialog "I'm Safari!"
display dialog "I'm not Safari!"