Open URL in new Safari tab with AppleScript

前端 未结 12 1657
挽巷
挽巷 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:22

    I think this also does what you asked for, but it is much shorter and is less browser-specific:

    do shell script "open http://www.webpagehere.com"
    

    This will open the specified URL in your default browser. And if you explicitly want to open it in Safari, use this:

    do shell script "open -a Safari 'http://www.webpagehere.com'"
    
    0 讨论(0)
  • 2020-12-13 13:23

    Code:

    tell application "System Events"
    tell application "Safari" to activate
    tell process "Safari"
    click menu item "New Tab" of menu "File" of menu bar 1
    end tell
    end tell
    
    tell application "Safari"
    set URL of document 1 to "http://www.stackoverflow.com/"
    end tell
    

    One problem is that this only works if the system's language is set to English.

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

    This should usually create a new tab and focus it (or focus an existing tab if the URL is already open):

    tell application "Safari"
        open location "http://stackoverflow.com"
        activate
    end tell
    

    It opens a new window if "Open pages in tabs instead of windows" is set to never though.

    tell application "System Events" to open location doesn't work with some URLs that contain non-ASCII characters:

    set u to "http://ja.wikipedia.org/wiki/漢字"
    tell application "System Events" to open location u
    --tell application "Safari" to open location u
    --do shell script "open " & quoted form of u
    

    This opens a new tab even when new pages are set to open in windows:

    tell application "Safari"
        activate
        reopen
        tell (window 1 where (its document is not missing value))
            if name of its document is not "Untitled" then set current tab to (make new tab)
            set index to 1
        end tell
        set URL of document 1 to "http://stackoverflow.com"
    end tell
    tell application "System Events" to tell process "Safari"
        perform action "AXRaise" of window 1
    end tell
    

    set index to 1 doesn't raise the window, but it makes the window appear as window 1 to System Events, which can AXRaise it.

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

    Worked for me in Safari v.11

    tell application "Safari"
        tell window 1
            make new tab with properties {URL:"https://twitter.com"}
        end tell
    end tell
    
    0 讨论(0)
  • 2020-12-13 13:28

    I found a way to open a new tab in the background with Safari.

    tell application "Safari"
    
    set the URL of (make new tab in window 1) to "your.url.net"
    
    end tell
    

    During the time I wrote this answer I made this

    tell application "Safari"
    
    try
    
        display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
    
        set theURL to text returned of result
    
        set netProto to "https://"
    
        if theURL contains netProto then
    
            set the URL of (make new tab in window 1) to theURL
    
        else
    
            set the URL of (make new tab in window 1) to netProto & theURL
    
        end if
    
    end try
    
    end tell
    

    New version

    tell application "Safari"
    
    repeat
    
        try
    
            display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1
    
            set theURL to text returned of result
    
            if theURL is "" then exit repeat
    
            set netProto to "https://"
    
            if theURL contains netProto then
    
                set the URL of (make new tab in window 1) to theURL
    
            else
    
                set the URL of (make new tab in window 1) to netProto & theURL
    
            end if
    
            display dialog "Do you want to open a new tab?" buttons {"Yes", "No"} default button "Yes"
    
            if button returned of result is "No" then exit repeat
    
        end try
    
    end repeat
    
    end tell
    

    Any suggestions will be appreciate

    Best regards

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

    This will work:

    tell application "Safari"
        tell window 1
            set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
        end tell
    end tell
    
    0 讨论(0)
提交回复
热议问题