Skip to content

Instantly share code, notes, and snippets.

@justinjones
Created May 31, 2012 02:26
Show Gist options
  • Save justinjones/2840544 to your computer and use it in GitHub Desktop.
Save justinjones/2840544 to your computer and use it in GitHub Desktop.
AppleScript for setting your desktop to an image from Desktoppr
-- Usage: osascript /path/to/desktop-switcher.scpt
-- Requires: "JSON Helper" from the mac app store (free)
-- Downloads a random wallpaper from desktoppr.co and sets it to your desktop wallpaper
-- Use at your own risk.
-- creates a wallpapers folder in downloads directory and delete anything in it (from previous runs)
set wallpapersFolder to POSIX path of (path to downloads folder as string) & "wallpapers"
do shell script "mkdir -p " & wallpapersFolder
do shell script "rm -f " & wallpapersFolder & "/*.wallpaper"
-- generate a not-very-random string for the filename because OSX wont reset
-- the desktop wallpaper to the same file as currently shown even if it has changed
set filename to ""
repeat with x from 1 to 20
set randomChar to ASCII character (random number from 97 to 122)
set filename to filename & randomChar
end repeat
-- requires JSON Helper, can be found on the mac app store for free
tell application "JSON Helper"
-- fetch the JSON
set wallpaperJson to fetch JSON from "https://api.desktoppr.co/1/wallpapers/random"
-- pull out the URL
set wallpaperUrl to |url| of |image| of |response| of wallpaperJson
-- get a reference to our file
set wallpaperFile to wallpapersFolder & "/" & filename & ".wallpaper"
-- download the file wiht curl
do shell script "curl -L " & wallpaperUrl & " -o " & wallpaperFile
-- set the desktop wallpaper to the downloaded file
tell application "Finder"
set desktop picture to POSIX file wallpaperFile
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment