|
(* |
|
|
|
File: |
|
Rename Active Document.scpt |
|
|
|
Abstract: |
|
This script will intelligently rename the active document in BBEdit. |
|
|
|
Version: |
|
1.0 |
|
|
|
Author: |
|
Ben Spaulding <http://benspaulding.us/> |
|
|
|
Details: |
|
This script will intelligently rename the active document in BBEdit. It |
|
handles lone documents, documents in project windows, unsaved documents, |
|
and even documents created from the command line that are as yet unsaved. |
|
|
|
Special care has been given to handle errors and provide context in the |
|
renaming process. |
|
|
|
This script was originally based off of a script by the same name written by |
|
John Gruber <http://daringfireball.net/2004/10/rename_active_document>. |
|
Though this version has grown significantly from there, it would not exist |
|
at all without his willingness to share his work. Props to him and all those |
|
kind enough to share. |
|
|
|
*) |
|
|
|
on run |
|
tell application "BBEdit" |
|
activate |
|
|
|
try |
|
set doc to active document of text window 1 |
|
on error |
|
display alert "No active document." ¬ |
|
message "You must have a document open to rename one, silly!" ¬ |
|
buttons {"OK"} ¬ |
|
cancel button 1 |
|
end try |
|
set oldName to name of doc |
|
set newName to my promptForName(oldName) |
|
-- If the name did not change we can bail. |
|
if newName = oldName then return true |
|
my renameDoc(doc, oldName, newName) |
|
|
|
end tell |
|
end run |
|
|
|
|
|
on promptForName(oldName) |
|
-- Input: A document name as a string. |
|
-- Returns: A new document name as a string. |
|
|
|
tell application "BBEdit" |
|
set renameDialog to display dialog ¬ |
|
"Enter a new name:" ¬ |
|
default answer (oldName) ¬ |
|
buttons {"Cancel", "Rename"} ¬ |
|
default button 2 ¬ |
|
cancel button 1 ¬ |
|
with title "Rename “" & oldName & "”" ¬ |
|
with icon note |
|
end tell |
|
|
|
-- Note that we don’t need to handle cancel/rename logic because the dialog |
|
-- itself does that. (If they hit cancel, it bails. Otherwise, we rename.) |
|
|
|
return text returned of renameDialog |
|
|
|
end promptForName |
|
|
|
|
|
on renameDoc(doc, oldName, newName) |
|
-- Input: A BBEdit text document, |
|
-- A document name as a string. |
|
-- A new document name as a string. |
|
-- Returns: Nothing. |
|
|
|
tell application "BBEdit" |
|
if doc is on disk then |
|
set theFile to doc's file as alias |
|
tell application "Finder" to set the name of theFile to newName |
|
else -- It's a document that has never been saved. |
|
try |
|
set name of doc to newName |
|
on error |
|
set message1 to "Did you open this file with the bbedit command line tool?" |
|
set message2 to " If so, try saving, then renaming it." |
|
|
|
display alert "Cannot rename “" & oldName & "” in its current state." ¬ |
|
message message1 & message2 ¬ |
|
as warning ¬ |
|
buttons {"Cancel", "Save and Retry"} ¬ |
|
cancel button 1 |
|
|
|
-- Again, note that we don’t need to handle cancel/rename logic |
|
-- because the dialog itself does that. (If they hit cancel, it |
|
-- bails. Otherwise, we try to save and rename.) |
|
|
|
try |
|
save doc |
|
my renameDoc(doc, oldName, newName) |
|
on error |
|
set message1 to "For some unknown reason this document" |
|
set message2 to " can be neither saved nor renamed." |
|
|
|
display alert "Cannot rename “" & oldName & "”." ¬ |
|
message message1 & message2 ¬ |
|
as warning ¬ |
|
buttons {"OK"} ¬ |
|
cancel button 1 |
|
end try -- save doc |
|
|
|
end try -- set name |
|
end if -- doc on disk |
|
end tell |
|
|
|
end renameDoc |