···7788## [Unreleased](https://github.com/patrik-csak/mac-terminal/compare/v4.0.0...HEAD)
991010+### Added
1111+1212+- [`getTerminalDefaultProfile()`](readme.md#getterminaldefaultprofile)
1313+1014### Changed
11151216- **BREAKING**: `setTerminalProfile()` only tries to update Terminal windows if Terminal is running
+36-21
index.js
···77import {runAppleScript} from 'run-applescript';
8899/**
1010- * @returns {Promise<string[]>} - List of installed Terminal.app profiles
1010+ * @returns {Promise<string>} - The name of the default profile
1111+ */
1212+export async function getTerminalDefaultProfile() {
1313+ if (await isTerminalRunning()) {
1414+ return runAppleScript(
1515+ 'tell application "Terminal" to get name of default settings',
1616+ );
1717+ }
1818+1919+ const {stdout} =
2020+ await $`defaults read com.apple.Terminal Default\ Window\ Settings`;
2121+ return stdout.trim();
2222+}
2323+2424+/**
2525+ * @returns {Promise<string[]>} - List of installed profiles
1126 */
1227export async function getTerminalProfiles() {
1328 const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`;
···1934}
20352136/**
2222- * @returns {Promise<boolean>} - Whether Terminal.app is currently running
3737+ * @returns {Promise<boolean>} - Whether Terminal is currently running
2338 */
2439export async function isTerminalRunning() {
2540 const processes = await psList();
···2843}
29443045/**
3131- * Update all open Terminal.app tabs to use the given profile
4646+ * Set the default Terminal profile for new windows/tabs
3247 *
3333- * @param {object} parameters
3434- * @param {string} parameters.profile - Terminal.app profile name, e.g. 'Clear
3535- * Dark'
3636- * @param {boolean} [parameters.setDefault] - Whether to also make the
3737- * profile the default
4848+ * @param {string} profile - Profile name, e.g. 'Clear Dark'
3849 * @return {Promise<void>}
3950 */
4040-export async function setTerminalProfile({profile, setDefault}) {
5151+export async function setTerminalDefaultProfile(profile) {
4152 ow(profile, ow.string.oneOf(await getTerminalProfiles()));
42534354 if (await isTerminalRunning()) {
4455 await runAppleScript(`tell application "Terminal"
4545- set current settings of tabs of windows to settings set "${profile}"
5656+ set default settings to settings set "${profile}"
4657end tell`);
4747- }
4848-4949- if (setDefault) {
5050- await setTerminalDefaultProfile(profile);
5858+ } else {
5959+ await $`defaults write com.apple.Terminal Default\ Window\ Settings -string ${profile}`;
6060+ await $`defaults write com.apple.Terminal Startup\ Window\ Settings -string ${profile}`;
5161 }
5262}
53635464/**
5555- * Set the default Terminal.app profile for new windows/tabs
6565+ * Update all open Terminal tabs to use the given profile
5666 *
5757- * @param {string} profile - Terminal.app profile name, e.g. 'Clear Dark'
6767+ * @param {object} parameters
6868+ * @param {string} parameters.profile - Profile name, e.g. 'Clear
6969+ * Dark'
7070+ * @param {boolean} [parameters.setDefault] - Whether to also make the
7171+ * profile the default
5872 * @return {Promise<void>}
5973 */
6060-export async function setTerminalDefaultProfile(profile) {
7474+export async function setTerminalProfile({profile, setDefault}) {
6175 ow(profile, ow.string.oneOf(await getTerminalProfiles()));
62766377 if (await isTerminalRunning()) {
6478 await runAppleScript(`tell application "Terminal"
6565- set default settings to settings set "${profile}"
7979+ set current settings of tabs of windows to settings set "${profile}"
6680end tell`);
6767- } else {
6868- await $`defaults write com.apple.Terminal Default\ Window\ Settings -string ${profile}`;
6969- await $`defaults write com.apple.Terminal Startup\ Window\ Settings -string ${profile}`;
8181+ }
8282+8383+ if (setDefault) {
8484+ await setTerminalDefaultProfile(profile);
7085 }
7186}
+32-22
readme.md
···10101111## API
12121313+### `getTerminalDefaultProfile()`
1414+1515+```typescript
1616+function getTerminalDefaultProfile(): Promise<string>;
1717+```
1818+1919+#### Example
2020+2121+```javascript
2222+import {getTerminalDefaultProfile} from 'mac-terminal';
2323+2424+await getTerminalDefaultProfile(); // 'Clear Dark'
2525+```
2626+1327### `getTerminalProfiles()`
14281529```typescript
···20342135#### Example
22362323-```js
3737+```javascript
2438import {getTerminalProfiles} from 'mac-terminal';
25392640await getTerminalProfiles(); // ['Basic', 'Clear Dark', 'Clear Light', ...]
···4256await isTerminalRunning(); // true
4357```
44585959+### `setTerminalDefaultProfile()`
6060+6161+```typescript
6262+function setTerminalDefaultProfile(profile: string): Promise<void>;
6363+```
6464+6565+Set the default Terminal profile for new windows / tabs
6666+6767+#### Example
6868+6969+```javascript
7070+import {setTerminalDefaultProfile} from 'mac-terminal';
7171+7272+await setTerminalDefaultProfile('Clear Dark');
7373+```
7474+4575### `setTerminalProfile()`
46764777```typescript
···6696});
6797```
68986969-### `setTerminalDefaultProfile()`
7070-7171-```typescript
7272-function setTerminalDefaultProfile(profile: string): Promise<void>;
7373-```
7474-7575-Set the default Terminal profile for new windows / tabs
7676-7777-#### Example
7878-7979-```javascript
8080-import {setTerminalDefaultProfile} from 'mac-terminal';
8181-8282-await setTerminalDefaultProfile('Clear Dark');
8383-```
8484-8599## Related
861008787-- [auto-terminal-profile](https://github.com/patrik-csak/auto-terminal-profile) – Automatically switch macOS Terminal’s profile when the system-wide dark / light appearance mode changes
8888-8989-## Acknowledgements
9090-9191-Thanks to [Jimmy Bosse](https://github.com/jbosse) for his [Stack Overflow answer](https://stackoverflow.com/a/66080297/4411309)
101101+- [auto-terminal-profile](https://github.com/patrik-csak/auto-terminal-profile) – Automatically switch Terminal profiles when macOS dark/light mode changes