You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

优化Automator复制当前路径脚本:按选中对象适配路径

优化Finder路径复制的Automator脚本实现

完全可以实现你需求的三种路径复制逻辑,修改后的AppleScript代码如下:

try
    tell application "Finder"
        -- 获取当前选中的项目
        set selectedItems to selection
        -- 判断选中情况
        if count of selectedItems is 0 then
            -- 未选中任何内容,复制当前目录路径
            set the clipboard to POSIX path of (target of window 1 as alias)
        else
            -- 选中了项目,取第一个选中项(支持单个选中,若要支持多个可扩展)
            set targetItem to item 1 of selectedItems
            -- 复制选中项的完整POSIX路径
            set the clipboard to POSIX path of (targetItem as alias)
        end if
    end tell
on error
    beep
end try

代码逻辑说明

  • 未选中内容时:脚本会获取当前Finder窗口的目标目录,将其POSIX路径复制到剪贴板,和你原有代码的逻辑一致。
  • 选中单个文件/文件夹时:脚本会优先获取选中的对象,将该对象的完整POSIX路径复制到剪贴板。
  • 错误处理:保留原有的try/on error结构,遇到异常(比如没有打开的Finder窗口)时会发出提示音。

扩展提示

如果需要支持多选对象复制多个路径(每行一个),可以把代码中的item 1 of selectedItems部分替换为以下逻辑:

set pathList to {}
repeat with anItem in selectedItems
    set end of pathList to POSIX path of (anItem as alias)
end repeat
set the clipboard to (join pathList with linefeed)

内容的提问来源于stack exchange,提问作者Miles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 17:39:57