Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get iTerm location Applescript

Tags:

applescript

I would like to know how to get the current location of the current session in iTerm using Applescript.

I have tried all sorts of things. The dictionary is very difficult for me to understand.

Here is my latest attempt

tell application "iTerm"
        tell the current session of current terminal
            set theSelection to attachment file name
            display dialog theSelection
        end tell

    end tell

And the error this produces is:

error "iTerm got an error: Can’t get file name of current session of current terminal." number -1728 from file name of current session of current terminal

I just want the location the iTerm session is currently in: /Users/SuperCoolGuy/Desktop

like image 385
Mike Fielden Avatar asked Sep 12 '25 07:09

Mike Fielden


2 Answers

This is ugly...

tell application "iTerm"
    if not (exists terminal 1) then reopen
    set mySession to current terminal's current session
    tell mySession
        write text "pwd"
        set sessionContents to it's text
    end tell
end tell

set myPath to paragraph -2 of (do shell script "grep . <<< " & quoted form of sessionContents)
like image 109
adayzdone Avatar answered Sep 16 '25 08:09

adayzdone


If somebody stumbles upon this question, there's another option now with Shell integration. With it you can access the path in AppleScript using the iTerm defined variable session.path:

activate application "iTerm"
tell application "iTerm"
    tell current session of current window
        set myTerm to (variable named "session.path")
        write text "echo this is the current path: " & myTerm
    end tell
end tell
like image 20
LeZuse Avatar answered Sep 16 '25 08:09

LeZuse