bash 将脚本添加到 MacOS finder 上下文菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4215287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:56:10  来源:igfitidea点击:

Adding a script to MacOS finder contextual menu

macosbashapplescriptfinderautomator

提问by Petruza

I want to add an option to the finder context menu that calls hg add %1with %1 being the full path of the selected file in finder.
Of course there are more useful cases I can think of, to add to the context menu.
Is there a simple way to do that which doesn't involve installing any 3rd party software or coding in a compiled language and building binary plugins?
Like creating a script with the script editor and dropping it in /Library/Contextual Menu Items/?

我想在 finder 上下文菜单中添加一个选项,该选项以hg add %1%1 作为 finder 中所选文件的完整路径调用。
当然,我还能想到更多有用的案例,添加到上下文菜单中。
有没有一种简单的方法可以做到不涉及安装任何 3rd 方软件或用编译语言编码和构建二进制插件?
喜欢使用脚本编辑器创建脚本并将其放入/Library/Contextual Menu Items/?

采纳答案by Zygmunt

Yeah, I know this is third party software - but for the sake of a fuller overview - http://www.abracode.com/free/cmworkshop/on_my_command.html. Another tool that would make your script writing easier is http://wafflesoftware.net/thisservice/adding items to the service menu.

是的,我知道这是第三方软件 - 但为了更全面的概述 - http://www.abracode.com/free/cmworkshop/on_my_command.html。另一个使您的脚本编写更容易的工具是http://wafflesoftware.net/thisservice/向服务菜单添加项目。

回答by studgeek

The steps have changed for Snow Leopard/10.6+ since @khachik's correct answer. To make sure its clear, here are the steps:

自@khachik 的正确答案以来,Snow Leopard/10.6+ 的步骤已更改。为确保其清晰,以下是步骤:

  • Open Automator
  • Create a new Service
  • Set the top two drop downs across the top to "Service receives selected files or foldersin Finder.app"
  • Set Pass input to as arguments
  • Write your script (see below for that).
  • Save and choose the service name
  • 打开自动机
  • 创建一个新的 Service
  • 将顶部的两个下拉菜单在顶部,以“服务接收选择files or foldersFinder.app
  • 将通过输入设置为 as arguments
  • 编写您的脚本(请参见下文)。
  • 保存并选择服务名称

Your Automator window should look like the this: :screenshot

您的 Automator 窗口应如下所示:截屏

You can now select multiple files in Finder and then execute your service from the Services sub-menu.

您现在可以在 Finder 中选择多个文件,然后从“服务”子菜单中执行您的服务。

For your script, I think you want the following. This changes to each argument's directory then adds it. I'm using a for loop because Finder allows you to select multiple files in different folders (which could be in different repositories).

对于您的脚本,我认为您需要以下内容。这会更改每个参数的目录,然后添加它。我正在使用 for 循环,因为 Finder 允许您在不同的文件夹(可能位于不同的存储库)中选择多个文件。

for f in "$@"
do
    cd $(dirname $f); hg add $f 
done

If you assumed they are all in the same repository you could do this:

如果您认为它们都在同一个存储库中,您可以这样做:

cd $(dirname ); hg add $@

回答by khachik

Open Automator, create a custom workflow. From Librarychoose Utilites, then drag and drop Run shell scriptto the workflow. Set Pass inputto As arguments. Write in your script: hg add $1. Then File menu->Save as a Plugin, specify the name, choose plugin for Finder, Save. Right click on the file, choose More->Autamator-><PLUGIN_NAME>.

打开 Automator,创建自定义工作流程。从Library选择Utilites,然后拖放Run shell script到工作流程中。设置Pass inputAs arguments。在您的脚本中写入:hg add $1. 然后File menu->Save as a Plugin,指定名称,选择plugin for Finder, Save。右键单击该文件,选择More->Autamator-><PLUGIN_NAME>.