Skip to content

Instantly share code, notes, and snippets.

@chsh
Created February 22, 2012 03:42
Show Gist options
  • Save chsh/1881150 to your computer and use it in GitHub Desktop.
Save chsh/1881150 to your computer and use it in GitHub Desktop.
Set spotlight searchable content scraped from web to FinderComment for webloc
#!/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
@chsh
Copy link
Author

chsh commented Feb 22, 2012

worked on Mac OS only. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment