3

I want to get the output of a shell command run through iTerm2's applescript into an applescript variable. I tried the following but it does not work:

tell application "iTerm"
  activate
  try
    set _session to current session of current terminal
  on error
    say "Error"
  end try
  tell _session
    set Directory to write text "pwd"
    get Directory
    set Directory to exec command "pwd"
    get Directory
  end tell
end tell

Thanks for your help!

0

you could try this.

tell application "iTerm"
    activate
    try
        set _session to current session of current tab of current window
    on error
        say "Error"
    end try
    tell _session
        write text "pwd"
        set terminalContent to text of _session
    end tell
end tell
set output to paragraph -2 of trim(true, terminalContent)

on trim(theseCharacters, someText)
    if class of someText is text and length of someText > 0 then
        -- default values (all whitespace)
        if theseCharacters is true then ¬
            set theseCharacters to {" ", tab, ASCII character 10, return, ASCII character 0}

        repeat until first character of someText is not in theseCharacters
            set someText to text 2 thru -1 of someText
        end repeat

        repeat until last character of someText is not in theseCharacters
            set someText to text 1 thru -2 of someText
        end repeat
    end if

    return someText
end trim

This will return output of command that you specify in write text in line number 9 of above code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.