wbond / sublime_terminal

Launch terminals from the current file or the root project folder
https://packagecontrol.io/packages/Terminal
MIT License
597 stars 117 forks source link

iterm2 does not open and change directory on first call #208

Closed kitsguru closed 5 years ago

kitsguru commented 5 years ago

In issue #207, it was noted that iterm2 did not cd to the project directory. The solution is to activate the window then tell the session to cd

#!/bin/bash

CD_CMD="cd "\\\"$(pwd)\\\"" && clear"
if echo "$SHELL" | grep -E "/fish$" &> /dev/null; then
    CD_CMD="cd "\\\"$(pwd)\\\""; and clear"
fi
VERSION=$(sw_vers -productVersion)
OPEN_IN_TAB=0

while [ "$1" != "" ]; do
    PARAM="$1"
    VALUE="$2"
    case "$PARAM" in
        --open-in-tab)
            OPEN_IN_TAB=1
            ;;
    esac
    shift
done

if (( $(expr $VERSION '<' 10.7) )); then
    RUNNING=$(osascript<<END
    tell application "System Events"
        count(processes whose name is "iTerm2")
    end tell
END
)
else
    RUNNING=1
fi

if (( ! $RUNNING )); then
    osascript<<END
    tell application "iTerm"
        tell current window
            activate
            tell current session
                write text "$CD_CMD"
            end tell
        end tell

    end tell
END
else
    if (( $OPEN_IN_TAB )); then

        osascript &>/dev/null <<EOF
        tell application "iTerm"
            if (count of windows) = 0 then
                set theWindow to (create window with default profile)
                set theSession to current session of theWindow
            else
                set theWindow to current window
                tell current window
                    set theTab to create tab with default profile
                    set theSession to current session of theTab
                end tell
            end if
            tell theSession
                write text "$CD_CMD"
            end tell

            activate
        end tell
EOF
    else
        osascript &>/dev/null <<EOF
        tell application "iTerm"
            tell (create window with default profile)
                tell the current session
                    write text "$CD_CMD"
                end tell
            end tell

            activate
        end tell
EOF
    fi
fi
twolfson commented 5 years ago

Cool, can you submit this as a PR? I'm crunched for time so I don't have much for copy/pasting code (also have no Mac to test on =/

neins commented 5 years ago

Hello, i have same issue with iTerm and i would like to know how to use your solution? Where to use this code?