user2407195
user2407195

Reputation: 11

How do I pass a file path parameter to an osascript called from command line?

I am using a shell script which is calling these two lines of code:

iname=$(ls -d -1 $PWD/*jpg) 
osascript -e 'tell app "Finder" to set desktop picture to POSIX file \"$iname\"'

where iname is a variable, which is the absolute path to the picture I want. From what I've read, this is how to pass variables to osascripts. But I am getting this error when trying to run these two lines

55:56: syntax error: Expected expression, property or key form, etc. but found unknown token. (-2741)

Can someone please explain how I can fix this?

Upvotes: 1

Views: 1418

Answers (2)

nisetama
nisetama

Reputation: 8863

You can also use an explicit run handler to get command line arguments:

osascript -e'on run {a}
    tell app "finder" to set desktop picture to posix file a
end' "$iname"`

If the path can be relative, you can use GNU readlink to convert it to an absolute path:

osascript -e'on run {a}
    tell app "finder" to set desktop picture to posix file a
end' "$(greadlink -f "$iname")"

If you need to pass a list of absolute paths to a script, you can do something like this:

osascript -e'on run a
    set l to {}
    repeat with f in a
        set end of l to posix file f
    end
    l
end' "$@"

If you need to pass a list of paths that to a script and convert relative paths to absolute paths, you can do something like this:

osascript -e'on run {a}
    set l to {}
    repeat with f in (get paragraphs of a)
        set end of l to posix file f
    end
    l
end' "$(greadlink -f "$@")"

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246744

You are trying to expand a variable in single quotes. Try this:

osascript -e 'tell app "Finder" to set desktop picture to POSIX file "'"$iname"\"

Upvotes: 3

Related Questions