Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OS X StickieDatabase extractor to separate RTF files
#!/usr/bin/env ruby -wKU
$stickieIndex = 0
def uniq_index
i = $stickieIndex
$stickieIndex += 1
i
end
class Stickie
def initialize(file, offset=0)
@index = uniq_index
parse(file, offset)
end
def parse(file, offset)
@contents = ""
curOffset = offset
curBrackets = 0
loop do
@contents << file[curOffset]
if file[curOffset-1] != '\\'
if file[curOffset] == '{'
curBrackets += 1
elsif file[curOffset] == '}'
curBrackets -= 1
break if curBrackets == 0
end
end
curOffset += 1
end
end
def write(dir)
filename = "stickie#{@index}.rtf"
path = File.join(dir, filename)
IO.binwrite(path, @contents)
filename
end
def length
@contents.length
end
end
if ARGV.count != 2
puts "Usage: #{File.basename(__FILE__)} StickiesDatabase OutputDirectory"
exit
end
databaseFile = ARGV[0]
outputDirectory = ARGV[1]
if !File.exist?(databaseFile)
puts "#{databaseFile} does not exist."
exit
end
if !Dir.exist?(outputDirectory)
puts "#{outputDirectory} does not exist."
exit
end
database = IO.binread(databaseFile)
currentIndex = 0
loop do
currentIndex = database.index('{\\rtf1', currentIndex)
break if !currentIndex
stickie = Stickie.new(database, currentIndex)
currentIndex += stickie.length
puts "Wrote stickie of length #{stickie.length} to #{stickie.write(outputDirectory)}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.