Skip to content

Instantly share code, notes, and snippets.

@joech4n
Forked from ttscoff/textexpander_gen.rb
Last active December 22, 2015 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joech4n/0adb10c8067dbf7ebcfa to your computer and use it in GitHub Desktop.
Save joech4n/0adb10c8067dbf7ebcfa to your computer and use it in GitHub Desktop.
Background: http://brettterpstra.com/2013/01/04/mass-creating-textexpander-snippets/ Forked from Brett's script to handle exported .csv from aText
#!/usr/bin/ruby
require 'csv'
require 'erb'
require 'cgi'
if ARGV.length == 2
input_csv = ARGV[0]
output_te = ARGV[1]
unless output_te =~ /\.textexpander$/
puts "Second argument must have a '.textexpander' extension."
puts "#{File.basename(__FILE__)} input.csv output[.textexpander]"
exit
end
else
puts "Two arguments are required, input filename and output filename"
puts "#{ARGV[0]} input.csv output[.textexpander]"
exit
end
template = ERB.new <<-ENDTEMPLATE
<dict>
<key>abbreviation</key>
<string><%= abbr %></string>
<key>abbreviationMode</key>
<integer>0</integer>
<key>creationDate</key>
<date>2013-01-03T20:17:10Z</date>
<key>flags</key>
<integer>0</integer>
<key>label</key>
<string></string>
<key>modificationDate</key>
<date>2013-01-03T20:19:04Z</date>
<key>plainText</key>
<string><%= expansion %></string>
<key>snippetType</key>
<integer>0</integer>
<key>useCount</key>
<integer>0</integer>
<key>uuidString</key>
<string><%= uuid %></string>
</dict>
ENDTEMPLATE
header =<<-ENDHEADER
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>groupInfo</key>
<dict>
<key>expandAfterMode</key>
<integer>2</integer>
<key>groupName</key>
<string>#{File.basename(output_te).gsub(/\.textexpander$/,'')}</string>
</dict>
<key>snippetsTE2</key>
<array>
ENDHEADER
footer =<<-ENDFOOTER
</array>
</dict>
</plist>
ENDFOOTER
fh = File.new(File.expand_path(output_te),'w+')
input = CSV.read(File.expand_path(input_csv))
fh.puts header
input.each { |item|
abbr = CGI.escapeHTML(item[0].strip)
# modified to convert atext snippets
expansion = CGI.escapeHTML(item[1].strip.gsub(/【|】/,'%').gsub(/%clipboard%/,'%clipboard').gsub(/%field:[\s\S]*%/s,'%fill:input%'))
uuid = %x{uuidgen}.strip
$stderr.puts "Processed #{abbr} -> #{expansion}"
fh.puts template.result(binding)
}
fh.puts footer
fh.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment