Skip to content

Instantly share code, notes, and snippets.

@benspaulding
Created October 16, 2011 04:33
Show Gist options
  • Select an option

  • Save benspaulding/1290510 to your computer and use it in GitHub Desktop.

Select an option

Save benspaulding/1290510 to your computer and use it in GitHub Desktop.

Revisions

  1. benspaulding revised this gist Dec 22, 2011. 2 changed files with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Rename Active Document.applescript
    Original file line number Diff line number Diff line change
    @@ -105,7 +105,7 @@ on renameDoc(doc, oldName, newName)
    my renameDoc(doc, oldName, newName)
    on error
    set message1 to "For some unknown reason this document"
    set message2 to " cannot be neither saved nor renamed."
    set message2 to " can be neither saved nor renamed."

    display alert "Cannot rename “" & oldName & "”." ¬
    message message1 & message2 ¬
    Binary file modified Rename Active Document.scpt
    Binary file not shown.
  2. benspaulding revised this gist Dec 22, 2011. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion Rename Active Document.applescript
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,14 @@ on run
    tell application "BBEdit"
    activate

    set doc to active document of text window 1
    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.
  3. benspaulding revised this gist Oct 22, 2011. 3 changed files with 129 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions ReadMe.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    ====================================
    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.
    114 changes: 114 additions & 0 deletions Rename Active Document.applescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@
    (*
    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

    set doc to active document of text window 1
    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 " cannot 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
    Binary file modified Rename Active Document.scpt
    Binary file not shown.
  4. benspaulding revised this gist Oct 16, 2011. 1 changed file with 26 additions and 20 deletions.
    46 changes: 26 additions & 20 deletions Rename Active Document.scpt
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,16 @@ File:
    Abstract:
    This script will intelligently rename the active document in BBEdit.
    Version:
    1.0
    1.1
    Author:
    John Gruber <http://daringfireball.net/>
    Modified By:
    Ben Spaulding <http://benspaulding.us/>
    Details:
    For more information, see the following:
    @@ -22,22 +25,25 @@ Details:
    *)

    tell application "BBEdit"
    activate
    set old_name to name of text window 1
    set dialog_result to display dialog ¬
    "Rename active document:" default answer (old_name) ¬
    buttons {"Cancel", "Rename"} default button 2 ¬
    with icon note
    if button returned of dialog_result = "Rename" then
    set new_name to text returned of dialog_result
    set d to active document of text window 1
    if (d's on disk) then
    set the_file to d's file
    tell application "Finder"
    set name of the_file to new_name
    end tell
    else -- it's a document that has never been saved
    set name of d to new_name
    end if
    end if
    activate
    set old_name to name of text window 1
    set dialog_result to display dialog ¬
    "Rename active document:" default answer (old_name) ¬
    buttons {"Cancel", "Rename"} default button 2 ¬
    with icon note
    if button returned of dialog_result = "Rename" then
    set new_name to text returned of dialog_result
    set d to active document of text window 1
    if (d's on disk) then
    set the_file to d's file
    tell application "Finder"
    -- I was getting an error from Finder, and the following alias
    -- fixed it. I wish I fully understood why.
    set the_file to the_file as alias
    set name of the_file to new_name
    end tell
    else -- it's a document that has never been saved
    set name of d to new_name
    end if
    end if
    end tell
  5. benspaulding created this gist Oct 16, 2011.
    43 changes: 43 additions & 0 deletions Rename Active Document.scpt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    (*
    File:
    Rename Active Document.scpt
    Abstract:
    This script will intelligently rename the active document in BBEdit.
    Version:
    1.0
    Author:
    John Gruber <http://daringfireball.net/>
    Details:
    For more information, see the following:
    - http://daringfireball.net/2004/10/rename_active_document
    - BBEdit User Manual
    *)

    tell application "BBEdit"
    activate
    set old_name to name of text window 1
    set dialog_result to display dialog ¬
    "Rename active document:" default answer (old_name) ¬
    buttons {"Cancel", "Rename"} default button 2 ¬
    with icon note
    if button returned of dialog_result = "Rename" then
    set new_name to text returned of dialog_result
    set d to active document of text window 1
    if (d's on disk) then
    set the_file to d's file
    tell application "Finder"
    set name of the_file to new_name
    end tell
    else -- it's a document that has never been saved
    set name of d to new_name
    end if
    end if
    end tell