Skip to content

Instantly share code, notes, and snippets.

@marzocchi
Created September 27, 2011 21:47
Show Gist options
  • Save marzocchi/1246350 to your computer and use it in GitHub Desktop.
Save marzocchi/1246350 to your computer and use it in GitHub Desktop.
Save and restore Finder's desktop icons position
#!/usr/local/bin/macruby
# Damn evil Finder will insist crashing and reshuffling
# icons in my carefully laid out desktop.
require 'rubygems'
require 'thor'
require 'yaml'
framework "ScriptingBridge"
class DesktopIcons < Thor
desc "save", "saves desktop icons position"
def save
positions = Hash[finder.desktop.items.map do |i|
pos = i.desktopPosition
[ i.name, [pos.x, pos.y] ]
end]
File.open(file, 'w') do |f|
f.write YAML::dump(positions)
end
end
desc "restore", "restores desktop icons to previous position"
def restore
unless File.exists? file
say "#{file} not found.", :red
exit 1
end
data = YAML::load(File.new file)
finder.desktop.items.
select { |i| data[i.name] }.
each { |i| i.desktopPosition = data[i.name] }
puts "Done restoring icons."
end
no_tasks do
def file
ENV['HOME'] + '/.desktop-icons.yml'
end
def finder
@finder ||= SBApplication.applicationWithBundleIdentifier("com.apple.Finder")
end
end
end
DesktopIcons.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment