public
Last active

Switch between two desktop images on OS X e.g. a standard image and a texture for use behind transparent windows like Terminal or MacVim

  • Download Gist
toggle_desktop_image.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#!/usr/bin/env ruby
 
STD_IMG_REF = '/Users/paulcarey/.std_desktop_image.txt'
IMG_SRC = '/Users/paulcarey/dev/images/ir_black_bg.png'
 
def current_img
cmd = "defaults read com.apple.Desktop Background | grep ImageFilePath | tail -n 2 | head -n 1"
res = `#{cmd}`
res = res.split('=')[1].strip
res[1, res.size - 3]
end
 
def save_current_img
File.open(STD_IMG_REF, 'w+') { |f| f.puts current_img }
end
 
def load_prev_img
File.open(STD_IMG_REF, 'r') { |f| f.gets.strip }
end
 
def set_img src
script = %Q<tell application "Finder"
set desktop picture to POSIX file "#{src}"
end tell>
IO.popen('osascript -', 'w') { |p| p.puts script }
end
 
if current_img == IMG_SRC
set_img load_prev_img
else
save_current_img
set_img IMG_SRC
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.