Skip to content

Instantly share code, notes, and snippets.

@ktheory
Last active July 24, 2017 12:51
Show Gist options
  • Save ktheory/6833946 to your computer and use it in GitHub Desktop.
Save ktheory/6833946 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
##
# Restore simplenote backup
#
# If you want to restore your Simplenote for Mac database, you can use this script.
# I created it for this issue:
# http://help.simplenote.com/customer/en/portal/questions/2773965-most-of-my-notes-were-deleted?new=2773965
#
# First, find a good backup of your simplenote database stored in
# ~/Library/Containers/com.automattic.SimplenoteMac/Data/Library/Simplenote/Simplenote.storedata
#
# In simplenote, manually delete all your notes (cmd+a, delete)
#
# Then run this script:
# $ cat path_to_simplenote.storedata | ruby simplenote_restore.rb
#
# ...and that's it.
# NB: this script doesn't restore tags, modification times, or anything but the content.
# That's all I cared about.
require 'rubygems'
begin
require 'nokogiri'
rescue
puts "Error: Please install nokogiri: sudo gem install nokogiri"
exit 1
end
simplenote_raw_data = STDIN.read
# Applescript to create a new note in Simplenote, and paste clipboard
applescript = %{
tell application "Simplenote" to activate
tell application "System Events"
keystroke "n" using {command down}
keystroke "v" using {command down}
end tell
}.strip
xml = Nokogiri::XML(simplenote_raw_data)
xml.css("object[type=NOTE]").each do |note|
# Skip notes in trash
next unless note.css('attribute[name=deleted]').text == '0'
content = note.css('attribute[name=content]').text
title = content.split("\n").first
puts title
# Copy note content to clipboard
IO.popen('pbcopy', 'w') {|f| f << content }
# Run applescript
IO.popen('osascript', 'w') {|f| f << applescript }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment