Monday, February 18, 2008

Camino, Safari - How-To: Reopen Closed Tabs (with AppleScript)

Firefox has been my browser of choice for many reasons. First up on the list would be add-ons. I run several extensions, but my favorite - by far - is Tab Mix Plus. It allows you to freeze a tab preventing it's accidental closure. If you happen to close a tab and want to reopen it, you can - complete with the tabs history in tact.

But Firefox has its own quibbles. Tab Mix Plus can slow things down a little GUI-wise. At least that's been my perception. And Firefox is not exactly Mac like. Firefox 3 is looking great; however, AppleScript is not working as well in 3 - and if you know a fix for getting the URL of an existing page, I'd be most appreciative.

This used to work:

tell application "Firefox"
set Link to «class curl» of front window
set theTitle to «class pTit» of front window
end tell

Neither the «class curl» or «class pTit» work in FF3.

So on to Camino - it is awesome, faster than Firefox, but is not expandable with add-ons in the same manner that Firefox is. Tab Mix Plus is sadly missing.

So I put my brain to work and came up with a partial solution. I say partial because I cannot restore the history of a reopened tab, and these scripts can only restore the immediate last closed tab, not the last several tabs as in TMPlus.

These scripts work with Camino 1.5, but will need modification to work with v. 1.6 - which I do not have installed. Will update when 1.6 is out of beta.

I use the scripts in conjunction with FastScripts assigning the script the CMD-W keystroke overriding Camino's cmd-w. So when you use "cmd-w" to close a window, the script runs, copies the URL of the current tab to the clipboard. If you want to reopen the closed tab, hit "cmd-F12" or whatever key combo you assign and presto, it reopens (NOTE: if you have copied something to the clipboard AFTER you closed the tab, this won't work - this solution is for an "oops, I didn't mean to close that window" type of scenario! Also of note, this will NOT work if you use the close tab button.).

Here's the CMD-W script - I call this script "CAM-CloseTab(CmdW)", assigned keystroke "Cmd W" in FastScripts:

------- Script cut/paste below into new script ---------
tell application "Camino"
activate
set Remember to URL of window 1 (* Camino 1.5 *)
set the clipboard to Remember
(* BELOW - is if you have a site(s) you want to prevent from closing - replace XYZ with the TITLE or part of the TITLE of the page. Thus when you click Cmd-W, this site will not close *)
if name of front window contains "XYZ" or name of front window contains "XYZ" then
tell application "Camino" to activate
else
my closeTab()
end if
end tell

(* Tab Handler *)
on closeTab()
tell application "System Events"
tell process "Camino"
click menu item "Close Tab" of menu "File" of menu bar item "File" of menu bar 1
end tell
end tell
end closeTab
-----End Script----

Here's the Reopen closed tab script - I call it "UNDO Close Tab", assigned keystroke "cmd-F12" in FastScripts:

tell application "Camino"
set Remember to the clipboard
open url Remember (* Camino 1.5 *)
end tell

It is worth noting that some of this syntax will change with Camino 1.6.

This can be done with Safari as well, here's the scripts:

Assigned in FastScripts as "Command W":

tell application "Safari"
activate
set Remember to URL of document 1
set the clipboard to Remember
if name of window 1 contains "XYZ" or name of window 1 contains "XYZ" then
activate
else
close current tab of window 1
end if
end tell

Reopen closed tab (assigned in FastScripts as cmd-F12 keys):

tell application "Safari"
set Remember to the clipboard
open location Remember
end tell

If you see something that could use tweaking or such, drop me a note in the comments.

4 comments:

Unknown said...

great script, it works well. your post is from feb 2008, is this still the easiest way to do this?

Unknown said...

upon tinkering around, i realized that if you try to close a blank tab, an error pops up. any way to solve this?

Peter in Japan said...

I have a good solution I think. I am running Quickeys and I use this option. To keep my precious command W closing, I used Quickeys to execute an applescript for the "remember and close" script, and I can re-open it with the command bar top. This is a pretty major thing to have to hack -- if Camino is still serious I hope they address this lack in the future. But for now, this seems to work okay.

Anonymous said...

This script works brilliantly! Great work!