Friday, January 18, 2013

Changing OSX Wallpapers on Multiple Monitors via Script

[UPDATED 05/18/2014: Fixed script for Mavericks compatibility!]

For whatever reason, I like to use different desktop wallpapers during the day than I do during the night. I feel like it helps my eyes focus better on the open window I am working in (and I hate working in  maximized/fullscreen apps).

The problem is that I use a 4 monitor setup, and setting each desktop's wallpaper manually twice a day is so ... manual!  So I developed a script to help me make those changes automatically. The special thing about this script it that it iterates over desktops and changes the wallpaper. This was actually kind of tricky to find information about. Most information I could find only really applied to single monitor setups, and while they changed my wallpaper on the main monitor, did nothing for the other monitors. After some research and tweaking I finally came up with this, which seems to work well:

tell application "Finder"
 
 set path_to_file to "/Users/YourAccountName/Pictures/your-image.jpg"
 
 tell application "System Events"
  set theDesktops to a reference to every desktop
  repeat with aDesktop in theDesktops
   set picture of aDesktop to path_to_file
  end repeat
 end tell
 
end tell

Obviously (at least, hopefully so) you would modify the path_to_file assignment to an image that is interesting to you ... or you could handle an argument to the script from here, or query for a file, etc. etc.

The way I have this setup for my situation is that I have two copies of this script with different names and different images: One for night and one for day. This makes it easy to run them from a scheduler, or more typically, I call them from Alfred when I desire a change.

Anyways, hope you find that useful. Have fun!

Extract Emails from Contacts.app

I recently signed up for Mailroute.net and wanted to configure a whitelist of email addresses for all my regular, and less-than regular, contacts. My email addresses are stored in OSX's Contacts.app, and visiting each contact record and cut-n-pasting email addresses out would be a ridiculously tedious effort. Luckily, being a programmer, I see this as an opportunity to develop a program to do the work for me!

Without further ado, here is a short Applescript to extract all email addresses from Contacts.app on Mac OSX 10.8+ and dump them to a new TextEdit file. It could be converted to earlier OSX versions without much difficulty probably, since I think the only difference is the Contact Manager app used to be called "Address Book" instead of "Contacts".

Here's the code:
set unique_emails to {}

tell application "Contacts"
 repeat with this_person in people
  repeat with this_email in every email of this_person
   copy the value of this_email & "
" to the end of unique_emails
  end repeat
  
 end repeat
 
end tell

tell application "TextEdit"
 activate
 make new document
 set theDate to current date
 set text of document 1 to unique_emails as text
end tell

One thing to note is that the copy the value of ... line literally includes a newline within the quotes. This causes an actual newline to be appended to every email address. If you would rather the email addresses be comma separated, you would use a comma here instead.

Have fun!