Save Safari tabs to Instapaper

InstaPaper iconI posted a way to save your Safari tabs to Evernote, which I’ve found is generally a great way to save bookmarks. It syncs automatically to your other computers and your iPhone, and it’s fast and easy. If you really want to highlight a few tabs to make sure you get back to them, you might consider this script, though. Once you’ve saved your entire Safari browsing session for later, close everything except for those special urls, run the code below as a script, and your open tabs will be saved as entries in your InstaPaper account.

There are two “property” lines at the top of the script; edit them to set your Instapaper username and password (if you have one, otherwise, set it to ""). The next section handles everything, iterating through each tab, grabbing its title and url and building a shell command to do a simple curl call to the InstaPaper API.

After that, the rest of the script is a routine for url encoding that I nicked here. It’s called when setting both the _title and _url variables to make the curl call from the shell work. I haven’t tested this extensively yet, but it’s worked for everything I’ve tried. A title with odd characters in it could potentially cause problems. You can always add a shell-escaping routine…

property _user : "yourusername"
property _pass : "yourpassword"

tell application "Safari"
	repeat with _tab in tabs of front window
		set _title to my urlencode(name of _tab)
		set _url to my urlencode(URL of _tab)
		set _script to (
			"curl '"
			& _user & "&password=" & _pass & "&url=" & _url & "&title=" & _title & "'")
		set output to do shell script _script
	end repeat
end tell

on urlencode(theText) --
	set theTextEnc to ""
	repeat with eachChar in characters of theText
		set useChar to eachChar
		set eachCharNum to ASCII number of eachChar
		if eachCharNum = 32 then
			set useChar to "+"
		else if (eachCharNum  42) and (eachCharNum  95) and (eachCharNum < 45 or eachCharNum > 46) and (eachCharNum < 48 or eachCharNum > 57) and (eachCharNum < 65 or eachCharNum > 90) and (eachCharNum < 97 or eachCharNum > 122) then
			set firstDig to round (eachCharNum / 16) rounding down
			set secondDig to eachCharNum mod 16
			if firstDig > 9 then
				set aNum to firstDig + 55
				set firstDig to ASCII character aNum
			end if
			if secondDig > 9 then
				set aNum to secondDig + 55
				set secondDig to ASCII character aNum
			end if
			set numHex to ("%" & (firstDig as string) & (secondDig as string)) as string
			set useChar to numHex
		end if
		set theTextEnc to theTextEnc & useChar as string
	end repeat
	return theTextEnc
end urlencode
Brett Terpstra

Brett is a writer and developer living in Minnesota, USA. You can follow him as ttscoff on Twitter, GitHub, and Mastodon. Keep up with this blog by subscribing in your favorite news reader.

This content is supported by readers like you.

Join the conversation