setting desktop picture depending on time of day.

hi all, i was wondering if say i have 3 desktops, from 6:00 until 11:00 i want a certain picture as my desktop and then from 11:01 until 5:00 i want another and then from 5:01 until 5:59 the next day i wan a different one. how would i go about doing this?

EDIT: oh a quick question, is it possible to write a prefpane in applescript using xcode? because i dont want this application to have to be open all the time.

Hi Hendo,

either a stay open script or a crontab entry or a launchd agent, which triggers a script every hour to change the desktop if necessary

Just in case you need help setting/changing your desktop picture, here’s a script I made which might help you. Basically this script inverts your screen colors. This isn’t what you want but it does include a part about changing your desktop picture which should help you. In this script I change to desktop picture to white so that when I invert the screen colors the desktop is black.

Anyway, I change the desktop picture by writing to a preference file, and then to make the desktop picture change to the new setting I invert the screen. So in your case you can set the preference file to a certain desktop picture, and then invert the screen colors twice (so you get back to normal screen colors) to get the new desktop picture to take effect.

Hope that helps.

nice one regulus! I’ll definitely take a look! :smiley:

  • Hendo

One mistake Hendo, after a quick check I found you don’t need to “invert screen colors” for the desktop picture change to take effect. Just change the preference file. Here’s a script to do it. Note that this is set up for my situation where I have 2 monitors attached to my computer. If you have only one monitor the just set the variable “desktopPics” to contain only one item in its list (obviously for more monitors make the list contain the same number of “paths to pictures” as you have monitors).

Notice the path to the pictures is a posix path, and also note that spaces in the path does not matter! Use that form!

-- Change desktop pictures for multiple displays
-- make sure the variable "desktopPics" contains the same number of items as the number of displays you have attached to your computer

set prefFile to (path to preferences folder from user domain as Unicode text) & "com.apple.desktop.plist"
set desktopPic_display1 to "/Library/Desktop Pictures/Classic Aqua Blue.jpg"
set desktopPic_display2 to "/Library/Desktop Pictures/Jaguar Aqua Blue.jpg"
set desktopPics to {desktopPic_display1, desktopPic_display2}

-- first get the display ids for the displays
set displayIDs to my get_displayIDs()

-- set the preference file
tell application "System Events"
	repeat with i from 1 to (count of displayIDs)
		set value of property list item "ImageFilePath" of property list item (item i of displayIDs) of property list item "Background" of property list file prefFile to (item i of desktopPics)
	end repeat
end tell

(*=================== SUBROUTINES ======================*)
on get_displayIDs()
	try
		set plistFolderPath to (path to preferences folder from user domain as Unicode text) & "ByHost:"
		tell application "Finder" to set plistName to (name of files of folder plistFolderPath whose name contains "com.apple.windowserver") as Unicode text
		set plistPath to plistFolderPath & plistName
		tell application "Image Events" to set count_of_displays to count of displays
		set j to 1
		repeat
			set displayIDs to {}
			tell application "System Events" to set displaySet to value of property list item j of property list item "DisplaySets" of property list file plistPath
			try
				repeat with i from 1 to count_of_displays
					set expID to |DisplayID| of (item i of displaySet)
					set stringID to my number_to_text(expID)
					set end of displayIDs to stringID
				end repeat
				exit repeat
			on error
				set j to j + 1
			end try
		end repeat
		return displayIDs
	on error
		set frontApp to displayed name of (info for (path to frontmost application))
		tell application frontApp to display alert "Error. A needed file is missing!" message "Please make some changes to the settings in the \"Displays\" preference pane because this action will create the missing file." & return & return & "Note: you only have to do this once. You can make any change in the preference pane (which will create the needed file) and then you can change the setting right back if you wish." as warning
		return false
	end try
end get_displayIDs

on number_to_text(this_number)
	set this_number to this_number as text
	if this_number contains "E+" then
		set x to the offset of "." in this_number
		set y to the offset of "+" in this_number
		set z to the offset of "E" in this_number
		set the decimal_adjust to characters (y - (length of this_number)) thru ¬
			-1 of this_number as Unicode text as number
		if x is not 0 then
			set the first_part to characters 1 thru (x - 1) of this_number as Unicode text
		else
			set the first_part to ""
		end if
		set the second_part to characters (x + 1) thru (z - 1) of this_number as Unicode text
		set the converted_number to the first_part
		set decimal_added to false
		repeat with i from 1 to the (length of second_part)
			try
				set the converted_number to ¬
					the converted_number & character i of the second_part
			on error
				set the converted_number to the converted_number & "0"
			end try
			if decimal_added is false and (i mod decimal_adjust) is 0 and (i is not (length of second_part)) then
				set the converted_number to the converted_number & "."
				set decimal_added to true
			end if
		end repeat
		return the converted_number
	else
		return this_number
	end if
end number_to_text

Hi regulus,

For one display this is sufficient

tell application "Finder" to set desktop picture to ((path to desktop pictures folder as Unicode text) & "Aqua Blue.jpg")

That’s funny, StefanK. One line! Sometimes I wish I only had one monitor! Actually my script is built from one simple command too, the rest of it is dealing with getting the display ID of each monitor so you can deal with each individually. It’s ashame applescript doesn’t have a hook to work with multiple displays… at least not that I know of.

BTW, here’s the one command from my script…

tell application "System Events"
   set value of property list item "ImageFilePath" of property list items of property list item "Background" of property list file prefFile to (item i of desktopPics)
end tell

That will do :stuck_out_tongue:

Thanks anyway Regulus. your time is appreciated!