-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (75 loc) · 2.54 KB
/
index.js
File metadata and controls
86 lines (75 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os from 'node:os';
import alphaSort from 'alpha-sort';
import bplist from 'bplist-parser';
import {$} from 'execa';
import ow from 'ow';
import psList from 'ps-list';
import {runAppleScript} from 'run-applescript';
/**
* @returns {Promise<string>} - The name of the default profile
*/
export async function getTerminalDefaultProfile() {
if (await isTerminalRunning()) {
return runAppleScript(
'tell application "Terminal" to get name of default settings',
);
}
const {stdout} =
await $`defaults read com.apple.Terminal Default\ Window\ Settings`;
return stdout.trim();
}
/**
* @returns {Promise<string[]>} - List of installed profiles
*/
export async function getTerminalProfiles() {
const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`;
const terminalPreferences = await bplist.parseFile(terminalPlistPath);
return Object.keys(terminalPreferences[0]['Window Settings']).sort(
alphaSort({caseInsensitive: true, natural: true}),
);
}
/**
* @returns {Promise<boolean>} - Whether Terminal is currently running
*/
export async function isTerminalRunning() {
const processes = await psList();
return processes.some((process) => process.name === 'Terminal');
}
/**
* Set the default Terminal profile for new windows/tabs
*
* @param {string} profile - Profile name, e.g. 'Clear Dark'
* @return {Promise<void>}
*/
export async function setTerminalDefaultProfile(profile) {
ow(profile, ow.string.oneOf(await getTerminalProfiles()));
if (await isTerminalRunning()) {
await runAppleScript(`tell application "Terminal"
set default settings to settings set "${profile}"
end tell`);
} else {
await $`defaults write com.apple.Terminal Default\ Window\ Settings -string ${profile}`;
await $`defaults write com.apple.Terminal Startup\ Window\ Settings -string ${profile}`;
}
}
/**
* Update all open Terminal tabs to use the given profile
*
* @param {object} parameters
* @param {string} parameters.profile - Profile name, e.g. 'Clear
* Dark'
* @param {boolean} [parameters.setDefault] - Whether to also make the
* profile the default
* @return {Promise<void>}
*/
export async function setTerminalProfile({profile, setDefault}) {
ow(profile, ow.string.oneOf(await getTerminalProfiles()));
if (await isTerminalRunning()) {
await runAppleScript(`tell application "Terminal"
set current settings of tabs of windows to settings set "${profile}"
end tell`);
}
if (setDefault) {
await setTerminalDefaultProfile(profile);
}
}