Last active
February 20, 2025 10:02
-
-
Save ttscoff/4453362 to your computer and use it in GitHub Desktop.
Mass generate TextExpander snippets from a CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) | |
expansion = CGI.escapeHTML(item[1].strip) | |
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