Skip to content

Instantly share code, notes, and snippets.

@mzbali
Last active January 28, 2024 23:17
Show Gist options
  • Select an option

  • Save mzbali/aef668271f336137b6a50c82b1c6838c to your computer and use it in GitHub Desktop.

Select an option

Save mzbali/aef668271f336137b6a50c82b1c6838c to your computer and use it in GitHub Desktop.
Quickly open iTerm2 or create a new file in your current Finder location with these macOS Quick Actions, customisable with your preferred keyboard shortcuts.

Finder Quick Actions: Open iTerm2 and Create New File

This guide provides step-by-step instructions on how to create two Quick Actions in Finder:

  • Open the current folder in iTerm2
  • Create a new file in the current folder

These actions can be triggered using keyboard shortcuts for quick and easy access.

Open iTerm2 from Finder

Follow these steps to create a Quick Action that opens the current folder in iTerm2:

  1. Open Automator: You can find it in your Applications folder or search for it in Spotlight.

  2. Create a New Document: Click on "New Document" and then select "Quick Action". Click "Choose".

  3. Set Workflow Input: Set the "Workflow receives" dropdown to "no input".

  4. Add AppleScript Action: In the left panel, find "Run AppleScript" and drag it to the right panel.

  5. Enter AppleScript Code: Replace the default AppleScript code with the following:

try
  tell application "Finder" to set this_folder ¬
   to (folder of the front window) as alias
on error -- no open folder windows
  set this_folder to path to desktop folder as alias
end try

tell application "iTerm"
  if it is running then
    tell current window
      create tab with default profile
      tell current session
        write text "cd " & quoted form of POSIX path of this_folder
      end tell
    end tell
  else
    activate
    tell current window
      tell current session
        write text "cd " & quoted form of POSIX path of this_folder
      end tell
    end tell
  end if
end tell
  1. Save the Workflow: Save the workflow with a name like "Open iTerm Here".

Add a Keyboard Shortcut

  1. Open System Preferences: Go to System Preferences -> Keyboard -> Shortcuts -> Services.

  2. Find the Service: Scroll down until you find the Service "Open iTerm Here" under the "General" section.

  3. Add Shortcut: Click on the "none" text on the right side of "Open iTerm Here", which will change to "Add Shortcut".

  4. Set Shortcut: Press the keys you want to use as a shortcut, for example, ⌘ Command + ⌥ Option + T.

Now, when you press your chosen shortcut, iTerm2 will open in the current folder or the Desktop folder if no Finder window is open.

Create New File from Finder

Follow the same steps as above, but replace the AppleScript code with the following and save the workflow as "Create New File":

try
  tell application "Finder" to set the this_folder ¬
   to (folder of the front window) as alias
on error -- no open folder windows
  set the this_folder to path to desktop folder as alias
end try

set thefilename to text returned of (display dialog ¬
 "Create file named:" default answer "filename.txt")
set thefullpath to POSIX path of this_folder & thefilename
do shell script "touch \"" & thefullpath & "\""

For the keyboard shortcut, you might use ⌘ Command + ⌥ Option + N.

Note: The keyboard shortcuts mentioned in this document are just examples. You can choose any combination that suits your needs and is not already in use by the system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment