PDA

View Full Version : AppleScript to Change the Wallpaper of Two Screens


ecbtln
05-31-2008, 08:51 PM
I recently got a second screen for my iMac and love the prospect of having one giant panoramic picture as my background. However, I cannot just choose one, I have about 20 pictures that are in the running. The only way, if I am correct, to then set these panoramic as the background is to crop them accordingly and then choose separate pictures in System Preferences for each screen. But then, there comes the problem as to if you want the picture to change. When choosing change picture, it will randomly choose pictures that do not necessarily match up. I can't seem to find a solution to this rather than manually changing the backgrounds myself. First of all, is there any third-party app that does it for me? I doubt it, so then the question is, can this feat be accomplished with applescript and/or cron, somehow randomly selected a number that is assigned to a pair of pictures?
it would go something like this:

randomly generate number

if number = 1
set desktop picture screen 0 to 1a.jpg
set desktop picture screen 1 to 1b.jpg

if number = 2
set desktop picture screen 0 to 2a.jpg
set desktop picture screen 1 to 2b.jpg

and so on.

anyone have any ideas?

ecbtln
05-31-2008, 10:19 PM
i seem to have figured out every part of the script except for the place to actually specify which display to use, display 0 or display 1. I think applescript does it by using the display name, i just can't figure out how to specify the display name.

tell application "System Events"
-- SET DESKTOP TO SPECIFIC PICTURE
tell current desktop
set picture rotation to 0 -- (0=off, 1=interval, 2=login, 3=sleep)
set picture to file "Mac OS X:Library:Desktop Pictures:Plants:Agave.jpg"
end tell
end tell

if i stick in a set display name to "iMac", it just gives me an error, anyone know how to define this?

tw
06-01-2008, 12:22 AM
I don't think you want to use "current desktop". first, run this in the script editor while your second display is plugged in.

tell application "System Events"
get display name of every desktop
end tell

that will tell you the names of your displays. then write your script like this

tell application "System Events"
-- SET DESKTOP TO SPECIFIC PICTURE
tell desktop "name 1"
set picture rotation to 0 -- (0=off, 1=interval, 2=login, 3=sleep)
set picture to file "Mac OS X:Library:Desktop Pictures:Plants:Agave.jpg"
end tell
tell desktop "name 2"
set picture rotation to 0 -- (0=off, 1=interval, 2=login, 3=sleep)
set picture to file "Mac OS X:Library:Desktop Pictures:Plants:Agave.jpg"
end tell
end tell

me, I'd save it in the Script menu folder - that way I could have launchd run it periodically, and still run it myself if I feel like it.

ecbtln
06-01-2008, 01:02 AM
im getting an error from System Events by an Invalid key form highlighting the picture rotation line of code, any idea why?

ecbtln
06-01-2008, 08:22 AM
it doesn't seem to recognize the tell desktop "iMac" part of the code right under system events.

tw
06-01-2008, 01:38 PM
it doesn't seem to recognize the tell desktop "iMac" part of the code right under system events.

hmph! sorry, System Events seems to need a little arcane langauge. try this:

tell application "System Events"
-- SET DESKTOP TO SPECIFIC PICTURE
tell (every desktop whose display name is "Name 1")
set picture rotation to 0 -- (0=off, 1=interval, 2=login, 3=sleep)
set picture to file "Mac OS X:Library:Desktop:Pictures:Plants:Agave.jpg"
end tell
tell (every desktop whose display name is "Name 2")
set picture rotation to 0 -- (0=off, 1=interval, 2=login, 3=sleep)
set picture to file "Mac OS X:Library:Desktop:Pictures:Plants:Agave.jpg"
end tell
end tell

ecbtln
06-01-2008, 02:00 PM
yup that does it, thanks

Milko
12-11-2008, 12:02 AM
great script tw, thank you.
I have 2 cinema HD displays connected to my mac. The name of both displays is the same (Cinema HD). How can I change the desktop picture only on the second display?

tw
12-11-2008, 12:37 PM
well, the easiest solution would be to give one of them to me and buy yourself a different kind of display for the second monitor. it is almost Christmas, you know... :D

that being said, the desktop pictures implementation is a bit limited. if you have two displays with the same (hardwired) name, then you're reduced either to using a hack (being that I think the displays will always appear in the same order in the list of desktops) or reading from the system plist file that handles global configurations to get the (unique) ID code for the display and match it to the (non-unique) names. bit of a pain, that last...

Milko
12-11-2008, 01:30 PM
a very nice and cheap solution tw:D

If the order is always the same, do you have any idea to use a hack?

tw
12-12-2008, 06:07 PM
a very nice and cheap solution tw:D

If the order is always the same, do you have any idea to use a hack?

well, that is the hack. if you know, for instance, that the display you want is always listed second, then you can use the following to change its image:

tell application "System Events"
set theDesktops to every desktop whose display name is "Cinema HD"
set picture of item 2 of theDesktops to alias "path:to:picture"
end tell

Milko
12-12-2008, 06:53 PM
I tried the following code on my macbook at home and get the error message "Sytem Events got an error:invalid key form"

tell application "System Events"
set theDesktops to every desktop whose display name is "farb-LCD"
set picture of item 1 of theDesktops to alias "Data:Flow.jpg"
end tell


On mondy I will test it on my macpro with 2 displays and let you know if it works.

Have a nice weekend.

Milko
12-15-2008, 12:03 PM
I get at work on my macpro with 2 Cinema HD displays the same error message "Sytem Events got an error:invalid key form"

tell application "System Events"
set theDesktops to every desktop whose display name is "Cinema HD"
set picture of item 2 of theDesktops to alias "Data:Flow.jpg"
end tell


The first line shows "{desktop "69490793" of application "System Events", desktop "69490796" of application "System Events"}" and the second line the error message.

Have a nice day.

tw
12-17-2008, 07:34 PM
'invalid key form' usually means that it's getting something that it doesn't expect. it could be that you need to add parentheses around 'item 2 of theDesktops', or maybe you need to use 'file' or 'disk item' instead of 'alias'.

try set picture of (item 2 of theDesktops) to file "Data:Flow.jpg" for the third line.

Milko
12-18-2008, 11:08 AM
I have tried

set picture of (item 2 of theDesktops) to disk item "Data:Flow.jpg"
set picture of item 2 of theDesktops to disk item "Data:Flow.jpg"
set picture of (item 2 of theDesktops) to file "Data:Flow.jpg" and
set picture of item 2 of theDesktops to file "Data:Flow.jpg"

unfortunately the same error message.

Thank you for your Help. Happy Christmas!

tw
12-19-2008, 03:36 AM
sorry, it seems that system events is being persnickety. try this (a variation of which works on my machine)

tell application "System Events"
set theDesktops to a reference to every desktop whose display name is "Cinema HD"
set picture of item 1 of theDesktops to file "TMac:Library:Desktop Pictures:Lines Moss.jpg"
end tell


the 'reference to' bit seems to help...

Milko
12-22-2008, 03:46 PM
and........
it WORKS!!!

Thank you very mutch for your Help tw.

Skillet
06-16-2010, 11:17 AM
Thank you TW this is great and can be used with SwitchResX to solve my computer problems when used in a lab. I can now change my monitors used (we have three including a projector), and multiple engineers and we all like the monitors to move in different directions (up or to the right). When we use SwitchResX we can run and applescript and this will allow us to change our desktops so we can quickly tell who's settings are currently being used.

Anyway thanks much for your help and work around with two displays that have the same name I had that problem too.

tell application "System Events"
set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
set picture of item 1 of theDesktops to file "Mac Pro HD:Library:Desktop Pictures:Lines Moss.jpg"
end tell
tell application "System Events"
set theDesktops to a reference to (every desktop whose display name is "Cinema HD")
set picture of item 2 of theDesktops to file "Mac Pro HD:Library:Desktop Pictures:Lines Plum.jpg"
end tell

This works great!