首页 > 解决方案 > 如何使用 Applescript 在 BBEdit 窗口的项目窗格中选择特定文件?解决了

问题描述

解决了——最后!

如何使用 Applescript 在 BBEdit 窗口的项目窗格中选择特定文件?

当我在没有 AppleScript 的情况下打开 BBEdit 时,我的项目打开了,左侧窗格中的项目文件列表就像它应该的那样。

使用 AppleScript,我希望

  1. 在此窗格中选择特定文件,然后
  2. 在其“标记”菜单下选择“在 BBEdit 中预览”。

现在的挑战是将 AppleScript 用于#1。我尝试了以下方法,但它不起作用。

tell application "BBEdit"
    activate

    set theFile to "compiled_corona_virus.html"

    select theFile
end tell

我收到此错误:

error "BBEdit got an error: \"compiled_corona_virus.html\" doesn’t understand the “select” message." number -1708 from "compiled_corona_virus.html"

我肯定看过 BBEdit 的 AppleScript Dictionary,上面写着:

select v : Select the specified object
select [specifier] : the object to select

对于它的价值,我在 Apple 的脚本编辑器中尝试了这个脚本:

tell application "BBEdit"
    activate

    tell application "Finder" to set theFile to "Macintosh HD:Users:johnlove:Sites:www.lovetoteach.dev:Web_Site_Storage:lovesongforever.com:coronavirus:compiled_corona_virus.html"

    select theFile
end tell

相同的选择错误?

我也尝试过使用

open theFile

并且 BBEdit 确实打开了它并在最右边的窗格中显示了打开的文件,但没有在最左边的项目窗格中选择这个文件。

由 barebones.com 上的 Rich Siegel 解决

Rich 是《智慧之珠》一书的作者“总有一天我会回顾这一切并大笑……直到他们让我镇定下来!”

on setAutoRevealSelectedDocumentInProjectList()

    tell application "Terminal"
        activate

        tell application "System Events"
            keystroke "defaults write com.barebones.bbedit AutoRevealSelectedDocumentInProjectList -bool YES "
            keystroke return
        end tell
    end tell

end setAutoRevealSelectedDocumentInProjectList

Rich 耐心地告诉我们,BBEdit 的窗口最左边的 Pane,由两个独立的部分组成:

1) the Project's files on top
2) Currently Open Documents at the bottom

如果您开发一个通过 open(name) 命令打开文件的 AppleScript,BBEdit 将打开它并在底部的“当前打开的文档”下突出显示它。

但是,这不会更改顶部选择的项目文件。

所以,要做到这一点,程序员必须调用终端:

setAutoRevealSelectedDocumentInProjectList()

谢谢你,丰富!!

标签: applescriptbbedit

解决方案


使用“打开”而不是“选择”。如果文件已经打开,BBEdit 会选择它。如果文件尚未打开,BBEdit 将打开它然后选择它。(您是否使用硬编码路径无关紧要,只要您要求 BBEdit 打开的文件确实存在。)

“打开”命令是指对刚刚打开的文档的引用。

“选择”动词不适合用于文件;它仅适用于“文档”对象。

这(或类似的东西)会做你想做的事:

tell app "BBEdit"
    set openedDocument to (open POSIX file "/path/to/some/file.txt") -- substitute your actual file path here
    select openedDocument
end tell

此外:如果您正在处理项目文档,“项目”部分中的选择不会自动更改以反映活动文档。(“当前打开的文档”部分中的选择总是如此。)

有一个专家偏好可以控制这种行为。来自专家偏好帮助的抄袭:

当您选择项目列表时,BBEdit 不会自动显示项目列表中的文档;View => Reveal in Project List 用于此目的。如果要自动显示:

defaults write com.barebones.bbedit AutoRevealSelectedDocumentInProjectList -bool YES


推荐阅读