3

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.

2 Answers 2

4

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.

4
  • Oh! Thanks for offering this nice solution. I really hoped for a just install extension or the like, as I have no experience with AppleScript so far. Maybe I have to take a look finally. :)
    – andreas
    Commented Apr 27, 2021 at 20:11
  • sorry. didn't formulate properly. Of course I looked around and checked the description of at least 6 Safari extensions regarding this functionality. Thanks for adding the description on how to execute the script. Very apprechiated! Thanks again.
    – andreas
    Commented Apr 28, 2021 at 22:34
  • 1
    @Andreas, I've added, to the answer, the details to use as an Automator Service/Quick Action in a bit more detail than what was in the comment. Also mentioned it's usage with third-party applications as applicable. Commented Apr 28, 2021 at 22:57
  • This is very handy. Thank you. No additional extension is necessary to copy the URLs of all the tabs from Chrome. See this answer Commented Feb 10 at 8:32
1

To copy the URLs of all tabs in Chrome, no additional extension is necessary. I followed the following steps (as suggested by Google) to copy all the URLs

Here's how to save all open tabs in Google Chrome:

1. Click the three-dot menu in the upper-right corner of Chrome.
2. Select Bookmarks.
3. Select Bookmark All Tabs.
4. Type a name for the folder.
5. Select Save. 

Here's how to copy the URLs of all open tabs in Chrome:

1. In the Bookmark manager, open the folder with the tabs.
2. Press Ctrl + A to select all the bookmarks.
3. Right-click on any bookmark and select Copy from the context menu.
4. Open a text editor or a Word document.
5. Right-click and Paste or press Ctrl + V.
6. The bookmarks links will appear as plain text.

After this, I followed this answer to open the tabs in Safari. I simply used the first example script and didn't even save it. I directly pressed the run/play button in the Automator as shown below

Automator screenshot

Replicating the AppleScript code from the above answer in case that answer is removed

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

PS: I know this answer does not add any additional information regarding the question. However, this answer comes in handy if someone comes across this question before installing a Chrome extension to copy the URLs.

2
  • it's a nice way to copy all urls, but still you need to do a lot of legwork, compared to two clicks with the chrome extension I use currently...
    – andreas
    Commented Feb 12 at 9:53
  • Right! If you do it regularly, I guess an extension is easier. If someone is looking to do this only once or very rarely and do not want to install an extension for that, this provides an alternative. Commented Feb 13 at 9:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .