Friday, February 29, 2008

How To: Re-open Closed Tabs in Camino & Safari with AppleScript

Rather than update my previous entry on this topic I decided to write a new one since much has changed.

First I'd like to acknowledge Lisa Thompson for her AppleScript expertise, and code contributions. I have adjusted my scripts to use some of her ideas, with permission. As well, Lisa has some excellent AppleScripts for Camino, including her own rendition of the scripts you'll find here. While I lean more toward keyboard shortcuts and apps like FastScripts, Lisa looks to use a new feature in Camino 1.6 - that of adding scripts to the toolbar menu. (note that my scripts can also be used in the toolbar, but mine also tend to have a bit more stuff - relating to keeping certain windows open, etc.).

If you are a user of FastScripts, iKey, Keyboard Maestro, etc., and you like to launch scripts with keyboard shortcuts, you'll find these scripts handy.

Objective: To give Camino and Safari the functionality of automatically saving the current URL of a tab, writing that URL to file for later retrieval, if desired. If the tab was closed accidentally, using the second script, one can reopen that URL in a new tab.
(
Note: Saft already provides this functionality in Safari; Saft costs $12. In addition to reopening closed tabs, Saft also retains the history of said tab. These scripts do not retain the history of the closed tab, merely the URL of the tab at the time it was closed. To my knowledge, nothing like this exists as a plug-in on Camino.)

(Note: The "Save|CloseTab" and "Save|Prevent|CloseTab" scripts save ONE URL at a time. So if you want to go back to a tab you closed earlier, but have open/closed tabs since, these will not work - yet. I am looking at the possibilities of implementing that down the road.)

Background: Why did I do this? As a heavy-duty user of Firefox, I rely heavily on the Tab Mix Plus plug-in which retains tabs (with history), freezes tabs, and much more. But Firefox can get bogged down over time, or sometimes I just want to change browsers for a while. Whenever I switched to Camino for a breather I was amazed at the speed difference, yet discouraged at the lack of Firefox-like plug-ins (specifically Tab Mix Plus - hereafter "TM+").

So a few weeks back I decided to write an AppleScript a solution. I have found it so useful that I thought to share it with others. Perhaps these will be of help to you as well.

(Note: Very important to understand that these scripts do not help if you use the close button located on the actual tab, or if you take your mouse up to "File > Close Tab" or "File > Close Window" - those actions circumvent the AppleScripts).

Usage: When I'm browsing and close a tab (using Command W) that I wish to reopen, I simply hit Command F12 to reopen it. Why Command F12? Simply because it's the default key combo used in TM+. You can use something else if you prefer.

The CloseTab scripts are editable, once open, you'll see my notes as to what/where.

BOTH Save|CloseTab scripts will create a file in the root of the Sites folder. Camino's file is "UndoCloseTab-CAM.txt", Safari's is "UndoCloseTab-SAF.txt". Those text files will open/close automatically when called - you won't see anything, it's all in the background.

HOW-TO:

1) Put the AppleScripts in the folder of your choosing (For FastScripts, use the FS menu, Open Scripts Folder. Inside Applications, if not already there, create a "Camino" and/or "Safari" folder and place the scripts in the appropriate folder(s).
2) Open FastScripts (or your app of choice) and assign the key combo of "Command W" to the "CAM•Save|CloseTab" or "SAF•Save|CloseTab" script (or to the "Save|Prevent|CloseTab" scripts).

(Note: You'll want to assign keystrokes to a specific application. FastScripts, QuicKeys X, Keyboard Maestro, iKey can all do this. Make sure you're not assigning "Command W" as a global command, otherwise that combo will break in other apps. So just assign it to Camino or Safari.)

3) While in FastScripts, assign Command F12 (or other combo) to the "CAM•UndoCloseTab" or "SAF•UndoCloseTab" script.

There are more specific editing instructions in the CAM/SAF UndoCloseTab scripts.

Camino Scripts: (these scripts are for Camino 1.6)

1) "CAM•Save|CloseTab" (only saves/closes tab, does not prevent particular tabs from closing)

2) "CAM•Save|Prevent|CloseTab" (this saves tab URL, looks for page title of user-specified page and prevents closure of said tab, and also prevents last tab from closing)

3) "CAM•UndoCloseTab" (does what the name says)

Safari Scripts:

1) "SAF•Save|CloseTab" (only saves/closes tab, does not prevent particular tabs from closing)

2) "SAF•Save|Prevent|CloseTab" (this saves tab URL, looks for page title of user-specified page and prevents closure of said tab, and also prevents last tab from closing)

3) "SAF•UndoCloseTab" (does what the name says)

That's it. Enjoy browsing with a little safety net below ya. :-)

If you're an AppleScript guru and have suggestions, I'm all ears, so please drop a note in the comments.

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.