How to exec command(s) in iTerm with AppleScript "write text" and wait for completion

2,215 views
Skip to first unread message

Gianni Costanzi

unread,
Jan 27, 2013, 2:57:22 AM1/27/13
to iterm2-...@googlegroups.com
Hi,
I've seen some examples of using AppleScript iTerm extensions and I have the following problem: I'm able to activate a session in an iTerm terminal (checking if it already exists etc) and then I've used <write text "shell command to execute"> in AppleScript to launch a command within the terminal session; the command executes and I can see its output within the session. The problem is that I have a repeat cycle within my script that repeatedly execs the write text command within the session, and I can see that the command is printed in the iTerm session twice or more (one for each repeat loop) but it is executed only the first time. Maybe the problem is that I write the second command before the first command has been completed. So, I'd like to know if one of the two things can be done:
- how to execute multiple commands with multiple write text commands, without knowing if command N has been completed when I write the command N+1 and so on..
- ho to execute a command within the session (I need to see the output, so I'd like to use something like write text) and block the calling AppleScript until he command has been completed

If it's not clear enough, I'll try to post a piece of code next days (at present time I'm in a hurry, so I can't.

Thank you in advance for any help
   Gianni Costanzi

--
--------------------------------------------------------------------------------------------------------------
|  Gianni Costanzi (gianni....@gmail.com)
|       http://it.linkedin.com/in/giannicostanzi
|       http://utenti.multimania.it/giannicostanzi/my-projects.html
|
|  My Fotoblog:
|       http://giannicostanzi.fotoblog.it
--------------------------------------------------------------------------------------------------------------

George Nachman

unread,
Jan 29, 2013, 12:48:12 AM1/29/13
to iterm2-...@googlegroups.com
Can you put the commands in a shell script and have applescript run that instead?


--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Gianni Costanzi

unread,
Jan 30, 2013, 3:34:28 AM1/30/13
to iterm2-...@googlegroups.com
Yes, that is what I'm doing at present time, but I'd like to see their output printed within an iTerm session, to see the progress of the command.

    Gianni

George Nachman

unread,
Feb 3, 2013, 1:13:31 AM2/3/13
to iterm2-...@googlegroups.com
Disclaimer: I am allergic to applescript.

The code below seems to work as far as running two commands; I assume you're doing something similar, so I'm not sure why it wouldn't work for you. Does this run two echo commands for you? Getting applescript to see command output would be challenging without doing some really crazy things, though.

tell application "iTerm"
    activate

    try
        set _session to current session of current terminal
    on error
        set _term to (make new terminal)
        tell _term
            launch session "Default"
            set _session to current session
        end tell
    end try

    tell _session
        write text "echo hello"
        write text "echo world"
    end tell
end tell


Reply all
Reply to author
Forward
0 new messages