0

I have two versions of firefox. One has an application name "Firefox" and the other is called "Firefox 2".

I'd like to tell AppleScript to close both instances of Firefox. Here's my code

tell application "Firefox" to quit
tell application "Firefox 2" to quit

It's weird because I can type this into Script Editor, but when I press save it always turns into this.

tell application "Firefox" to quit
tell application "Firefox" to quit

enter image description here

I ran this code and I'll put the output below.

tell application "System Events"
    set theVisibleApps to (name of application processes where visible is true)
end tell

{"Finder", "Terminal", "Script Editor", "firefox", "firefox"}

And here's the error code: error "Firefox got an error: Connection is invalid." number -609

How do I make sure they both close?

1

I'm assuming that you made a physical. on-disk copy of Firefox.app, and renamed it in the Finder to "Firefox 2.app". Is that correct? If so, that is going to cause you headaches. The visible name in the Finder isn't really used by the system much. The system uses the app's bundle identifier — for Firefox, I believe that's something like 'org.mozilla.firefox' — which is found in the info.plist file within the app bundle, and draws display names and such from that plist also. Unix, on the other hand, tends to use the name of the executable, which is in the MacOS folder inside the app bundle. Since your physical copy will have the same bundle ID as the original, the system will get confused if you try to distinguish between the apps that way.

The normal way to run two instances of an application is to use the unix open command with the -n option, e.g., one of these:

open -n '/Applications/FireFox.app/Contents/MacOS/firefox'

open -n -g org.mozilla.firefox

Each opened instance will have the same name and bundle id, but they will have different process identifiers (pid), and you can distinguish between them using those. There are a few different ways of doing that, depending on how you've set things up, but for a quick hack in AppleScript you can use something like:

tell application "System Events"
    set unixIDs to unix id of (every process whose name is "Firefox")
end tell

Whichever unix ID is lower should generally be the instance that was launched first.

2
  • Just an FYI... Firefox does not allow multiple running occurrences started with the open command to stay alive and why the OP has made second copy of the application bundle and renamed it. The only thing the first example open command does, in Terminal, is after attempting to start, it immediately terminates the additional occurrence and open another window for the other occurrence, while leaving a [Process completed] Terminal window open to be dealt with. The second example open command return an error e.g. The file /Users/me/org.mozilla.firefox does not exist. ... Cont. ... – user3439894 Apr 13 '20 at 4:11
  • ... Cont. ... even though I have an occurrence of Firefox running. Using the -b option instead of -g fixes the error but the end results are the same in this use case. By the way, the same holds true for Google Chrome too. That said, there is no need to worry about the PID's as this can be handled differently, as shown in my answer to the OP. – user3439894 Apr 13 '20 at 4:11
0

To keep Script Editor from changing "Firefox 2" to "Firefox", the real issue you're having, the following example AppleScript code will resolve your issue and allow for a graceful quitting of both occurrences of Firefox:

set fx1 to "Firefox"
set fx2 to "Firefox 2"

tell application fx1 to quit
delay 2
tell application fx2 to quit

In the screenshot below you can see in the Replies pane that each occurrence of Firefox returned error number 0, which is a graceful quit! Any error number other then 0 means something went wrong.

Script Editor window


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

-2

If you do a 'ps' from the command line you'll get the PID for each version of Firefox. You can then kill a process by typing kill -9 PID. There should be a way to grep out the PID for each process and then kill each one. Maybe use awk which i know little about.

2
  • You do not need to use ps or awk for that matter. One could simply use killall firefox, however that does not gracefully quit Firefox. You also do not need to first use ps to get the PID to then use kill -9 PID because pkill firefox will do just as killall firefox does. But if you do want to get the PID of Firefox first to use kill you could use kill $(pgrep firefox). That said, none of these examples will quit Firefox gracefully, even with the right signal, because Firefox is just very finicky/sensitive to the type of method. Believe me I've tried. – user3439894 Apr 13 '20 at 3:41
  • I suggested ps because the OP wanted to write a script to automate killing all versions. Since a script was desired I suggested using ps to get PID and then use kill to kill the versions that are running. I understand what you are saying about a graceful exit but that would require a more sophisticated script I suspect. I don't think my answer warrants a downvote. Certainly not 2. My method, in theory, works as good as the others except I don't know how to pull out the PID after running ps. – Natsfan Apr 13 '20 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .