Created
February 22, 2012 03:42
-
-
Save chsh/1881150 to your computer and use it in GitHub Desktop.
Set spotlight searchable content scraped from web to FinderComment for webloc
This file contains 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 'tempfile' | |
require 'open-uri' | |
require 'rubygems' | |
require 'sanitize' | |
require 'nokogiri' | |
webloc = ARGV[0] | |
tf = Tempfile.new('webloc-spotlight') | |
tf.write(File.read(webloc)) | |
tf.close | |
system 'plutil', '-convert', 'xml1', tf.path | |
f = tf.open | |
doc = Nokogiri::XML(f) | |
tf.close(true) | |
url = doc.xpath('/plist/dict/string').text | |
html = open(url).read | |
sanitized_html = Sanitize.clean html, :remove_contents => %w(script style) | |
tf = Tempfile.new('webloc-spotlight') | |
tf.write(<<EOL) | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<string>#{sanitized_html}</string> | |
</plist> | |
EOL | |
tf.close | |
system 'plutil', '-convert', 'binary1', tf.path | |
attr = `xxd -ps #{tf.path}` | |
tf.close(true) | |
exec 'xattr', '-wx', 'com.apple.metadata:kMDItemFinderComment', attr, webloc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
worked on Mac OS only. ;-)