Posterous theme by Cory Watilo

Using Quicksilver and Applescript to Keep a Stack of Safari URLs

My friend posted this question on Hacker News. I answered:

If you're on a Mac and you're using Safari you might like Urly (http://www.zenonez.com/urly/news.html). It lets you make a pretty basic stack of browser links. If you don't like that (and you're a Quicksilver user), it might be worth writing some Quicksilver triggers to push and pop urls using a text file as a stack.

I decided I might as well code my solution. I'd never written any Applescript before, so what have is cobbled together from various examples I found online and very likely not the best way to do it.  I made two scripts.
 
bpush.scpt:

tell application "Safari" 
 
set theurl to URL of front document 
end tell 
 
set clipFil to (path to documents folder as text) & "tobrowse.txt" 
try 
close access file clipFil 
end try 
set filRef to open for access file clipFil with write permission 
write (return & (theurl)) to filRef starting at eof 
close access filRef 
tell application "Safari" to activate

bpop:scpt:
set clipFil to (path to documents folder as text) & "tobrowse.txt" 
 
try 
close access file clipFil 
end try 
 
set filRef to open for access file clipFil with write permission 
set filetext to (read filRef) 
set thelist to every paragraph of filetext 
set theurl to item (length of thelist) of thelist 
set item (length of thelist) of thelist to "" 
set newtext to (thelist) as text 
set eof of filRef to 0 
write newtext to filRef 
close access filRef 
 
tell application "Safari" 
activate 
set URL of document 1 to theurl 
end tell

I can activate these by just typing bpush and bpop into Quicksilver. I've tried using this while browsing for the past hour or so and it's definitely too soon to say for sure, but I think I might actually use these.