Say I am in a Finder window (eg: ~/Documents/Code ) and I want to be able to use a keyboard shortcut to open a terminal window with the prompt
Most the the questions are answered using mouse.
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.Sign up to join this community
In System Preferences > Keyboard > Shortcuts > Services under Files and Folders there is a New Terminal at Folder shortcut which you can assign a keyboard shortcut to; however, it only appears on the Finder > Services menu, or the right-click context menu, when a Folder is selected. So if you have a Finder window open but no additional folder selected, it is not available. This may be fine for one's particular usage, but if not, then here is an alternative.
For example, say you have a Finder window open to your Downloads folder, which is selected in the Sidebar, and you want to open a Terminal window there. Well the built in service isn't available because an individual Folder isn't selected and what's selected in the Sidebar doesn't count...
One way to open a Terminal window at the location of the current Finder window, using a keyboard shortcut, is to use AppleScript in an Automator Service (Quick Action in macOS Mojave), then assign it a keyboard shortcut in: System Preferences > Keyboard > Shortcuts > Services > ...
Create an Automator Service adding a Run AppleScript action, then replace the default code with the example AppleScript code shown below.
Save the service as e.g. Open Terminal Here with the settings as shown in the image below. Then assign it a keyboard shortcut. It should show under the General section of the aforementioned path.
Example AppleScript code:
tell application "Finder" if exists Finder window 1 then set currentFolder to target of Finder window 1 as alias else return end if end tell tell application "Terminal" do script "cd " & quoted form of POSIX path of currentFolder & "; clear" activate end tell
As an added bonus, you can also use the example AppleScript code saved as an AppleScript application in Script Editor and place it in the Toolbar of Finder, thus allowing one to click the icon to open a Terminal window at the current location when one wants versus using the keyboard short assigned to the Automator service. No third-party app required!
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
There is a 3rd party app named gotoshell available at the Macapp Store for free. This app enables you to put a small icon on your finder window tool bar. When you click the icon, a terminal or iterm2 window opens at the same directory as the finder window. The icon appears on all finder windows. An image of icon is shown below.