Beyond the good ol' LaunchAgents - 20 - Terminal Preferences

This is part 20 in the series of “Beyond the good ol’ LaunchAgents”, where I try to collect various persistence techniques for macOS. For more background check the introduction.

This is another application specific persistence method, related to the Terminal application.

In the Terminal Preferences, under the Profiles tab, we can set a command that will be executed upon Terminal’s startup. This is shown in the screen below.

Terminal Profile Settings

This is stored inside Terminal’s preferences PLIST file, located at ~/Library/Preferences/com.apple.Terminal.plist. Normally this is a binary PLIST, and thus we can use plutil to display it in a human readable form.

csaby@mac Preferences % plutil -p com.apple.Terminal.plist| less
{
  "Default Window Settings" => "Basic"
  "DefaultProfilesVersion" => 1
  "HasMigratedDefaults" => 1
  "Man Page Window Settings" => "Man Page"
  "NSNavLastRootDirectory" => "~/Documents"
  "NSNavLastUserSetHideExtensionButtonState" => 0
  "NSWindow Frame NSNavPanelAutosaveName" => "732 711 328 167 0 0 1792 1095 "
  "NSWindow Frame TTAppPreferences" => "563 437 668 554 0 0 1792 1095 "
  "NSWindow Frame TTWindow" => "132 206 1522 805 0 0 1792 1095 "
  "NSWindow Frame TTWindow Basic" => "132 178 1522 833 0 0 1792 1095 "
  "NSWindow Frame TTWindow Man Page" => "59 152 570 707 0 0 1792 1095 "
  "NSWindow Frame TTWindow test" => "61 555 934 561 0 0 1920 1177 "
  "NSWindow Frame TTWindow test 1" => "76 310 585 365 0 0 1792 1097 "
  "ProfileCurrentVersion" => 2.07
  "SecureKeyboardEntry" => 0
  "Shell" => ""
  "Startup Window Settings" => "Basic"
  "TTAppPreferences Selected Tab" => 1
  "Window Settings" => {
    "Basic" => {
      "CommandString" => "touch /Users/Shared/executed.txt;"
...

As we can see above, the setting is stored under the “Window Settings” -> Basic (which is the name of the profile) -> “CommandString”. The Startup profile is set under “Startup Window Settings”. Which is important, because the command associated with the Startup profile will be executed.

In XML it looks like the following:

csaby@mac Preferences % plutil -convert xml1 com.apple.Terminal.plist -o -| less 
...
        <key>Startup Window Settings</key>
        <string>Basic</string>
...
        <key>Window Settings</key>
        <dict>
                <key>Basic</key>
                <dict>
                        <key>CommandString</key>
                        <string>touch /Users/Shared/executed.txt;</string>
...

If we are not sandboxed, we can write to this file, and whenever a user starts Terminal our command will be executed.