Open list of URLs in your clipboard as new tabs in Safari I want to open a list of tabs in Safari I have currently open in Chrome.
In Chrome there i use the Copy all URLs extension, which allows me to copy all open tabs URLs into the clipboard. The same extension allows me also to open a new tab for a list of URLs in the clipboard.
I found Tabs to Links, which allows me to copy all open Safari tabs as URLs. But nothing to open tabs for a given list of URLs.
Do I miss something? Has anybody a suggestion how to solve this?
Thanks in advance.
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 &quot;Safari&quot;
    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 &quot;/private/tmp/&quot; &amp; windowName &amp; &quot;.html&quot;
set tmpFileContent to &quot;&lt;html&gt;&lt;head&gt;&lt;title&gt;&quot; &amp; windowName &amp; &quot;&lt;/title&gt;&lt;/head&gt;&lt;/html&gt;&quot;

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

tell application &quot;Safari&quot;
    
    make new document with properties {URL:&quot;file://&quot; &amp; 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 &quot;System Events&quot; to delete file tmpFileName


--  # Handler #

on writeToFile(theData, theFile, overwriteExistingContent)
    try
        set theFile to theFile as string
        if theFile contains &quot;/&quot; 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 &gt; New     ⌘N
Choose a type for your document: Quick Action
Set Workflow receives [no input] in [Safari]
Add a Run AppleScript action from: Actions &gt; Library &gt; 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 &gt; Keyboard &gt; Shortcuts &gt; 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.