Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/8875201 to your computer and use it in GitHub Desktop.
Save henrik/8875201 to your computer and use it in GitHub Desktop.
An awful hack (not cleaned up) to programmatically rename events (adding number prefixes to follow a certain schema) in iPhoto even though it doesn't seem to support that via AppleScript. Exploiting the convenient keyboard shortcuts.
NUMBER = DATA.readlines.inject({}) { |h, line|
number, name = line.chop.split(". ", 2)
h[number] = name
h
}
def osascript(cmd)
system "osascript", "-e", cmd
end
def assign(number, name)
`echo | pbcopy` # clear out clipboard in case the title is empty and the cmd+c fails
osascript %{tell app "System Events" to keystroke "c" using command down} # copy
actual_name = `pbpaste`
name_was_empty = actual_name.to_s.chop.empty? # names generated from dates may show as empty when you try to edit them
if (actual_name == name) || name_was_empty
puts "go on"
osascript %{tell app "System Events" to keystroke (ASCII character 28)} # left
osascript %{tell app "System Events" to keystroke "#{number}. "}
osascript %{tell app "System Events" to keystroke #{name.inspect}} if name_was_empty
osascript %{tell app "System Events" to keystroke tab}
else
puts "Got: #{actual_name.inspect} but expected #{name.inspect} (number: #{number})"
osascript %{tell app "iPhoto" to display dialog "Not the name I expected. Bailing!"}
exit 1
end
end
osascript %{tell app "iPhoto" to activate}
osascript %{tell app "iPhoto" to display dialog "Click the '#{NUMBER.values.first}' event title and wait…"}
sleep 3
NUMBER.each do |number, name|
assign(number, name)
end
__END__
62. Fashion St. Louis
55. 29 apr 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment