Set multiple monitor background policy

Chris_Holm
New Contributor II

I'm fairly new in learning scripting but google has been a good friend at times. Anyway I have a script that works in ARD to force an image in the /Library/Desktop Images folder as the background for multiple monitors but I can't get it to work from a policy and not certain what I'm doing wrong.

This works in ARD but not from a policy or Casper Remote:
osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/QICDesktop.jpg"'

I've tried adding the #!/bin/sh but still get an error in logs of : Script result: 57:64: syntax error: Expected class name but found application constant or consideration. (-2741)

Any help is appreciated. I'm more of a hardware guy and this may seem fairly easy to some so forgive the rookie scripting question.

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

@justin.smith][/url - this isn't true. osascript is a command line binary for running AppleScripts in a shell environment. (short for Open Scripting Architecture) and generally works fine. Its something that's included in every version of OS X since the beginning.

I suspect the reason it isn't working is because ARD tends to run scripts or Unix commands as the current user unless you specify that it run the Unix command as something like root. OS X is very strict about what it allows to run as the user when its being run by another account or in a root context.
/url">@Chris_Holm][/url - take a look at this thread for some information on how you may be able to get this to work, i.e, run the osascript commands as the current user from a policy.
[https://jamfnation.jamfsoftware.com/discussion.html?id=9902

Edit: Just wanted to mention to pay special attention to quoting and escaping in the command in the above linked thread, as they are super important to getting it work right.

View solution in original post

6 REPLIES 6

jssmith
New Contributor III

.

jssmith
New Contributor III

Adding "the shebang (#!/bin/)" tells the computer to expect a shell script, but your code is still an apple script. You can use python to run your apple script as a subprocess. I'm not as well versed in python, but hopefully that's enough info to get you started.

mm2270
Legendary Contributor III

@justin.smith][/url - this isn't true. osascript is a command line binary for running AppleScripts in a shell environment. (short for Open Scripting Architecture) and generally works fine. Its something that's included in every version of OS X since the beginning.

I suspect the reason it isn't working is because ARD tends to run scripts or Unix commands as the current user unless you specify that it run the Unix command as something like root. OS X is very strict about what it allows to run as the user when its being run by another account or in a root context.
/url">@Chris_Holm][/url - take a look at this thread for some information on how you may be able to get this to work, i.e, run the osascript commands as the current user from a policy.
[https://jamfnation.jamfsoftware.com/discussion.html?id=9902

Edit: Just wanted to mention to pay special attention to quoting and escaping in the command in the above linked thread, as they are super important to getting it work right.

Chris_Holm
New Contributor II

So if I understand correctly the script needs to find logged in user first and then run as said user?

#!/bin/sh

## Get the logged in user's name
loggedInUser=$( ls -l /dev/console | awk '{print $3}' )
## Get the PID of the logged in user
loggedInPID=$( ps -axj | awk "/^$loggedInUser/ && /Dock.app/ {print $2;exit}" )

## Use the above to run Applescript command to set the QICDesktop background
/bin/launchctl bsexec "${loggedInPID}" sudo -iu "${loggedInUser}" "/usr/bin/osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/QICDesktop.jpg"'

mm2270
Legendary Contributor III

Yes, give that a try. I can't guarantee it will work since I haven't tried it with the commands you're specifically trying, but it should work.

Only thing is, your last line should look like this.

/bin/launchctl bsexec "${loggedInPID}" sudo -iu "${loggedInUser}" "/usr/bin/osascript -e 'tell application "System Events" to set picture of every desktop to "/Library/Desktop Pictures/QICDesktop.jpg"'"

Note the escaped double quotes around "System Events" and the path to the image, plus the final double quote at the end to complete the quotes around the command.

Chris_Holm
New Contributor II

Thanks @mm2270 for your help, that did it. Ran at check-in and worked on all 5 test machines with varying OS versions.