开发者

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

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 accomp开发者_运维技巧lish that?


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


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


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.


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.


solutions which "activate" are wrong because they switch desktop

do shell script "open -n -a Firefox"


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 :)!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜