【问题标题】:Applescript - open a new tabApplescript - 打开一个新标签
【发布时间】:2018-04-25 10:58:14
【问题描述】:

我的 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 tab 并且在 Apple 的在线文档中搜索“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

【问题讨论】:

  • Service 的设置是什么,即 Service 在 [what] 中接收选定的 [what],以及如何Run AppleScript action 接收其input,换句话说,它是第一个或唯一一个 action 还是什么?顺便说一句,由于您所做的研究没有提出任何非 UI 脚本 方法来打开 tab,这是正确的...... AFAIK 没有办法使用 regular i> AppleScript 打开tab 而不诉诸UI 脚本。另外,您运行的是哪个版本的 macOS
  • @user3439894 - 额外信息添加到问题。如果您需要更多信息,请告诉我。

标签: tabs applescript


【解决方案1】:

ma​​cOS 默认 在选项卡中打开文件夹而不是新窗口 首选项Finder未选中,Dock 首选项 打开文档时首选标签:系统首选项中设置仅限全屏,那么下面的示例 AppleScript 代码应该可以根据需要结合您的原始AppleScript codenew_tabhandlercode

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 command 但是,delay commands 在您的系统上可能需要也可能不需要系统,如果是这样,在适当调整时根据需要添加。

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

  • 需要将 Finder 添加到 System PreferencesSecurity & Privacy 下的 Accessibility

  • 在 macOS High Sierra 下测试。


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

【讨论】:

  • 就是这个!似乎没有什么区别,但是我设置了 Dock & Finder 首选项。它给了我一个新窗口,每个别名都有标签……这实际上比我想象的要好。好东西,谢谢。
  • @Tetsujin,因为另一个答案是更改 default 设置,所以我提到了这两个 default 设置,因为我没有使用修改后的 default 设置,只有原始 defaults。另外,我没有提到 Finder 需要添加到 System Preferences > Security & Privacy > Accessibility因为我想你已经添加了它。顺便说一句,我也不喜欢使用 UI 脚本 但是当另一种方法不起作用时它就在那里。很高兴这对你有用。谢谢!
  • 当然。实际上我通常不使用标签,所以把它们都关掉了。即使关闭了首选项,您的解决方案也会生成选项卡,这很棒,而且正是我完成这项任务所需要的。我最初试图找到一个没有 UI 脚本的解决方案,但这种混合似乎完全符合我的需要。再次感谢。
【解决方案2】:

AppleScript 从 POSIX 路径列表中打开 Finder 中的选项卡

在应用程序脚本编辑器中运行它会从一个可以在普通 POSIX 文件路径中设置的列表中打开一个预定义的选项卡列表,例如/path/to/folder-or-file。要获得指向您的文件夹或文件的链接,请在菜单中按CMD+i 或按File->Get Info(或只需右键单击文件/文件夹本身)。在弹出的小窗口中,从名为General -> Where: 的字段中复制路径或使用打印工作目录 (pwd) 命令从终端获取路径,然后复制粘贴到中的 item-vars 中下面的脚本。使用重复循环,我们遍历列表中包含的变量并为每个项目拉出一个选项卡。对于那些使用相同文件夹但数量很多的项目来说非常方便!

on convertPathToAlias(thePath)
    tell application "System Events"
        try
            return (path of disk item (thePath as string)) as alias
        on error
            return (path of disk item (path of thePath) as string) as alias
        end try
    end tell
end convertPathToAlias


set item1 to "/Users/username/Desktop/myfolder1"
set item2 to "/Users/username/Desktop/myfolder2"
set item3 to "/Users/username/Desktop/myfolder3"
set item4 to "/Users/username/Desktop/myfolder4"

set myList to {item1, item2, item3, item4}

set default_path to convertPathToAlias(item1)

tell application "Finder"
   activate
   open default_path
end tell

repeat with theItem in myList

    set current_path to convertPathToAlias(theItem)

    tell application "System Events" to keystroke "t" using command down
    delay 0.3
    tell application "Finder" to set target of front window to current_path

end repeat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 2012-06-04
    • 2015-04-09
    • 2011-09-22
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多