为BBEdit'重命名活动文档'脚本

A feature I’ve always wanted, but which no application I’m aware of offers, is the ability to rename an open document“Save As” is not the same thing — because while it allows you to save a document with a new name, it leaves behind the previously-saved copy of the file with the old name.

通常,要重命名打开的文档,您需要执行以下操作:

  1. 保存并关闭文档。
  2. 切换到Finder。
  3. 选择文档文件。
  4. 改名。
  5. 重新开放。

You can skip steps 1 and 5 in apps that are savvy enough to recognize when the name of an open document file has been changed in the Finder. But even then, this has always struck me as something that ought to be doable in a single step.

所以,这是一个用于BBEdit 8的AppleScriptWhen you run the script, it pops up a simple dialog box containing a text edit field, set with the current name of the frontmost documentTo rename the document, just type a new name and click the Rename button.

重命名活动文档对话框的屏幕截图

Not only is it multi-document window savvy (renaming the current document in a multi-doc window), but itallows you to rename new untitled documents which haven’t yet been savedI use this for temporary scratch documents which I don’t need to keep, but for which I’d like to have more descriptive names than something like “untitled text 5”.

(这个脚本会work with versions of BBEdit prior to 8.0, because it depends on recent improvements to BBEdit’s scripting syntax.)

这是脚本:

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

To use it, copy and paste the script into a new Script Editor document, then save it in your ‘〜/ Library / Application Support / BBEdit / Scripts /’ folder.

这个怎么运作

First, we save the current name of the frontmost document by getting the name of文字窗口1我们将其保存在名为的变量中OLD_NAME. Then we use AppleScript’s显示对话框command to present a dialog box with a text edit field, the contents of which are set toOLD_NAME

一个名为的变量dialog_resultis used to store the result record from the显示对话框命令This record contains which button was clicked to dismiss the dialog and the string in the dialog’s text edit field.

如果用户单击重命名,我们将从中获取新文档名称dialog_result记录,并创建一个名为的变量dthat references the active document of text window 1.

BBEdit中的文献object has an “on disk” property; this is a boolean value that’s true if the document is represented by a file somewhere in your file system, false otherwise (like, say, a new document that has never been saved)如果(磁盘上的's')is true, we tell the Finder to change the name ofd’s associated file — “file” is another property defined by BBEdit’s文献宾语。

否则,如果(磁盘上的's')是假的,我们只是改变了它的价值d的“名字”财产。