How do I run apple script on a particular instance of Firefox?

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