Skip to content

Instantly share code, notes, and snippets.

@benspaulding
Created October 16, 2011 04:33
Show Gist options
  • Save benspaulding/1290510 to your computer and use it in GitHub Desktop.
Save benspaulding/1290510 to your computer and use it in GitHub Desktop.
Intelligently rename active document in BBEdit.

BBEdit Rename Active Document Script

The script itself has most of the documentation. Just know that there are two scripts here:

  1. A plain-text version (.applescript)
  2. A compiled version (.scpt)

The plain text is here so you can actually see the script before downloading. However, BBEdit will need a compiled version. So either download the .scpt file and save it to the proper location, or copy the contents of the .applescript file, open AppleScript Editor, paste it in a new document, and save it as a .scpt to the proper location.

(*
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment