Open list of URLs in your clipboard as new tabs in Safari

In what format is the Copy all URLs extensions in Google Chrome placing the URLs on the clipboard?

If it is just lines of text with one URL on each line, then the following example AppleScript code will open a new window in Safari and open each URL in a new tab:

set theURLs to paragraphs of (the clipboard)

tell application "Safari"
    make new document with properties {URL:first item of theURLs}
    activate
    tell front window
        repeat with i from 2 to the length of theURLs
            set current tab to (make new tab with properties {URL:item i of theURLs})
        end repeat
        set current tab to first tab
    end tell
end tell


If you want to not have the target Safari window remain frontmost while opening each URL in a new tab, then here is a way to have that happen:

Example AppleScript code:

set theURLs to paragraphs of (the clipboard)

set windowName to random number from 1000000 to 9999999
set tmpFileName to "/private/tmp/" & windowName & ".html"
set tmpFileContent to "<html><head><title>" & windowName & "</title></head></html>"

if not my writeToFile(tmpFileContent, tmpFileName, true) then return

tell application "Safari"
    
    make new document with properties {URL:"file://" & tmpFileName}
    set i to 0
    repeat while not (exists (windows whose name is windowName))
        delay 0.1
        set i to i + 1
        if i = 30 then return
    end repeat
    set winID to (id of windows whose name is windowName) as number
    
    make new tab at end of tabs of window id winID with properties {URL:item 1 of myURLs}
    delete first tab of window id winID
    repeat with i from 2 to (length of myURLs)
        make new tab at end of tabs of window id winID with properties {URL:item i of myURLs}
        delay 1
    end repeat
    
end tell

tell application "System Events" to delete file tmpFileName


--  # Handler #

on writeToFile(theData, theFile, overwriteExistingContent)
    try
        set theFile to theFile as string
        if theFile contains "/" then
            set theOpenedFile to open for access theFile with write permission
        else
            set theOpenedFile to open for access file theFile with write permission
        end if
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        write theData to theOpenedFile starting at eof
        close access theOpenedFile
        return true
    on error
        try
            close access file theFile
        end try
        return false
    end try
end writeToFile

The example AppleScript code can be used in an Automator Service/Quick Action with a Run AppleScript action using the example AppleScript code in place of the default code.

Open: Automator -- File > New     ⌘N

Choose a type for your document: Quick Action

Set Workflow receives [no input] in [Safari]

Add a Run AppleScript action from: Actions > Library > Utilities

Replace the default code with the example AppleScript code.

Save the Automator Service/Quick Action.

Assign the new Service/Quick Action a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services.

It is then available from the Services menu in Safari and as a keyboard shortcut.

The example AppleScript code can also be used in any third-party application that has the ability to trigger AppleScript code and or assign it a keyboard shortcut. I personally use FastScripts and I am not associated with its developer, just a satisfied user of the product.


Notes:

  • This second example AppleScript code includes some error handling, in that if the tmp file (value of the tmpFileName variable) is not created the script aborts without any message. This can be changed by converting the if not my writeToFile ... statement to a full if block and include an appropriate display alert, display dialog or display notification command, as wanted, followed by the return command.

  • The repeat loop as coded is written to wait up to 3 seconds for the HTML file to load and should be more than enough time. Adjust if necessary.

  • As coded, it assumes the clipboard contains just lines of text with one URL on each line.


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.