Open URLs (given as args) in _background_ tabs in Safari

I don’t know AppleScript very well, so I tend to do things very kludge-y, usually wrapping AppleScript in a shell script.

For example, if I want to make a script that will take X number of arguments (all of which are URLs) and open each one as a background tab in Safari, the only way I know how to do it is like this:

for MY_URL in "$@"
do

/usr/bin/osascript <<EOT
set myURL to "$MY_URL"

tell application "Safari"
  tell front window
    make new tab at end of tabs with properties {URL:myURL}
  end tell
end tell

EOT

done

but it is very slow, I assume because it is a mix of shell scripting and AppleScripting.

Is there a way to do that with just AppleScripting?

(As far as I know, there’s no way to specify opening an URL in a background tab using shell scripting alone.)

Take them from where?

Most likely a command line / shell script situation where I’ll want to call it followed by a bunch of URLs and have them each opened in a separate background tab in Safari.

You’d be better to pass a single string containing all the URLs, so you only have to launch one instance of osascript. Assuming you delimit the URLs by linefeeds, the AS code would be like this:

set myURLS to "$MY_URLS"
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {linefeed}}
set myURLS to text items of myURLS
set AppleScript's text item delimiters to saveTID
repeat with oneURL in myURLS
	tell application "Safari"
		tell front window
			make new tab at end of tabs with properties {URL:oneURL}
		end tell
	end tell
end repeat
1 Like

Wowzers, ok that’s like 10,000 times faster.

Thank you! :star: :heart: :+1: