Skip to content

Instantly share code, notes, and snippets.

@nisc
Created May 26, 2025 00:59
Show Gist options
  • Save nisc/ec2edaabd585e90912248dc432507287 to your computer and use it in GitHub Desktop.
Save nisc/ec2edaabd585e90912248dc432507287 to your computer and use it in GitHub Desktop.
Add "Open in iTerm" to MacOS Finder context menu (Automator script)
-- Adds "Open with iTerm" action in Finder right-click context menu
-- If a file is selected, it navigates to its parent folder and outputs the file type
-- If a folder is selected, it navigates to that folder
on run {input, parameters}
-- Message printed in new iTerm tab
set finderMessage to "\\033[3m~~~ Opened by Finder ~~~\\033[0m"
-- Get the first selected item (file or folder)
set theItem to item 1 of input
set itemInfo to info for theItem
set isFolder to folder of itemInfo
if isFolder then
-- If it's a folder, use its POSIX path directly
set theFolder to POSIX path of theItem
set fileInfo to ""
else
-- If it's a file, get its POSIX path and parent folder
set theFile to POSIX path of theItem
set theFolder to do shell script "dirname " & quoted form of theFile
-- Get file type and MIME type
set fileTypeCmd to "file -b " & quoted form of theFile
set fileType to do shell script fileTypeCmd
set mimeTypeCmd to "file -b --mime-type " & quoted form of theFile
set mimeType to do shell script mimeTypeCmd
set fileInfo to "File type: " & fileType & " (MIME: " & mimeType & ")"
end if
-- Construct the printf part
if isFolder then
-- For folders, just the message and two newlines
set printfPart to "printf '" & finderMessage & "\\n\\n'"
else
-- For files, message, file info, and two newlines
set printfFormat to finderMessage & "\\n%s\\n\\n"
set printfPart to "printf '" & printfFormat & "' " & quoted form of fileInfo
end if
-- Construct the full command
set cdPart to "cd " & quoted form of theFolder
set command to cdPart & "; clear; " & printfPart
-- Open iTerm and execute the command
tell application "iTerm"
if (count of windows) = 0 then
-- Create a new window
-- TODO: bug - may create two iTerm tabs and/or windows instead of just one
set newWindow to create window with default profile
tell newWindow
tell first session of first tab
write text command
end tell
end tell
else
-- Create a new tab in existing window
tell current window
create tab with default profile
tell current session
write text command
end tell
end tell
end if
activate
end tell
end run
@nisc
Copy link
Author

nisc commented May 26, 2025

Adds a context menu item:

image

How to add in Automator:

image

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