Skip to content

Instantly share code, notes, and snippets.

@peterc
Created January 26, 2011 04:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterc/796221 to your computer and use it in GitHub Desktop.
Save peterc/796221 to your computer and use it in GitHub Desktop.
Basic Ruby code to read and write ".webloc" files (as used on OS X)
# Basic Ruby code to read and write ".webloc" files (as used on OS X)
# Reads both "old" Safari style and newer plist style
# Writes "old" style only
# Works on Ruby 1.9.2 and 1.8.7
require 'plist'
class Webloc
attr_accessor :url
def initialize(url)
@url = url
end
def self.load(filename)
data = File.read(filename)
data = data.force_encoding('binary') rescue data
if data !~ /\<plist/
offset = (data =~ /SURL_/)
length = data[offset + 6]
length = length.ord rescue length
url = data[offset + 7,length]
else
url = Plist::parse_xml(filename)['URL'] rescue nil
end
raise ArgumentError unless url
new(url)
end
def data
@data = "\x62\x70\x6C\x69\x73\x74\x30\x30\xD1\x01\x02\x53\x55\x52\x4C\x5F\x10"
@data += @url.length.chr
@data += @url
@data += "\x08\x0B\x0F\x00\x00\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
@data += (@url.length + 18).chr
end
def save(filename)
File.open(filename, 'w:binary') { |f| f.print data }
end
end
# To READ a webloc file:
#puts Webloc.load(ARGV.first).url
# To CREATE a webloc file:
#Webloc.new('http://peterc.org/').save('peterc.webloc')
@millie
Copy link

millie commented Nov 16, 2017

Thank you for this!

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