Determining if Safari is frontmost

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.

Can anyone help me with this?

Thank you,


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
end if

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

Like the “some”. I’ve always used “first”, but with only one, some works just as well. :wink:

SOME? I’m shocked that, after knowing this exists, that there aren’t also:

set i to something sorta close to 10
if theObject kinda looks like window "mainWIndow" then
tell application "Safari" to load stuff

Not sure if you’re serious Mike, but “some” is very useful for randomizing a choice from a list.

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. :slight_smile:

Mikey-San just doesn’t, y’know, get it 'cause some of that stuff doesn’t do those things with those whatsits.



Well, it’s just like that one thing from the book where the dude said that stuff.

then again isn’t there a command like

round as taught in school

apple devs have wayyy to much time on their hands.

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
end tell

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.

I am using Fastscripts, and I have the script in the right folder. I have assigned a hotkey (shortcut) to trigger the script.
So I guess the solution is as you said:

“… set the ‘visible’ of your script application’s application process to false in System Events …”

This sounds exactly like what I am looking for.
How do I do this?
Thanks for your help,

Hi, Fed.

  1. 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.

  2. 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.

  3. 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")
end tell
tell application (path to frontmost application as Unicode text)
	if (SafariNext) then
		display dialog "I'm Safari!"
		display dialog "I'm not Safari!"
	end if
end tell

I am trying to run a script by pressing a mouse button. The only way I can figure out how to do it is to make use an app. I guess it doesn’t use fastscripts after all.

Is there any way to run a script from a mouse click?Thanks for the tips so far. I will give it a try.


Hi Fed,

with Apple’s Mighty Mouse you can run (script) applications with a mouse click