Determining active program from AppleScript

64 views
Skip to first unread message

Mat Schaffer

unread,
Mar 8, 2012, 10:42:55 AM3/8/12
to iterm2-...@googlegroups.com
Hello all,

I wrote a vim/iTerm2 helper script that lets me use iTerm 2 in place of screen or tmux to interact with a REPL or shell or whatever: https://github.com/matschaffer/vim-islime2

But I'd be especially happy if there were a way for certain actions to verify that they're sending input to the right program (bash for example) rather than something I accidentally left open on the other panel (e.g., less). Does anyone know if there's an AppleScript or other solution I could use to find out what is the active process in a panel?

I think this may also be useful for another goal I have: mapping Cmd+c to "*y if vim is the active process (and paste too of course)

Thanks for all the hard work on iTerm2 so far. Undoubtedly the most capable terminal emulator I've ever seen!

-Mat

George Nachman

unread,
Mar 8, 2012, 2:46:13 PM3/8/12
to iterm2-...@googlegroups.com
This could be a possible use of scripting in a future iTerm2 that has scripting built in. At the moment we do figure out the process name to display in the title bar but it's not exposed.

Mat Schaffer

unread,
Mar 8, 2012, 3:44:48 PM3/8/12
to iterm2-...@googlegroups.com
Guess I'll keep my fingers crossed in the mean time ;)

Jeremy Volkman

unread,
Mar 8, 2012, 9:11:22 PM3/8/12
to iterm2-...@googlegroups.com
Well hello there. 

-Jeremy

Tom Feist

unread,
Mar 9, 2012, 2:06:21 PM3/9/12
to iterm2-...@googlegroups.com
If you don't mind cheating a bit, it's possible with the current Applescript capapbilities as well.
The trick is to use AS to get the name of the pty used by each open window/tab, and then read/parse
the output of 'ps' to match up programs with controlling ttys.

http://pastie.org/3559094 has a quick & dirty python script I knocked together to demonstrate it, using
the 'appscript' scripting bridge, because I really *really* hate writing applescript. You could probably
translate it pretty easily though, it's something like:

tell app "iTerm"
get tty of every session of every terminal
-- save it somewhere/do something with it.
end tell

The bit I didn't immediately find an answer to was which of the programs running on a given tty is the active/foreground
one? Basic logic suggests it should be the most recent/one with the highest PID, but if you C-z that and switch back to another,
it could get confused. There might be some other way to do it that I'm not aware of.

A less/more hacky approach would be to use/emulate a preexec style hook for your shell, which writes to a file in /tmp or whatever
the name of the thing you're about to execute. The postexec/prompt_command hook then removes the flle.

zsh has support for preexec functions built in, and there's http://www.twistedmatrix.com/users/glyph/preexec.bash.txt for bash
which emulates it via debug traps.

Hope some of this helps,

Tom.

Mat Schaffer

unread,
Mar 10, 2012, 8:26:06 AM3/10/12
to iterm2-...@googlegroups.com
This all sounds very promising, Tom. I'll post an update once I get a chance to hack on this a bit more.

And thanks!
Mat
Reply all
Reply to author
Forward
0 new messages