6

When I login to my Mac, I want to automatically open a new Terminal.app window with a certain profile, say Monitor, that has a startup command and several appearance settings. I thought it would be very easy to do this from AppleScript or command-line (via osascript), so I can add a script to my login items, but it turns out this is not so trivial after several tries.

My current best way is the following AppleScript:

tell application "Terminal"
    set origSettings to default settings
    set default settings to settings set "Monitor"
    activate
    tell application "System Events" to keystroke "n" using command down
    set default settings to origSettings
end tell

However, I want to know if there's a simpler way to do this without relying on UI scripting (with System Events) or messing around with my Terminal's default settings.

applescript (osascript) for changing Terminal.app profile seemed promising, but it's for when there's already a Terminal window open. I have no idea how to create a new Terminal tab or window with its current settings specified. The do script command, which does create a new window, unfortunately ignores the startup command set in the profile, making me fall back to UI scripting.

I also found this script: http://blog.azizlight.me/having-fun-with-applescript-and-terminal-app/ which again relies on UI scripting. Because some of my machines use non-English locale, I prefer sticking to keystrokes rather than menu labels.

2 Answers 2

2

Possible I am missing the point, but can't you Export the profile as a .terminal file and add that to your Login Items?

Export:

enter image description here

Add the exported file to Login Items:

enter image description here

3
  • 1
    Awesome, @beroe! I remember opening a .terminal file used to create duplicate profile entries in the past, but apparently, such behavior has changed at least in the latest version of OS X, 10.8 Mavericks. Although this doesn't answer how to achieve the thing in AppleScript, I'm picking this as the answer because it actually solves my original problem. Thanks!
    – netj
    Sep 6, 2014 at 6:43
  • Glad it helped. I think the AppleScript issue will be best addressed by the other answer linked in the comments.
    – beroe
    Sep 6, 2014 at 13:08
  • In my experience today this solution does create duplicate profile entires. I wish I knew a way around that, but I do not. Feb 7, 2015 at 20:23
4

Can you just run the startup command with do script?

tell application "Terminal"
    do script "emacs"
    set current settings of selected tab of window 1 to settings set "Grass"
end tell

If someone else doesn't need to run any startup command, you can use do script "" to open a new window:

tell application "Terminal"
    do script ""
    set current settings of selected tab of window 1 to settings set "Grass"
end tell

Or you could also use do script "" when changing default settings:

tell application "Terminal"
    set old to default settings
    set default settings to settings set "Grass"
    do script ""
    set default settings to old
    activate
end tell
2
  • The problem with do script "" is that it ignores the startup command already entered in the profile. BTW because do script returns the tab it created, we could further shorten to something like: set newTab to do script ... then set current settings of newTab to ...
    – netj
    Mar 4, 2014 at 1:41
  • temporarily swapping the default settings works great for setting the window title so that it can be detected by a window-manager like amethyst!
    – Mark
    Apr 19 at 2:10

You must log in to answer this question.

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