Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apple script to open an iterm2 "nightly" window from right-clicking on a file or folder in Finder. To use:(1) Open Automator(2) Create a new service(3) Change "Service receives selected" drop downs to "Files or folders" in "Finder"(4) Select "Run applescript" from the sidebar, then paste this script in and save
-- Adapted from these sources:
-- http://peterdowns.com/posts/open-iterm-finder-service.html
-- https://gist.github.com/cowboy/905546
--
-- Modified to work with files as well, cd-ing to their container folder
on run {input, parameters}
tell application "Finder"
set my_file to first item of input
set filetype to (kind of (info for my_file))
-- Treats OS X applications as files. To treat them as folders, integrate this SO answer:
-- http://stackoverflow.com/a/6881524/640517
if filetype is "Folder" or filetype is "Volume" then
set dir_path to quoted form of (POSIX path of my_file)
else
set dir_path to quoted form of (POSIX path of (container of my_file as string))
end if
end tell
CD_to(dir_path)
end run
on CD_to(theDir)
tell application "iTerm"
activate
set go_dir to "cd " & theDir
set newWindow to (create window with default profile)
tell current session of first window
write text go_dir
end tell
end tell
end CD_to
@evrpress

This comment has been minimized.

Copy link

@evrpress evrpress commented Jun 3, 2016

This is working but it opens the parent directory. Also it always creates a new window instead of a new tab. Any idea?

@redcomethk

This comment has been minimized.

Copy link

@redcomethk redcomethk commented Jun 5, 2016

@revaxarts Try this snippet:

on CD_to(theDir)
    tell application "iTerm"
        activate
        set go_dir to "cd " & theDir
        try
            set currentWindow to the last window
        on error
            set currentWindow to (create window with default profile)
        end try
        tell current window
            -- These commands return a tab
            set newTab to (create tab with default profile)
            tell newTab
                select
                tell current session of currentWindow
                    write text go_dir
                end tell
            end tell
        end tell
    end tell
end CD_to
@evrpress

This comment has been minimized.

Copy link

@evrpress evrpress commented Jun 20, 2016

theDir is still the parent so it has to be something in the first part of the script

@evrpress

This comment has been minimized.

Copy link

@evrpress evrpress commented Jun 20, 2016

Seems filetype is not correctly. Changing

    if filetype is "Folder" or filetype is "Volume" then
        set dir_path to quoted form of (POSIX path of my_file)
    else
        set dir_path to quoted form of (POSIX path of (container of my_file as string))
    end if

to

    set dir_path to quoted form of (POSIX path of my_file)

seems to work in my case, both for files and folders

@adriancmiranda

This comment has been minimized.

Copy link

@adriancmiranda adriancmiranda commented Sep 14, 2016

Thank's @revaxarts

@ttimasdf

This comment has been minimized.

Copy link

@ttimasdf ttimasdf commented Dec 20, 2016

use kind of upon info for is not recommanded according to apple documentation because it's localized. It will not work on non-English system. it returned 文件夹 for me... 😢

@jjaimon

This comment has been minimized.

Copy link

@jjaimon jjaimon commented Mar 24, 2017

Is this working on OSX 10.12.4 for anyone? It appears that "first item of input" causes the script to fail. For testing, I added "Get Specified finder items" in the workflow within Automator. Script receives input from that action and works correctly. However, when you click on the application, you just get a message "The action "Run Applescript" encountered an error"

I created this as a service. I can right click and select the service on the folder. It works and the folder is opened in iTerm as a . new tab.

@edeefelt

This comment has been minimized.

Copy link

@edeefelt edeefelt commented Nov 1, 2017

Took me trying a few other versions before fining this one. Awesome update to the two previous versions. Thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.