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.

3 comments:

Anonymous said...

Here's how to have a combined script that either stores or reads to a property list. Enjoy! -- SAL

tell application "Safari"
activate
display dialog "Save the current URL or retrieve the stored URL?" buttons {"Cancel", "Save", "Retrieve"} default button 1
if the button returned of the result is "save" then
set write_URL to true
try
set this_URL to the URL of document 1
on error
display alert "MISSING DOCUMENT ERROR" message "No browser is open displaying a webpage." buttons {"Cancel"} default button 1 cancel button 1
end try
else
set write_URL to false
end if
end tell
tell application "Finder"
if not (exists folder "Safari" of (path to application support folder from user domain)) then
make new folder at (path to application support folder from user domain) with properties {name:"Safari"}
end if
end tell
tell application "System Events"
set the plistfile_path to (the POSIX path of (folder "Safari" of (application support folder of user domain))) & "/Stored URLs.plist"
if not (exists disk item plistfile_path) then
-- delete any existing property list from memory
delete every property list item
-- create an empty property list dictionary item
set the parent_dictionary to make new property list item with properties {kind:record}
-- create a new property list using the empty record
set this_plistfile to make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
else
set this_plistfile to property list file plistfile_path
end if
if write_URL is true then
make new property list item at end of property list items of contents of this_plistfile with properties {kind:string, name:"storedURL", value:this_URL}
else -- read the stored URL
tell this_plistfile
set this_URL to the value of property list item "storedURL"
end tell
end if
end tell
if write_URL is false then
tell application "Safari" to open location this_URL
end if

Allie said...

I didn't know that before. Thanks a lot for saving me a truckload of time! Now if only this can also help me with my guitar tabs I'd be perfectly happy.

Unknown said...

Hiện nay tại các văn phòng công ty doanh nghiệp luôn lựa chọn cho văn phòng mình những mẫu bàn làm việc ưng ý cho nên hiện nay trên thị trường có các kiểu ban van phong hoa phat được tìm kiếm xu hướng tăng hiện nay. Chính vì thế những mẫu bàn trên thị trường có khá nhiều mẫu mã với thiết kế đa dạng nhưng đều tạp trung vào các tiêu chí chuẩn của văn phòng đặc biệt là tuân theo phong thủy văn phòng làm việc của nhân viên. Để chọn lựa mua bàn làm việc trên thị trường nội thất bạn cần nắm vững những kiến thức và nhu cầu như thế nào cho văn phòng làm việc của mình.
Vấn đề đầu tiên nhiều chủ doanh nghiệp băn khoăn về giá?
Mua hàng ở đâu giá rẻ, giá tốt mà sản phẩm lại chất lượng… những câu hỏi này đều xoay quanh chi phí đầu tư. Tôi sẽ gợi ý cho bạn rằng: cốt lõi nhất nằm ở việc, bạn xác định phạm vi chi phí dựa trên mục đích sử dụng ra sao!
Nếu bạn đầu tư bàn làm việc dành cho không gian văn phòng lãnh đạo, nơi bạn sẽ thường xuyên phải tiếp đối tác, để chứng minh cũng như tạo niềm tin tuyệt đối với khách hàng, bạn nên cân nhắc đầu tư mức chi phí cao hơn cho một chiếc bàn làm việc hay cả hệ thống bàn sang trọng, chuyên nghiệp với thiết kế tinh tế đẹp mắt hơn.
Còn nếu bạn đầu tư bàn làm việc giá rẻ hcm dành cho không gian làm việc của nhân viên, nơi phục vụ lợi ích tối đa cho công việc của họ, thì lúc này bạn cần những sản phẩm đơn giản, tiện dụng và có khả năng đồng bộ hóa cao, tạo ra sự chuyên nghiệp. Với mục đích như vậy, rõ ràng, sản phẩm bạn chọn mua sẽ có chi phí thấp hơn nhiều. Dưới đây tôi sẽ gợi ý cho bạn vài lựa chọn.