I'm writing a (Bash) script to automate setting up my Mac environment and would like to do this, except using the command line only. I'd also like to make that theme the default.

I have a .terminal file exported and all ready to go, I just can't figure out how to do this programmatically. Is there a documentation for this somewhere that I'm missing?

... or would these be easier using AppleScript? Never done that before though.

  • Why do you want to make this setting from a shell and not directly from within Terminal? – dan Aug 15 '13 at 18:56
  • 1
    @danielAzuelos So I can automate the process. – Matt Aug 15 '13 at 20:15
  • 1
    How do you intend to run this shell script? From a Terminal? – dan Aug 15 '13 at 21:33
  • Well, from a shell at least. If I had to do it from a Terminal, hopefully this automation script is the only one I'd have to run in the default Terminal settings. – Matt Aug 16 '13 at 4:35
  • → Matt: do you have to do this configuration more than once (i.e. on more than one account or on more than one computer)? – dan Aug 16 '13 at 6:49

Use the open command, followed by the preference change:

open ~/MyTerminalSettings.terminal
defaults write com.apple.terminal "Default Window Settings" "MyTerminalSettings"

The open command will further open a new Terminal window, but the script will continue running in the original window, and you can close the new window when convenient.

| improve this answer | |
  • That opens a terminal window which might get in the way (though, perhaps that can be stifled) -- and it does seem to import the theme... but how would I set it as the default theme? – Matt Aug 15 '13 at 19:33

This isn't an answer but the basic bricks you will have to use to write your shell script.

  1. Check that Terminal isn't running to avoid corrupting its plist:

    if ps ax | grep '[/]Terminal' >/dev/null 2>&1 ; then
        echo Terminal running! >&2 && exit 2
    # go ahead

    beware to the [/] which is here to avoid to grep your grep command which will be always successfull.

  2. Convert your Terminal plist to a text file with:

    plutil -convert xml1 ~/Library/Preferences/com.apple.Terminal.plist
  3. Strip your .terminal file from its header, let's says its name is imported.terminal

    sed -n '/<dict>/,/<\/dict>/p' imported.terminal >tmp.terminal
  4. Insert this tmp.terminal plist within the ~/Library/Preferences/com.apple.Terminal.plist inside the right dictionary.

    This will need some context analysis to do it right. I'd chose perl if I had to do it, but awk, ex, or sed might be easier with a few lines and testing.

| improve this answer | |
  • 2
    Might be kind of tricky to not have Terminal running while you have a script running in Terminal :-) – nohillside Aug 15 '13 at 18:20
  • I thought Matt wanted to do this from a shell and not directly from a Terminal because he is not doing this from a Terminal (for example through a ssh). But this should be clarified. – dan Aug 15 '13 at 18:40
  • Either way. Whichever is easier. – Matt Aug 16 '13 at 4:35
  • 1
    As Patrix noted it, one path will be hard. One can't easily modify Terminal plist if the shell doing the task is forked from this same Terminal program. When Terminal will terminate, it will overwrite all the modification done thus far. This was the reason for my suggested step 1. – dan Aug 16 '13 at 6:54
  • True, okay, understood. – Matt Aug 16 '13 at 13:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .