How do I instruct Applescript to open a new Firefox window with a link?
Asked Answered
D

7

16

My code looks like this

tell application "Firefox"
 open location "http://rubyquicktips.tumblr.com/"
end tell

But if I have Firefox open, the link will open in a new tab. But I want the link to open in a new Firefox window. How can I accomplish that?

Duchess answered 5/9, 2010 at 10:36 Comment(0)
S
6

try this...

tell application "Firefox"
    OpenURL "http://rubyquicktips.tumblr.com/"
end tell

or try this...

tell application "Firefox" to activate
tell application "System Events" to keystroke "n" using command down
tell application "Firefox"
    OpenURL "http://rubyquicktips.tumblr.com/"
end tell
Stinkstone answered 5/9, 2010 at 20:13 Comment(2)
OpenURL doesn't work with Leopard...but your second tip is brilliant!Duchess
As for now, we're actually on Mac OS 10.12.5. This solution won't work anymore. If someone still want to do so, you should replace the OpenURL by open location, like blubb is mentionning in is answer.Ponder
D
12

this works, but opens your welcome site in first tab...

tell application "Firefox" to activate
tell application "System Events" to keystroke "n" using command down
delay 3
tell application "Firefox"
    open location "http://rubyquicktips.tumblr.com/"
end tell
Dew answered 10/2, 2011 at 23:30 Comment(0)
S
6

try this...

tell application "Firefox"
    OpenURL "http://rubyquicktips.tumblr.com/"
end tell

or try this...

tell application "Firefox" to activate
tell application "System Events" to keystroke "n" using command down
tell application "Firefox"
    OpenURL "http://rubyquicktips.tumblr.com/"
end tell
Stinkstone answered 5/9, 2010 at 20:13 Comment(2)
OpenURL doesn't work with Leopard...but your second tip is brilliant!Duchess
As for now, we're actually on Mac OS 10.12.5. This solution won't work anymore. If someone still want to do so, you should replace the OpenURL by open location, like blubb is mentionning in is answer.Ponder
G
5

I'm not entirely familiar with AppleScript, but I was looking to open a brand new default window. Here's a method that works:

tell application "System Events"
    tell process "Firefox"
        click menu item "New Window" of menu "File" of menu bar 1
    end tell
end tell

Optionally, to focus on the new window, add these lines afterward:

tell application "Firefox"
    activate
end tell

This will open a default new window. There may be a better way.

Golgi answered 28/10, 2013 at 21:55 Comment(0)
F
4

Note:

As of at least Firefox v50, you can make Firefox default to opening URLs in a new window, by unchecking Open new windows in a new tab instead on the General tab of Firefox's preferences.

Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.

The solution below may still be of interest if you don't want to rely on the state of that setting.
(Unfortunately, due to Firefox's limited AppleScript support, there is no similarly robust solution for always opening in a tab, irrespective of the state of the setting).


Here's a more robust solution that:

  • doesn't rely on a fixed delay and

  • is language-neutral (as is the keystroke-sending approach); i.e., it also works with localized names for the menus and commands (e.g., "Datei" for "File", ...)

However, because GUI scripting is employed for programmatically clicking on a menu item, you do need to authorize the calling applicationfor assistive access first (a one-time action that requires administrative privileges) - e.g., if your script is run from a terminal, you must authorize Terminal.app, but even Script Editor.app must be authorized if you want to run your script while developing it.

tell application "Firefox"

    # Make Firefox frontmost.
    activate

    # Wait until it is truly frontmost.
    repeat while not frontmost
        delay 0.1
    end repeat

    # Open a new window using GUI scripting (requires authorization for assistive access),
    tell application "System Events" to tell application process "Firefox"
        set windowCountBefore to (count of windows)
        # Click on File > New to open a new window, but locate it
        # by keyboard shortcut rather than by name, so as to also work
        # with localized menu names.
        tell menu 1 of menu bar item 3 of menu bar 1
            click (first menu item whose value of attribute "AXMenuItemCmdChar" is "N" and value of attribute "AXMenuItemCmdModifiers" is 0)
        end tell
        # Wait for the new window to appear.
        repeat while (count of windows) = windowCountBefore
            delay 0.2
        end repeat
    end tell

    # Finally, open the URL.    
    open location "http://example.org/"

end tell

Note:

  • Under normal circumstances, if Firefox is responsive, this should work reliably. However, the code could be improved by implementing timeouts for the waiting loops.

  • Firefox isn't the fastest at opening the new window, so Firefox will first activate, and the new window will only appear about a second or so later.

  • The new window will invariably have an empty first tab, and the URL will open in the 2nd tab.

A side note re the unusual-looking tell application "System Events" to tell application process "Firefox" construct:

  • The main block targets the Firefox application (tell application "Firefox").
  • However, GUI scripting must be done in the context of the System Events application (tell application "System Events"), and, within that context, it is the Firefox process that must be targeted (tell application process "FireFox"). While you could write two nested tell blocks, they're combined into a single block here for convenience and brevity.
Furuncle answered 22/11, 2016 at 14:23 Comment(1)
@EdRandall: Yes, whatever application is executing the GUI scripting code must be authorized; I've added an explicit note re Script Editor. Re timeouts: Good idea, but the answer already states "the code could be improved by implementing timeouts for the waiting loops"; as an aside: it is not lack of authorization that could cause these specific loops to get stuck (the 1st one doesn't require authorization, and the 2nd one would not be reached if authorization were lacking).Furuncle
R
1

solutions which "activate" are wrong because they switch desktop

do shell script "open -n -a Firefox"
Refusal answered 8/3, 2021 at 12:31 Comment(3)
RE: "solutions which "activate" are wrong because they switch desktop" -- Well that is not true if one is only using one, and only true if using more than one and the target app is not opened on the current one. Also, the OP wants to open a URL in that new window and as such your answer is incomplete.Debbidebbie
have not found better alternative, for Safari I used "make new document", but with Big Sur update they made new window in Safari opening for 5 seconds (idk if they did it on purpose to make M1 macs feel faster)Refusal
The OP is talking about Firefox, so Safari has nothing to do with this! Your answer as written is factually inaccurate and incomplete of what the OP requests!Debbidebbie
I
1

I use the below simple shell script to open multiple firefox pages in different tabs and it works for me. May be you need to adjust the sleep time little bit.

Steps:

  1. Go to Automator

  2. Select: Application

  3. Click on choose

  4. Search Run Shell Script

  5. Click and drag it to the right hand window

  6. In Shell select /bin/bash

  7. In Pass input: select as arguments

  8. Paste the below code

    #!/bin/bash
    
    open -a Firefox https://stackoverflow.com
    sleep 0.5
    open -a Firefox https://apple.com
    sleep 0.5
    open -a Firefox https://google.com
    
  9. Command + S to save it

  10. Give a name and location to save.

  11. Choose File format: Application

  12. Save

  13. Add it in your dock (applicable for MAC os) and enjoy (applicable for every os :)!!

Ichthyology answered 30/11, 2021 at 8:20 Comment(0)
D
0

I know this post is old, but I had a similar question now in 2024 on MacOS Sonoma which doesn't seem to allow the keystroke option in Automator.

If you put the following script in Automator as a Quick Action (Run AppleScript):

on run {input, parameters}
    tell application "Terminal"
        if it is running then
            do script "open -n -a /Applications/Firefox.app --args -new-window 'http://rubyquicktips.tumblr.com/';exit"
        end if
        activate
    end tell
end run

It should do what you want with the main downside being that the terminal will flicker open.

I didn't want any particular website to open, so I ran the same without the --args option.

Deborahdeborath answered 16/7 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.