使用Mac(Mac)上的内置终端(Terminal)应用程序,您可以运行许多命令来在您的机器上执行各种操作。从截取屏幕截图(taking screenshots of your screens)到一次重命名一大堆文件,终端(Terminal)命令涵盖了您通常在计算机上执行的许多操作。
您可能不会觉得不方便的唯一一件事是每次您想要运行命令时都必须启动终端应用程序。(Terminal)如果有更好更快的方法在Mac上运行(Mac)终端(Terminal)命令会怎样?
嗯,确实有。事实上,有多种方法可以在Mac上使用键盘快捷键运行终端(Terminal)命令。您可以将您最喜欢的组合键分配给您的特定命令,按下组合键将在您的机器上执行该命令。
使用 App 在 Mac 上使用快捷方式运行命令(Use An App To Run Commands Using a Shortcut On Mac)
为命令分配键盘快捷键的最简单方法是使用名为iCanHazShortcut(iCanHazShortcut)的第三方应用程序。这个应用程序让您可以更轻松地将任何键盘快捷键分配给Mac上的任何命令。
要配置应用程序,您只需要知道要分配的键盘快捷键和要执行的命令。
(Download)在您的Mac上(Mac)下载免费的开源应用程序并将其移至Applications文件夹。安装后启动应用程序。
当应用程序界面加载时,默认情况下您将位于“快捷方式”选项卡中。(Shortcuts)在此屏幕上,找到底部带有+(加号)的按钮,然后单击它以添加新的快捷方式。
以下屏幕可让您配置快捷方式以及它需要执行的命令。这是您需要在屏幕上的每个字段中输入的内容。
快捷方式(Shortcut)——将光标放在此字段中,然后输入要分配给命令的快捷方式。
操作(Action)- 这是一个可选名称,您可以分配给以后在列表中查找快捷方式。
命令(Command)- 在此处输入要执行的确切命令。
Workdir – 如果您的命令需要特定目录作为工作目录,请在此处选择它。
您可以通过单击底部的播放图标进行测试运行。满意后,单击它旁边的图标,它将保存快捷方式。
应用程序中的“首(Preferences)选项”选项卡还有一些您可以自定义的选项。这应该让您更好地控制应用程序在Mac上的工作方式。
从现在开始,只要您按下指定的键盘快捷键,它就会运行您的终端命令。
如果要执行多个命令,您也可以将它们添加到应用程序中。如果您愿意,可以修改甚至删除快捷方式。
使用 Automator 使用快捷方式执行命令(Execute Commands With a Shortcut Using Automator)
Automator还允许您使用键盘快捷键运行命令。首先(First),您需要创建一个包含您的命令的服务,然后为该服务分配一个键盘快捷键。
在Mac(Mac)上启动Automator应用程序。当新文档屏幕出现时,单击服务(Service)并选择选择(Choose)。
在以下屏幕上,在操作列表中搜索名为Run Shell Script的操作。找到它后,将其拖到右侧的主窗格中。
您会在新添加的操作下方看到一个大白框。在此框中输入要执行的所有命令。将此框视为您键入命令的终端窗口。(Terminal)
输入命令后,单击顶部的文件菜单并选择(File)保存(Save)以保存您的服务。为服务输入一个有意义的名称,然后点击Save。
现在服务已创建,是时候为它分配键盘快捷键了。为此,请单击左上角的Apple标志并选择(Apple)System Preferences。在以下屏幕上选择键盘。(Keyboard)
前往快捷方式(Shortcuts)选项卡,然后从左侧列表中选择服务。(Services)然后在右侧列表中找到您的服务,单击它,然后按所需的键盘快捷键。
您的服务将被分配您选择的键盘快捷键。
当您按下此快捷方式时,它将运行该服务,该服务又将在您的Mac上执行您的(Mac)终端(Terminal)命令。
使用 ActionShortcuts 使用快捷方式运行命令(Use ActionShortcuts To Run Commands Using a Shortcut)
ActionShortcuts让您可以运行比传统终端(Terminal)命令更多的东西。它允许您运行Apple脚本、工作流程、服务,当然还有终端(Terminal)命令。
与其他方法不同,此应用程序不是免费的,售价 2.99 美元。如果您想先试用,可以使用 7 天试用期。
下面显示了如何使用此应用程序使用键盘快捷键运行终端命令。(Terminal)
在 Mac 上启动TextEdit应用程序。单击格式(Format)菜单并选择制作纯文本(Make Plain Text)以删除格式。
在文件中输入(Enter)您希望执行的所有终端命令。(Terminal)然后通过单击文件(File)菜单并选择保存(Save)来保存文件。
在“另存为(Save As)”对话框中,输入文件的任何名称,但确保扩展名为command。点击保存(Save)以保存文件。
在您的 Mac 上下载、安装并打开ActionShortcuts应用程序。(ActionShortcuts)单击主界面上的打开脚本文件夹按钮。(Open Scripts Folder)
当文件夹打开时,将您的命令(command)文件拖放到它上面。返回应用程序,您将在列表中看到您的文件。单击(Click)文件旁边的记录快捷方式按钮以分配键盘快捷方式。(Record Shortcut)
分配快捷方式后,按下快捷方式将在Mac上启动包含您的命令的.command文件。
如果您想添加其他文件以执行,您可以通过单击菜单栏中的应用程序图标并选择Open Scripts Folder来实现。所有要执行的命令都必须放在这个文件夹中,应用程序会识别它们。
How To Run a Terminal Command Using a Keyboard Shortcut On Mac
With the built-in Terminal apр оn yoυr Mac, you can run a number of commands to exeсute various actions on your machine. From taking screenshots of your screens to renaming a whole bunch of files at once, Terminal commands cover a lot of things that you usually do on your machines.
The only thing that you may not find to be inconvenient is having to launch the Terminal app each time you want to run a command. What if there was a better and quicker way to run Terminal commands on a Mac?
Well, there is actually. In fact, there are multiple ways to run a Terminal command using a keyboard shortcut on Mac. You can assign your favorite key combination to your specific command, and pressing the combination will execute that command on your machine.
Use An App To Run Commands Using a Shortcut On Mac
The easiest way to assign keyboard shortcuts to your commands is to use a third-party app called iCanHazShortcut. This app makes it a whole lot easier to assign any keyboard shortcut to literally any command on your Mac.
To configure the app, all you need to know is the keyboard shortcut you want to assign and the command that is to be executed.
Download the free and open-source app on your Mac and move it to the Applications folder. Launch the app once it’s installed.
When the app interface loads up, you’ll be in the Shortcuts tab by default. On this screen, find the button with a + (plus) sign in it at the bottom and click on it to add a new shortcut.
The following screen lets you configure the shortcut as well as the command it needs to execute. Here’s what you need to enter in each of the fields on the screen.
Shortcut – put your cursor in this field and type in the shortcut you want to assign to the command.
Action – it’s an optional name you can assign to later find the shortcut in the list.
Command – enter the exact command you want to be executed here.
Workdir – if your command requires a specific directory as the work directory, select it here.
You can do a test run by clicking on the play icon at the bottom. Once you’re satisfied, click on the icon next to it and it’ll save the shortcut.
The Preferences tab in the app also has a few options you can customize. This should give you more control as to how the app works on your Mac.
From now on, whenever you press the specified keyboard shortcut, it’ll run your Terminal command.
If there is more than one command to be executed, you can add those to the app as well. Shortcuts can be modified and even deleted as well if you’d like to do it.
Execute Commands With a Shortcut Using Automator
Automator also allows you to run your commands using a keyboard shortcut. First, you need to create a service containing your command and then assign the service a keyboard shortcut.
Launch the Automator app on your Mac. When the new document screen appears, click on Service and select Choose.
On the following screen, search for the action named Run Shell Script in the actions list. When you find it, drag it over to the main pane on the right-hand side.
You’ll see a large white box beneath the newly added action. Enter in all the commands you want to execute in this box. Think of this box as a Terminal window where you type your commands.
When you’ve entered your commands, click on the File menu at the top and select Save to save your service. Enter a meaningful name for the service and hit Save.
Now that the service is created, it’s time to assign it a keyboard shortcut. To do so, click on the Apple logo at the top-left corner and select System Preferences. Choose Keyboard on the following screen.
Head to the Shortcuts tab and then select Services from the list on the left. Then find your service in the right-hand side list, click on it, and press the desired keyboard shortcut.
Your service will be assigned your chosen keyboard shortcut.
When you press this shortcut, it’ll run the service which in turn will execute your Terminal command on your Mac.
Use ActionShortcuts To Run Commands Using a Shortcut
ActionShortcuts lets you run more things than just traditional Terminal commands. It lets you run Apple scripts, workflows, services, and of course, the Terminal commands.
Unlike other methods, this app isn’t free and costs $2.99. You can use the 7 day trial period though if you want to try it out first.
The following shows how to run a Terminal command with a keyboard shortcut using this app.
Launch the TextEdit app on your Mac. Click on the Format menu and select Make Plain Text to remove formatting.
Enter all the Terminal commands you wish to execute in the file. Then save the file by clicking on the File menu and selecting Save.
In the Save As dialog box, enter any name for the file but make sure the extension is command. Hit Save to save the file.
Download, install and open the ActionShortcuts app on your Mac. Click on the Open Scripts Folder button on the main interface.
When the folder opens, drag and drop your command file onto it. Get back to the app and you’ll see your file in the list. Click on the Record Shortcut button next to your file to assign a keyboard shortcut.
Once a shortcut is assigned, pressing the shortcut will launch the .command file containing your commands on your Mac.
If you’d like to add additional files for execution, you can do so by clicking on the app icon in your menu bar and selecting Open Scripts Folder. All the commands that are to be executed must be placed in this folder and the app will recognize them.