Maybe this exists already – I couldn’t find it, so I just wrote it. Of course, I am putting this here as code, but one day I’ll publish a new bundle of TextMate commands which includes this and some of the other ones I’ve put together. I was looking around today for some commands that I might use to augment the Perforce bundle — specifically, I needed a dialog. What I found was some references to the use of CocoaDialog, which is pretty handy. Another use I had envisioned for dialogs in a TextMate command was to be able to run terminal commands *in* the Terminal. After all, sometimes you would like to have something run and dump its output right there in the Terminal, rather than in a new TextMate document or something like that. The Apple Terminal is pretty nice for what it is, and its much better than the Linux KDE terminal, the Windows terminal, and the poor bastard Windows Cygwin terminal. To set up this in TextMate, open the Bundle Editor and create a new Command. Set the command to Save “Nothing”, Input “None”, and Output “Discard”. Give it a key command if you like, and leave the Scope Selector blank. In the Command(s) field, paste the following:
#!/bin/bash # First use CocoaDialog to pop a dialog to get # the command you would like to run. res=$(CocoaDialog inputbox --title "Run Command in Terminal Window" --informative-text "Enter the command here:" --button1 "Okay" --button2 "Cancel") # if the cancel button was pressed then exit [[ $(head -n1 <<<"$res") == "2" ]] && exit_discard # set the command string to what was entered command=$(tail -n1 <<<"$res") # create our applescript which will launch a # new Terminal window if there is not one running # and otherwise run the command we want in the # current running Terminal window aplscr="tell application \"System Events\" to set terminalRunning to (exists process \"Terminal\") if (not terminalRunning) then tell application \"Terminal\" to activate tell application \"Terminal\" to do script \"$command\" else tell application \"Terminal\" to activate tell application \"Terminal\" to do script \"$command\" in front window end if" # execute the applescript osascript -e "$aplscr" &>/dev/null &
In a nutshell, as the comments indicate, we capture the contents of the field in the dialog that is opened, and stick them into the applescript that we want to run. The applescript part checks to see if there is already a Terminal session window open, and will run the command in the Terminal. If Terminal isn’t open, then it launches it. You (or maybe me in the future) could fancy this up a bit if you wanted by having the Terminal change directory to the directory in which the file you are working on exists. Not sure that is always desirable though for my purposes.
Enjoy it. (if you’re a total geek you probably will, at least in principle)
Additional work needed: 1. Escape quotes (you have to do that manually now). 2. Have Terminal spawn a new window if it is already running but has no open windows. (need to check for window count in addition to process running)