3

Ok... I've been searching for an answer to this for a while and can't seem to find any good examples, so I thought I'd break down and ask.

How can I create a shell file (.command) in OSX that I can just double-click on which:

  • Opens a new Terminal window
  • Runs a few commands
  • ... and stays active so I can continue to run other things!

My goal is to setup various environments using individual .command files, which will each set variables and run certain command line tools, and then remain open to manually run other commands. I currently have one like this:

#!/bin/sh

export MY_VAR_A="blah A"
export MY_VAR_B="blah B"

cd /Users/

... and this doesn't work. It just opens a Terminal window with this output:

Last login: Sat Aug 17 12:52:15 on ttys000
unknown60c5470527e4:~ me$ /Users/me/Documents/test.command ; exit;
logout

[Process completed]

Is there a better (or just different) way of accomplishing what I want? Or do I just need to adjust something simple in my current .command file?

indiv
  • 17,306
  • 6
  • 61
  • 82

1 Answers1

3

Use applescript

tell application "Terminal" to activate
tell application "Terminal"
    do script ("ls -l") in window 1
    do script ("cd /Users/test/Music/iTunes/") in window 1
    do script ("ls -l") in window 1
end tell

Save apple script as application bundle.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • Thanks... although this has some weird aspects to it. It opens up two new Terminal windows, which are both hidden by default and need to be clicked on in the launch bar to be displayed. Any ideas how to fix that? (This is my first time messing with AppleScript) –  Aug 17 '13 at 17:32
  • tell application "Terminal" to activate tell application "Terminal" do script ("ls -l") in window 1 do script ("cd /Users/test/Music/iTunes/") in window 1 do script ("ls -l") in window 1 end tell – Parag Bafna Aug 17 '13 at 17:40
  • Thanks Parag! While this solution does provide the desired result... it's more of a management headache than I would like. I plan to have quite a few of these files, and will need to edit them frequently. This means I'd need to keep around the script and App for each one, open, edit, re-export, etc with each change... as opposed to just text-edit and save. If you know of a way to use the .command files, I'd prefer that method. –  Aug 17 '13 at 18:37
  • ... plus these script files and apps don't lend themselves well to source control and diffs. –  Aug 17 '13 at 18:40