首页 > 解决方案 > Applescript - 打开一个新标签

问题描述

我的 AppleScript 能力相当有限,所以请原谅可能是一个简单的问题。

我将此脚本作为 Automator 服务,它将在新窗口中打开一系列别名。
由 Finder 中的键盘命令通过首选项>键盘>快捷方式>服务触发。
服务在 Finder 中接收选定的文件或文件夹

on run {input, parameters}
    repeat with aFile in input
        tell application "Finder"
            try
                set origFile to original item of aFile
                set aWindow to make new Finder window
                set aWindow's target to origFile's parent
                select origFile
            end try
        end tell
    end repeat
end run

我想尝试在选项卡中打开,最好不要求助于 GUI 脚本。
set aWindow to make new Finder window似乎没有等价物,并且在set aWindow to make new Finder tabApple 的在线文档中搜索“make”或“tab”已被证明是毫无结果的……或者相当多的水果,所有错误的品种:/

我有另一个来源的 GUI 版本

on new_tab()
    tell application "System Events" to tell application process "Finder"
        set frontmost to true
        tell front menu bar to tell menu "File" to tell menu item "New Tab"
            perform action "AXPress"
        end tell
    end tell
end new_tab

所以,如果直接方法失败,我怎么能把它折叠到我现有的脚本中?

macOS 10.13.4

标签: tabsapplescript

解决方案


未选中在选项卡中打开文件夹而不是Finder的新窗口首选项macOS 默认设置,以及打开文档时的Dock首选项Prefer tabs:System Preferences设置为In Full Screen Only中,以下示例AppleScript代码应该可以正常工作通过合并您的原始AppleScript代码处理程序代码 new_tab

on run {input, parameters}
    set madeNewWindow to false
    repeat with i from 1 to count input
        tell application "Finder"
            if (kind of item i of input) is equal to "Alias" then
                set origFile to original item of item i of input
                if not madeNewWindow then
                    set theWindow to make new Finder window
                    set madeNewWindow to true
                else
                    my makeNewTab()
                end if
                set theWindow's target to origFile's parent
                select origFile
            end if
        end tell
    end repeat
end run

on makeNewTab()
    tell application "System Events" to tell application process "Finder"
        set frontmost to true
        tell front menu bar to tell menu "File" to tell menu item "New Tab"
            perform action "AXPress"
        end tell
    end tell
end makeNewTab
  • 在我的系统上,我没有必要使用该delay 命令,但是,您的系统上可能需要也可能不需要命令,如果需要,请在适当调整值的同时根据delay 需要添加

  • 编码用于Automator服务中的Run AppleScript 操作,其中Service 在 [Finder] 中接收选定的 [文件或文件夹]

  • 需要将Finder添加到System Preferences中Security & Privacy下的Accessibility中。

  • 在 macOS High Sierra 下测试。


注意:示例AppleScript 代码 就是这样,并没有采用任何其他错误处理,然后显示的内容仅用于显示完成任务的多种方法之一。用户始终有责任根据需要/想要 添加/使用适当的错误处理。


推荐阅读