5

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.

8
  • Why do you want to make this setting from a shell and not directly from within Terminal?
    – athena
    Commented Aug 15, 2013 at 18:56
  • 1
    @danielAzuelos So I can automate the process.
    – Matt
    Commented Aug 15, 2013 at 20:15
  • 1
    How do you intend to run this shell script? From a Terminal?
    – athena
    Commented Aug 15, 2013 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
    Commented Aug 16, 2013 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)?
    – athena
    Commented Aug 16, 2013 at 6:49

2 Answers 2

2

Use the open command, followed by the preference change:

open ~/MyTerminalSettings.terminal
defaults write com.apple.terminal "Default Window Settings" -string "MyTerminalSettings"
defaults write com.apple.Terminal "Startup Window Settings" -string "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.

0
0

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
    fi
    # 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.

6
  • 3
    Might be kind of tricky to not have Terminal running while you have a script running in Terminal :-)
    – nohillside
    Commented Aug 15, 2013 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.
    – athena
    Commented Aug 15, 2013 at 18:40
  • Either way. Whichever is easier.
    – Matt
    Commented Aug 16, 2013 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.
    – athena
    Commented Aug 16, 2013 at 6:54
  • True, okay, understood.
    – Matt
    Commented Aug 16, 2013 at 13:51

You must log in to answer this question.

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