9 C
London
Saturday, February 10, 2024

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


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

Whether it is simply strains of textual content with one URL on every line, then the next instance AppleScript code will open a brand new window in Safari and open every URL in a brand new tab:

set theURLs to paragraphs of (the clipboard)

inform software "Safari"
    make new doc with properties {URL:first merchandise of theURLs}
    activate
    inform entrance window
        repeat with i from 2 to the size of theURLs
            set present tab to (make new tab with properties {URL:merchandise i of theURLs})
        finish repeat
        set present tab to first tab
    finish inform
finish inform

If you wish to not have the goal Safari window stay frontmost whereas opening every URL in a brand new tab, then here’s a technique to have that occur:

Instance AppleScript code:

set theURLs to paragraphs of (the clipboard)

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

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

inform software "Safari"
    
    make new doc with properties {URL:"file://" & tmpFileName}
    set i to 0
    repeat whereas not (exists (home windows whose identify is windowName))
        delay 0.1
        set i to i + 1
        if i = 30 then return
    finish repeat
    set winID to (id of home windows whose identify is windowName) as quantity
    
    make new tab at finish of tabs of window id winID with properties {URL:merchandise 1 of myURLs}
    delete first tab of window id winID
    repeat with i from 2 to (size of myURLs)
        make new tab at finish of tabs of window id winID with properties {URL:merchandise i of myURLs}
        delay 1
    finish repeat
    
finish inform

inform software "System Occasions" to delete file tmpFileName


--  # Handler #

on writeToFile(theData, theFile, overwriteExistingContent)
    attempt
        set theFile to theFile as string
        if theFile accommodates "/" then
            set theOpenedFile to open for entry theFile with write permission
        else
            set theOpenedFile to open for entry file theFile with write permission
        finish if
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        write theData to theOpenedFile beginning at eof
        shut entry theOpenedFile
        return true
    on error
        attempt
            shut entry file theFile
        finish attempt
        return false
    finish attempt
finish writeToFile

The instance AppleScript code can be utilized in an Automator Service/Fast Motion with a Run AppleScript motion utilizing the instance AppleScript code rather than the default code.

Open: AutomatorFile > New     ⌘N

Select a sort in your doc: Fast Motion

Set Workflow receives [no input] in [Safari]

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

Change the default code with the instance AppleScript code.

Save the Automator Service/Fast Motion.

Assign the brand new Service/Fast Motion a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Providers.

It’s then accessible from the Providers menu in Safari and as a keyboard shortcut.

The instance AppleScript code will also be utilized in any third-party software that has the flexibility to set off AppleScript code and or assign it a keyboard shortcut. I personally use FastScripts and I’m not related to its developer, only a glad person of the product.


Notes:

  • This second instance AppleScript code contains some error dealing with, in that if the tmp file (worth of the tmpFileName variable) shouldn’t be created the script aborts with none message. This may be modified by changing the if not my writeToFile ... assertion to a full if block and embody an applicable show alert, show dialog or show notification command, as needed, adopted by the return command.

  • The repeat loop as coded is written to attend as much as 3 seconds for the HTML file to load and must be greater than sufficient time. Regulate if obligatory.

  • As coded, it assumes the clipboard accommodates simply strains of textual content with one URL on every line.


Word: The instance AppleScript code is simply that and sans any included error dealing with doesn’t comprise any further error dealing with as could also be applicable. The onus is upon the person so as to add any error dealing with as could also be applicable, wanted or needed. Take a look on the attempt assertion and error assertion within the AppleScript Language Information. See additionally, Working with Errors. Moreover, using the delay command could also be obligatory between occasions the place applicable, e.g. delay 0.5, with the worth of the delay set appropriately.

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here