Can't check if application is open

Hello, all.

I’ve got a mouse with…a lot of buttons. And right now, I use the wheel button to launch Safari.

However, I do a lot of web development/design, and I tend to preview in Firefox so that I can use Firebug.

With that in mind, I set out to write a script that would check if Firefox was open, and then if it was, select it instead of launching Safari. This is what I have:


tell application "System Events" to set ffopen to (get name of every application process) contains "Firefox"
if ffopen then
	tell application "Firefox" to activate
else
	tell application "Safari" to activate
end if

However, even when Firefox is open, this script opens Safari. The value is always getting set to false, whether Firefox is open or not. I’ve taken a look under Activity Monitor, and the process name is “Firefox”. I also tried with variations like “fox”, or “firef”.

Does anyone know what I might be doing wrong in my application check?

Thanks,
Girasquid

Edit: It’s interesting to note that this script works just fine, opening Safari if it’s closed and Firefox if Safari is already open:


tell application "System Events" to set ffopen to (get name of every application process) contains "Safari"
if ffopen then
	tell application "Firefox" to activate
else
	tell application "Safari" to activate
end if

This works for me:


tell application "System Events" to set ffopen to (get name of every application process whose visible is true)
ffopen contains "firefox-bin" --> true

It was all working, and then I noticed something: this doesn’t work if Firefox is ‘behind’ another window.

The script seems to work when run under Script Editor, but not otherwise.

The property visible is no concerned at all with the order of the windows. It only indicates that the application is accessible in the GUI.
An other way is exists process


tell application "System Events" to set ffopen to exists process "firefox-bin"
if ffopen then
	activate application "Firefox"
else
	activate application "Safari"
end if

It still doesn’t quite seem to work.

If I remove my focus from any windows(the word “Finder” is in the top left), and then fire the script, even with Firefox open it still opens Safari.

Edit: ack, nevermind. I’ve got localized settings for my mouse; it turns out that I’ve accidentally set most of my clicks to open Safari instead of run my script. Adjusting them fixed the issue.

Thanks for the code, though - I’ve put up my finished script here.

I am trying to do a similar thing but with different apps. It seems to open up Safari for me only in Lion 10.7.4. Where did you get the event process ID? I looked through Activity Monitor and could find it anywhere on “inspect” nor by adding all the view items. All I see is numbers that can change.
http://reference.Studioprime.com/forums/macscripter/ApplicationProcessIDChangesWithEachOpen.jpg

I want to open an app if closed and close the app if open. I extracted this from another post and modified it, but don’t know if this is the best way to do this. http://macscripter.net/viewtopic.php?id=38019


--checks if TextEdit is open if it is not then will open it up; if TextEdit is open it will close it.
tell application "System Events"
	if (exists process "TextEdit") then 
		tell application "TextEdit" to quit
	else
		tell application "TextEdit" to activate
	end if
end tell

Browser: Safari 534.57.2
Operating System: Mac OS X (10.7)

Since AppleScript 2.0 (introduced in Leopard) it’s quite easy


if application "TextEdit" is running then
	quit application "TextEdit"
else
	activate application "TextEdit"
end if

That’s great, I am glad I asked, thanks!