- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on
12-13-2018
07:27 AM
- last edited
Tuesday
by
kh-richa_mig
I'd like to pass a file name via a default script parameter into a bash script which calls an osascript, but am running into problems.
I've been fiddling around with something like the below, but am continually getting errors. The script does work perfectly if I hard-code the full path, and the file exists, so it must be something to do with pulling in the parameter.
$4 is set to green.png in my policy
Any ideas out there?
#!/bin/sh
if [ "$4" != "" ]
then
osascript -e 'tell application "System Events" to set picture of every desktop to ("/Library/Desktop\\ Pictures/$4" as POSIX file as alias)'
fi
exit 0
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-13-2018 08:37 AM
Try this:
#!/bin/sh
if [ "$4" != "" ]; then
/usr/bin/osascript << EOD
set imageName to do shell script "echo "$4""
tell application "System Events" to set picture of every desktop to ("/Library/Desktop Pictures/" & imageName as POSIX file as alias)
EOD
fi
exit 0
Edit: Changed script back to using $4

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-13-2018 08:37 AM
Try this:
#!/bin/sh
if [ "$4" != "" ]; then
/usr/bin/osascript << EOD
set imageName to do shell script "echo "$4""
tell application "System Events" to set picture of every desktop to ("/Library/Desktop Pictures/" & imageName as POSIX file as alias)
EOD
fi
exit 0
Edit: Changed script back to using $4

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-13-2018 08:45 AM
The variable needs to be escaped with double quotes, single quotes and double quotes again to properly escape the osascript command and pass the variable.
osascript -e 'tell application "System Events" to set picture of every desktop to ("/Library/Desktop\\ Pictures/"'"$4"'" as POSIX file as alias)'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 12-13-2018 09:10 AM
Thanks both so much for answering - @mm2270 That is spot on - thank you so much - that has been driving me and my colleagues crazy for about 2 days!!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Posted on 05-06-2020 05:21 AM
man thanks I had a similar issue that ""$VARIABLE"
" was hanging us up too. Thanks for the assist.
