Skip to main content
Solved

Passing a script parameter into osascript in shell to create file path


Forum|alt.badge.img+3
  • New Contributor
  • 6 replies

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

Best answer by mm2270

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

View original
Did this topic help you find an answer to your question?

4 replies

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • Answer
  • December 13, 2018

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


Serge
Forum|alt.badge.img+13
  • Contributor
  • 26 replies
  • December 13, 2018

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)'

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 6 replies
  • December 13, 2018

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!!


Forum|alt.badge.img+5
  • New Contributor
  • 9 replies
  • May 6, 2020

man thanks I had a similar issue that ""$VARIABLE"" was hanging us up too. Thanks for the assist.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings