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

能否使用Hammerspoon编辑Finder右键菜单项并创建Quick Actions?

用Hammerspoon自定义Finder右键菜单项及替代Automator Quick Actions方案

当然可以用Hammerspoon实现类似Automator Quick Actions的功能,甚至能灵活自定义Finder右键菜单项——核心思路是结合Hammerspoon的API和AppleScript(Hammerspoon可直接调用AppleScript操作Finder的UI元素)。

一、直接添加/修改Finder右键菜单项

Hammerspoon本身没有专门的Finder右键菜单API,但可以通过执行AppleScript代码实现。下面的示例会给Finder添加一个右键菜单项,选中文件后点击可快速将文件移动到指定文件夹:

-- 注册函数添加Finder右键菜单项
local function addFinderRightClickMenu()
  local appleScript = [[
    tell application "Finder"
      try
        -- 删除已存在的同名菜单项,避免重复添加
        delete menu item "移动到文档" of menu "Services" of menu bar item "Finder" of menu bar 1
      end try
      -- 添加新右键菜单项
      make new menu item at end of menu "Services" of menu bar item "Finder" of menu bar 1 with properties {name:"移动到文档", action:"moveToDocuments:"}
    end tell
  ]]
  hs.osascript.applescript(appleScript)
end

-- 启动时执行添加操作
addFinderRightClickMenu()

-- 绑定菜单项对应的操作逻辑
hs.application.watcher.new(function(name, event, app)
  if name == "Finder" and event == hs.application.watcher.activated then
    local selectedFiles = hs.finder.selectedFiles()
    if #selectedFiles > 0 then
      -- 将选中文件移动到~/Documents
      local destPath = os.getenv("HOME") .. "/Documents/"
      for _, file in ipairs(selectedFiles) do
        os.execute('mv "' .. file .. '" "' .. destPath .. '"')
      end
    end
  end
end):start()

二、实现类似Automator Quick Actions的快速操作

如果不需要修改右键菜单,只是想实现「选中文件后快速执行操作」,Hammerspoon的灵活性更高,比如用快捷键触发:

-- 绑定快捷键Cmd+Shift+M,触发将选中的Finder文件移动到文档文件夹
hs.hotkey.bind({"cmd", "shift"}, "M", function()
  local selectedFiles = hs.finder.selectedFiles()
  if #selectedFiles == 0 then
    hs.alert.show("没有选中任何文件")
    return
  end
  local destPath = os.getenv("HOME") .. "/Documents/"
  local successCount = 0
  for _, file in ipairs(selectedFiles) do
    local cmd = string.format('mv "%s" "%s"', file, destPath)
    local status = os.execute(cmd)
    if status then successCount = successCount + 1 end
  end
  hs.alert.show(string.format("已移动%d个文件到文档", successCount))
end)

你还可以监听Finder的选中变化自动触发操作(比如选中图片时自动显示预览),或者用hs.menubar创建全局菜单,配合Finder选中文件执行操作。

注意事项:

  • 修改Finder右键菜单的AppleScript可能需要系统权限,第一次执行会弹出权限请求,允许即可。
  • 要删除自定义右键菜单项,执行对应的AppleScript删除代码,或重启Finder即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 23:55:27