7

I have a file with a list of URL's. I need to pass each URL from a bash script to be used in an AppleScript.

while read urls
do 
    osascript -e \
    'tell application "Safari"
        tell window 1 
            tell current tab to open location "$urls"
        end tell
    end tell'
done <file.txt

This doesn't work. It passes the literal string $urls as the location to be opened in Safari. How can I pass the bash variable $urls to AppleScript?

2 Answers 2

3

This should work:

while read urls
do 
    osascript -e "tell application \"Safari\" to open location \"$urls\""
done<file.txt
2

The quotes don't expand the variable in AppleScript. What you need to do is ESCAPE the quotes:

'tell current tab to open location \"$urls\"`

However, a better way to do this is using run argv. For example:

#!/bin/bash
url="foobar.com"
osascript -e 'on run {myurl}' -e 'tell application "Safari" to set the URL of the document to myurl' -e 'end run' $url 

My preference is to use a separate script for the AppleScript. In bash, this would look like:

#!/bin/bash
osascript -e 'do scriptname \"$url\"'
8
  • The double quotes do expand, the single ones around the whole script don't. Commented Dec 29, 2016 at 16:39
  • @patrix - I've never run into the issue where what was in the single quote didn't work as expected - at least from what I recall. Commented Dec 29, 2016 at 16:56
  • I'm getting an error - syntax error: A “,” can’t go after this command name. Commented Dec 29, 2016 at 17:16
  • That comma was a typo; it shouldn't be there. Commented Dec 29, 2016 at 17:19
  • Ok I see that now. Will this work to open a list of multiple url's? Commented Dec 29, 2016 at 17:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.