2

I have three instances of Firefox called -

  1. Firefox
  2. Firefox 2
  3. Firefox 3

They all live in the Applications folder and the difference is that they have different Firefox profiles and cookies.

I'd like to run an apple script that works on a particular instance of Firefox.

tell application "Firefox"
    activate
    tell application "System Events"
        keystroke "n" using {command down}
    end tell
end tell

tell application "Firefox"
    activate
    tell application "System Events"
        keystroke "l" using {command down}
        keystroke "a" using {command down}
        key code 51
        keystroke "https://google.com"
        key code 36
    end tell
end tell

If I run this with Firefox open then it works exactly how I want it to.

But, if Firefox is closed, but Firefox 2 or Firefox 3 are open then it has weird behavior.

It will replace the word Firefox with Firefox 2 or Firefox 3 (whichever is open and last active) and then it will run the script using that instance of Firefox which is not what I want.

3
  • There is an add on called “title” that allows you to change the name of the Firefox window. Maybe you can use that in conjunction with AS to identify the specific instance you want. I run a whole separate instance of FF but I append “Development” so it appears as “Firefox - Development”
    – Allan
    Commented Jul 11, 2020 at 21:33
  • As a side-note, don't put one tell application block inside another. Keep them separate. You can easily just have tell application "Firefox" to activate on a line of its own before your System Events block.
    – CJK
    Commented Jul 12, 2020 at 0:44
  • Awesome, mind sharing why that's a bad practice?
    – Cauder
    Commented Jul 12, 2020 at 0:46

1 Answer 1

1

To test the example AppleScript code, I duplicated Firefox and renamed the copy as Firefox 2.

This was texted under macOS High Sierra, using different scenarios:

  • Both Firefox and Firefox 2 running, with Firefox started first.
    • Firefox being frontmost.
    • Firefox 2 being frontmost.
  • Both Firefox and Firefox 2 running, with Firefox 2 started first.
    • Firefox being frontmost.
    • Firefox 2 being frontmost.
  • Just Firefox 2 running.

In all tested scenarios the example AppleScript code preformed as wanted, Firefox being the one acted upon, not Firefox 2.

I believe the key here is using the fully qualified pathname, e.g. "/Applications/Firefox.app" verses just "Firefox".

tell application "/Applications/Firefox.app" to activate
delay 1
tell application "System Events"
    keystroke "n" using {command down}
    delay 1
    keystroke "l" using {command down}
    keystroke "a" using {command down}
    key code 51
    keystroke "https://google.com"
    key code 36
end tell

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

You must log in to answer this question.

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