如何使用 Spotlight 运行 shell 脚本?
有没有办法在 Mac OS X 10.6 上使用 Spotlight 运行 shell 脚本?我希望能够直接从 Spotlight 调用 shell 脚本,而无需打开终端。
Is there a way to run a shell script using Spotlight on Mac OS X 10.6? I would like to be able to invoke a shell script directly from Spotlight without opening up a terminal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
.command
后缀保存 shell 脚本 - 这使得它可以双击,并且您还应该能够直接从 Spotlight 运行它。Save your shell script with a
.command
suffix - this makes it double-clickable and you should also be able to run it directly from Spotlight too.另一种完全避免打开终端的方法:
在 Mac 上打开脚本编辑器,确保从语言下拉列表中选择 AppleScript,然后键入
将
touch ~/testfile
替换为您选择的代码 - 如您所见它不需要有 .command 后缀,甚至实际上也不需要是脚本。现在转到文件|导出并从文件格式:下拉列表中选择应用程序。确保所有选项:均未选中,并且从代码签名:下拉列表中选择不进行代码签名。随意命名,将其保存在任意位置。现在,您可以双击新的 AppleScript 应用程序或从 Spotlight 运行它,您的脚本将在不打开终端窗口的情况下运行。
作为一个额外的好处,因为它是 AppleScript,所以它可以与 Mac UI 交互——在显示对话框中显示结果、获取用户输入等。 https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html
Another approach that completely avoids opening a Terminal:
Open Script Editor on your Mac, make sure AppleScript is selected from the language dropdown and type
replacing
touch ~/testfile
with your code of choice--as you can see it need not have a .command suffix nor even actually be a script.Now go to File | Export and select Application from the File Format: dropdown. Make sure all the Options: are unchecked and Don't Code Sign is selected from the Code Sign: dropdown. Name it whatever you like, save it wherever you like. Now you can double-click your new AppleScript application or run it from Spotlight and your script will run without a Terminal window opening.
As a bonus since it's AppleScript it can interact with the Mac UI--show results in a display dialog, get user input, etc. https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html
我还想补充一点,如果您需要运行命令后终端窗口消失,您可以设置终端设置来做到这一点。
它位于设置>>>壳>>当外壳退出时。
这仍将使终端保持运行,但脚本至少会自行清理。
我用它来编写隐藏和显示桌面图标的脚本。
I would also like to add, if you need the terminal window to disappear after running the command, you can set the terminal settings to do just that.
It's under settings >> shell >> when the shell exits.
This will still leave Terminal running but the script will at least clean up after itself.
I used this for writing a script to hide and show desktop icons.
从 MacOS >= 10.0 开始,要使终端窗口在完成后消失:
Terminal
>首选项
>选择您的默认配置文件然后
Shell
>当 shell 退出时
选择如果 shell 完全退出则关闭
To make the Terminal window disappear when finished as of MacOS >= 10.0:
Terminal
>Preferences
> Select your default profileThen
Shell
>When the shell exits
selectClose if the shell exited cleanly