Open URL in new Safari tab with AppleScript

前端 未结 12 1656
挽巷
挽巷 2020-12-13 12:43

Is it possible to use AppleScript to open a link in a new tab in Safari?

相关标签:
12条回答
  • 2020-12-13 13:08

    You can try following approach:

    //make Safari window active and topmost
    tell application "Safari" to activate
    //start communication with Safari
    tell application "Safari"
        tell window 1
            //create new tab and open specified URL
            tab with properties {URL:"https://url.com"})
            //make tab active
            set visible to true
        end tell
    end tell
    

    Also u can combine usage of Apple script within FastScript (free for 10 shortcut)

    To add your script - just save script in /Library/Scripts. After you will be able to set some shortcut for new script.

    If you want to open new Window than new tab u can play within next:

    tell application "System Events"
        tell process "Safari"
            click menu item "New window" of menu "File" of menu bar 1
        end tell
    end tell
    

    Note: you need to allow AppleScript to use specialCapabilities in security settings in this case.

    0 讨论(0)
  • 2020-12-13 13:10

    Not the shortest solution but also works, and not only in English ...

    tell application "Safari"
        activate
    end tell
    
    tell application "System Events"
        set frontmost of process "Safari" to true
        keystroke "t" using {command down}
    end tell
    
    set myURL to "anyurl.html"
    delay 2
    tell application "Safari" to set the URL of the front document to myURL
    
    0 讨论(0)
  • 2020-12-13 13:16

    I ended up using automator to do this which was much easier and it works.

    0 讨论(0)
  • 2020-12-13 13:17

    I've been using the following script to open hundreds of docs into tabs in a single window.

    tell application "Safari"
        tell window 1
            make new tab with properties {URL:"http://foo.com/bar"}
            make new tab with properties {URL:"http://foo.com/baz"}
        end tell
    end tell
    

    But that no longer works in Safari 5.1 on Lion. It would open the new tab, but it wouldn't load the URL provided in the properties glob. I modified it to the following, which now works:

    tell application "Safari"
        tell window 1
            set URL of (make new tab) to "http://foo.com/bar"
            set make new tab to "http://foo.com/baz"
        end tell
    end tell
    
    0 讨论(0)
  • 2020-12-13 13:17

    It's been a while since a new answer's been posted here. I think this is the optimal way to do this. It will open Safari if it's not open, create a new window if there are no windows open, and add the tab to the current (or newly created) window.

    tell application "Safari"
        activate
        try
            tell window 1 to set current tab to make new tab with properties {URL:theURL}
        on error
            open location theURL
        end try
    end tell
    
    0 讨论(0)
  • 2020-12-13 13:17

    I can't comment :-/ so I will answer to say that Tim's answer (above) works as of OS X 10.8.5. This one-line version of his script also works:

    tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
    

    Arrgh -- the one line is overflowing. Here it is without the code tags:

    tell window 1 of application "Safari" to set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})

    0 讨论(0)
提交回复
热议问题