Applescript 如何在创建时向新BBEdit文档添加文本?

Applescript 如何在创建时向新BBEdit文档添加文本?,applescript,attachment,bbedit,Applescript,Attachment,Bbedit,我在Textwrangler/BBedit中打开了很多新文档,我希望它们总是在顶部打印日期。我希望这是自动的,这样我就不必每次都记得运行脚本 我是BBEdit新手,但我真的很喜欢Textwrangler,并且已经使用了很多年了。我阅读了BB上的一些文档,我认为在活动中附加一些Applescript可能是一种方法。然而,列出的事件似乎没有一个是完全正确的,我也不想在现有文档中添加日期 我发现以下页面是一个很好的起点: 我还从BB文档中找到了这些相关的钩子: 应用程序连接点 Applicatio

我在Textwrangler/BBedit中打开了很多新文档,我希望它们总是在顶部打印日期。我希望这是自动的,这样我就不必每次都记得运行脚本

我是BBEdit新手,但我真的很喜欢Textwrangler,并且已经使用了很多年了。我阅读了BB上的一些文档,我认为在活动中附加一些Applescript可能是一种方法。然而,列出的事件似乎没有一个是完全正确的,我也不想在现有文档中添加日期

我发现以下页面是一个很好的起点:

我还从BB文档中找到了这些相关的钩子: 应用程序连接点

  • ApplicationIDFinishLaunching:在应用程序完成时调用 启动
  • applicationShouldQuit:选择退出(或应用程序)时调用 因任何其他原因接收“退出”事件)
  • applicationDidQuit:在应用程序完成关闭并即将退出时调用
  • ApplicationIDSwitchin:当BBEdit被带到前台时调用
  • applicationWillSwitchOut:在将BBEdit放入后台时调用
文件附件点

  • documentDidOpen:当文档已打开并准备好使用时调用。(由于BBEdit支持多种类型的文档,因此脚本应允许参数为任何类型的文档。)
  • documentShouldClose:当应用程序准备关闭应用程序时调用 文件
  • documentDidClose:当应用程序关闭文档时调用
  • documentShouldSave:当应用程序试图确定是否应保存给定文档时调用
  • documentWillSave:当应用程序即将开始保存文件时调用 文件。(请注意,只有在从 “文档应保存”
  • documentDidSave:成功保存文档后调用
  • documentWillUnlock:当BBEdit要使文档可写时调用(例如,当您单击铅笔解锁文档时)
  • documentDidUnlock:当BBEdit成功生成文档时调用 可写的
  • documentWillLock:当BBEdit将文档设置为只读时调用
  • documentDidLock:当BBEdit成功地将文档设置为只读时调用
不过,我不知道这些是否真的合适。我也可以尝试将一些脚本添加到startup文件夹中,但我不确定该怎么做,比如说,为所有打开的文档添加一个日期。我以前从未做过applescript,所以这是一个小小的尝试和错误

我有一段代码,我试着自己运行,它运行得很好:

tell application "BBEdit"
tell text window 1
select insertion point after (last character)
set selection to ((current date) as string)
end tell
end tell
对于如何在创建文件时执行上述代码,我有点不知所措。

打开脚本编辑器,将以下代码粘贴到新的脚本文档中:

use BBEdit : application "BBEdit"
use scripting additions

on documentDidOpen(doc)
    set n to the doc's name
    set t to the doc's text as string

    if n does not start with "untitled text" then return
    if t's length > 0 then return

    set the contents of the doc to (the (current date) as text) ¬
        & linefeed & linefeed
end documentDidOpen
将其另存为类型
script
(扩展名
.scpt
),并将其命名为
Document.documentDidOpen.scpt
。直接保存或随后将其移动到文件夹
~/Library/Application Support/BBEdit/Attachment Scripts/
;如果该文件夹不存在,请创建它


重新启动BBEdit应该没有必要,但也不会有什么坏处。现在,每当您创建一个新文档(任何类型的),它都会以当前日期和时间作为标题。

尝试使用BBEdit的“将脚本附加到菜单项”功能(v11用户手册第295页)。简而言之,如果您使用基于菜单/命令的名称将脚本保存到“菜单脚本”文件夹中,则此脚本将在选择该菜单项时运行。因此,在您的场景中:

将下面的脚本保存到BBEdit的菜单脚本文件夹中,文件名为“新建•文本文档”

tell application "BBEdit"
    set cDate to ((current date) as text)
    make new document with properties {contents:cDate}
end tell
另一方面,通常可以避免将选择与插入点一起使用,例如:

tell document 1 of application "BBEdit" to set text of ¬
    first insertion point of text 1 to ((current date) as text)
tell application "BBEdit"
    set tdCount to count of text documents
    repeat with i from 1 to tdCount
    set text of first insertion point of text 1 of ¬
        text document i to ((current date) as text) & linefeed
    end repeat
end tell
在您的第二个“问题”场景中,您可能会循环使用所有现有窗口,例如:

tell document 1 of application "BBEdit" to set text of ¬
    first insertion point of text 1 to ((current date) as text)
tell application "BBEdit"
    set tdCount to count of text documents
    repeat with i from 1 to tdCount
    set text of first insertion point of text 1 of ¬
        text document i to ((current date) as text) & linefeed
    end repeat
end tell

恐怕没有;我创建了文件夹并保存了代码,但它看起来没有调用。有什么调试建议吗?编辑脚本的
documentDidOpen
处理程序,并在其最开始插入一行,内容为
return display alert“Hello”
。您可以从当前位置编辑脚本,然后简单地保存它。现在可以在BBEdit中打开一个文档,也可以创建一个新文档。是否会出现警报?另外,您可以告诉我您使用的是什么版本的macOS和BBEdit吗?