Applescript for iTerm (and a request for help with it) #
iTerm is a major part of my workspace. I’m a big fan of tabbed interfaces, and my fingers have gotten really good at the Apple-← and Apple-→ key combos to swap between them. I usually have three tabs going:
- One that runs Unison in batch mode to keep my mac and BSD machines in sync. I set the title of this one to “unisoner”.
- A second logged into my BSD machine in the package folder to easily relink the site package when I add files to it. This is the “relinker”. (Also, this one occasionally is used to
tail -f
on the apache log file.) - A third sitting in my code folder to do CVS updates, open files, grep for stuff, etc. This is “cvser”.
Of course, it’s a pain to go through the steps to set all this up. So, I wrote this Applescript, and it does it all:
tell Application "iTerm"
activate
set myterm to (make new terminal)
tell myterm
if (count of sessions) < 3 then
my open_tab("title unisoner
cd dev/orion
unisonpush
unisondev")
my open_tab("v
yroot orion_front
title relinker-tailer
cd dev/orion/front/package
yapl")
my open_tab("title cvser
cd dev/orion
open -a TextMate orion.tmproj")
end if
end tell
terminate first session of current terminal
end tell
on open_tab(command)
tell Application "iTerm" to tell first terminal
launch session "Default Session"
tell last session
write text command
end tell
end tell
end run_command
The only problem is, it requests a password when it ssh'es into my dev box and changes to the project root. So, that whole piece falls down.
Anyone out there in geek land know of a way to make an Applescript respond to things like that? Something like this:
whenever the last line is "Password:" then
write text returned of (display dialog "Password:" default answer "")
end if
Update:
Turns out, what I suggested is pretty close to what works. However, since there’s no event to listen for that would tell the script that the display has been updated, it takes a bit of fudging, and doesn’t always work perfectly. Basically, you put @@@PASSWORD@@@
in the list of commands where you may expect a password prompt, and it’ll wait for a second and display a dialog if necessary.
on open_tab(command)
set commands to paragraphs in command
tell Application "iTerm" to tell first terminal
launch session "Default Session"
repeat with currentCommand in commands
if (text of currentCommand = "@@@PASSWORD@@@") then
do shell script "sleep 1"
set pw to last word of (get contents of last session)
if pw = "Password" then
tell Application "Finder"
activate
set pw to text returned of (display dialog "Password:" default answer "" with hidden answer)
end tell
tell Application "iTerm" to activate
tell last session to write text pw
end if
else
tell last session to write text currentCommand
end if
end repeat
end tell
end open_tab
Get the finished product: Applescript for iTerm. You’ll want to change up the specific commands up near the top, of course.