···11+import {execFile} from 'node:child_process';
22+import {promisify} from 'node:util';
33+import {runAppleScript} from 'run-applescript';
44+import isTerminalRunning from './is-terminal-running.js';
55+66+const execute = promisify(execFile);
77+88+/**
99+ * @returns {Promise<string>} - The name of the default profile
1010+ */
1111+export default async function getTerminalDefaultProfile() {
1212+ if (await isTerminalRunning()) {
1313+ return runAppleScript(
1414+ 'tell application "Terminal" to get name of default settings',
1515+ );
1616+ }
1717+1818+ const {stdout} = await execute('defaults', [
1919+ 'read',
2020+ 'com.apple.Terminal',
2121+ 'Default Window Settings',
2222+ ]);
2323+2424+ return stdout.trim();
2525+}
+14
source/get-terminal-profiles.js
···11+import os from 'node:os';
22+import bplist from 'bplist-parser';
33+44+/**
55+ * @returns {Promise<string[]>} - List of installed profiles
66+ */
77+export default async function getTerminalProfiles() {
88+ const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`;
99+ const terminalPreferences = await bplist.parseFile(terminalPlistPath);
1010+1111+ return Object.keys(terminalPreferences[0]['Window Settings']).toSorted(
1212+ new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}).compare,
1313+ );
1414+}
+5
source/index.js
···11+export {default as getTerminalDefaultProfile} from './get-terminal-default-profile.js';
22+export {default as getTerminalProfiles} from './get-terminal-profiles.js';
33+export {default as isTerminalRunning} from './is-terminal-running.js';
44+export {default as setTerminalDefaultProfile} from './set-terminal-default-profile.js';
55+export {default as setTerminalProfile} from './set-terminal-profile.js';
···11+import {execFile} from 'node:child_process';
22+import {promisify} from 'node:util';
33+import {runAppleScript} from 'run-applescript';
44+import isTerminalRunning from './is-terminal-running.js';
55+66+const execute = promisify(execFile);
77+88+/**
99+ * Set the default Terminal profile for new windows/tabs
1010+ *
1111+ * @param {string} profile - Profile name, e.g. 'Clear Dark'
1212+ * @return {Promise<void>}
1313+ */
1414+export default async function setTerminalDefaultProfile(profile) {
1515+ if (await isTerminalRunning()) {
1616+ await runAppleScript(`tell application "Terminal"
1717+ set default settings to settings set "${profile}"
1818+end tell`);
1919+ } else {
2020+ await execute('defaults', [
2121+ 'write',
2222+ 'com.apple.Terminal',
2323+ 'Default Window Settings',
2424+ '-string',
2525+ profile,
2626+ ]);
2727+ await execute('defaults', [
2828+ 'write',
2929+ 'com.apple.Terminal',
3030+ 'Startup Window Settings',
3131+ '-string',
3232+ profile,
3333+ ]);
3434+ }
3535+}
+25
source/set-terminal-profile.js
···11+import {runAppleScript} from 'run-applescript';
22+import isTerminalRunning from './is-terminal-running.js';
33+import setTerminalDefaultProfile from './set-terminal-default-profile.js';
44+55+/**
66+ * Update all open Terminal tabs to use the given profile
77+ *
88+ * @param {object} parameters
99+ * @param {string} parameters.profile - Profile name, e.g. 'Clear
1010+ * Dark'
1111+ * @param {boolean} [parameters.setDefault] - Whether to also make the
1212+ * profile the default
1313+ * @return {Promise<void>}
1414+ */
1515+export default async function setTerminalProfile({profile, setDefault}) {
1616+ if (await isTerminalRunning()) {
1717+ await runAppleScript(`tell application "Terminal"
1818+ set current settings of tabs of windows to settings set "${profile}"
1919+end tell`);
2020+ }
2121+2222+ if (setDefault) {
2323+ await setTerminalDefaultProfile(profile);
2424+ }
2525+}