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

[亡魂溺海] 提交于 2019-12-22 04:26:13

问题


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?


回答1:


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



回答2:


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



回答3:


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.




回答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.


来源:https://stackoverflow.com/questions/3645763/how-do-i-instruct-applescript-to-open-a-new-firefox-window-with-a-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!