MacOS – Opening new Terminal.app window/tab with a certain profile from command-line or AppleScript

applescriptcommand linemacosterminal

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.

Best Answer

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