osascript to set secondary desktop background?

I have been using this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST
1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
 <key>Label</key>
  <string>com.filmscoring.display.plist</string>
 <key>ProgramArguments</key>
  <array>
    <string>osascript</string>
    <string>-e</string>
    <string>tell application "Finder"
set f to POSIX file "/Library/Desktop Pictures/mac cinema desktop yoda.png"
if desktop picture is not file f then set desktop picture to f
end tell</string>
  </array>
 <key>StartInterval</key>
  <integer>10</integer>
</dict>
</plist>

To prevent users from changing the desktop. (And I love it btw!) Original can be found here: How to restrict changing desktop wallpaper?

All of our stations have a secondary screen which I'd also like to fix up the same way using a different image.

Any idea how I can target the secondary display with this code? Thanks for the help!


Solution 1:

Quick Google search revealed the following code:

set tlst to {}
--set newtop to (path to home folder as string) & "Pictures:desktop pics folder:BigDipper 916.jpg" as alias -- sample line
set newtop to choose file


tell application "System Events"
   set tlst to a reference to every desktop
   --set picture of item 1 of tlst to newtop -- display 1
   set picture of item 2 of tlst to newtop -- display 2
end tell

Taken from

http://macscripter.net/viewtopic.php?id=35840

I don't have a second monitor so I cannot test it.

Hope it helps.

Solution 2:

Works on macOS Big Sur

For the Default monitor

osascript -e 'tell application "System Events" to set picture of current desktop to "<path-to-img>"'

For the second monitor

osascript -e 'tell application "System Events" to set picture of second desktop to "<path-to-img>"'