this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add getDefaultProfile()

+72 -43
+4
changelog.md
··· 7 7 8 8 ## [Unreleased](https://github.com/patrik-csak/mac-terminal/compare/v4.0.0...HEAD) 9 9 10 + ### Added 11 + 12 + - [`getTerminalDefaultProfile()`](readme.md#getterminaldefaultprofile) 13 + 10 14 ### Changed 11 15 12 16 - **BREAKING**: `setTerminalProfile()` only tries to update Terminal windows if Terminal is running
+36 -21
index.js
··· 7 7 import {runAppleScript} from 'run-applescript'; 8 8 9 9 /** 10 - * @returns {Promise<string[]>} - List of installed Terminal.app profiles 10 + * @returns {Promise<string>} - The name of the default profile 11 + */ 12 + export async function getTerminalDefaultProfile() { 13 + if (await isTerminalRunning()) { 14 + return runAppleScript( 15 + 'tell application "Terminal" to get name of default settings', 16 + ); 17 + } 18 + 19 + const {stdout} = 20 + await $`defaults read com.apple.Terminal Default\ Window\ Settings`; 21 + return stdout.trim(); 22 + } 23 + 24 + /** 25 + * @returns {Promise<string[]>} - List of installed profiles 11 26 */ 12 27 export async function getTerminalProfiles() { 13 28 const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`; ··· 19 34 } 20 35 21 36 /** 22 - * @returns {Promise<boolean>} - Whether Terminal.app is currently running 37 + * @returns {Promise<boolean>} - Whether Terminal is currently running 23 38 */ 24 39 export async function isTerminalRunning() { 25 40 const processes = await psList(); ··· 28 43 } 29 44 30 45 /** 31 - * Update all open Terminal.app tabs to use the given profile 46 + * Set the default Terminal profile for new windows/tabs 32 47 * 33 - * @param {object} parameters 34 - * @param {string} parameters.profile - Terminal.app profile name, e.g. 'Clear 35 - * Dark' 36 - * @param {boolean} [parameters.setDefault] - Whether to also make the 37 - * profile the default 48 + * @param {string} profile - Profile name, e.g. 'Clear Dark' 38 49 * @return {Promise<void>} 39 50 */ 40 - export async function setTerminalProfile({profile, setDefault}) { 51 + export async function setTerminalDefaultProfile(profile) { 41 52 ow(profile, ow.string.oneOf(await getTerminalProfiles())); 42 53 43 54 if (await isTerminalRunning()) { 44 55 await runAppleScript(`tell application "Terminal" 45 - set current settings of tabs of windows to settings set "${profile}" 56 + set default settings to settings set "${profile}" 46 57 end tell`); 47 - } 48 - 49 - if (setDefault) { 50 - await setTerminalDefaultProfile(profile); 58 + } else { 59 + await $`defaults write com.apple.Terminal Default\ Window\ Settings -string ${profile}`; 60 + await $`defaults write com.apple.Terminal Startup\ Window\ Settings -string ${profile}`; 51 61 } 52 62 } 53 63 54 64 /** 55 - * Set the default Terminal.app profile for new windows/tabs 65 + * Update all open Terminal tabs to use the given profile 56 66 * 57 - * @param {string} profile - Terminal.app profile name, e.g. 'Clear Dark' 67 + * @param {object} parameters 68 + * @param {string} parameters.profile - Profile name, e.g. 'Clear 69 + * Dark' 70 + * @param {boolean} [parameters.setDefault] - Whether to also make the 71 + * profile the default 58 72 * @return {Promise<void>} 59 73 */ 60 - export async function setTerminalDefaultProfile(profile) { 74 + export async function setTerminalProfile({profile, setDefault}) { 61 75 ow(profile, ow.string.oneOf(await getTerminalProfiles())); 62 76 63 77 if (await isTerminalRunning()) { 64 78 await runAppleScript(`tell application "Terminal" 65 - set default settings to settings set "${profile}" 79 + set current settings of tabs of windows to settings set "${profile}" 66 80 end tell`); 67 - } else { 68 - await $`defaults write com.apple.Terminal Default\ Window\ Settings -string ${profile}`; 69 - await $`defaults write com.apple.Terminal Startup\ Window\ Settings -string ${profile}`; 81 + } 82 + 83 + if (setDefault) { 84 + await setTerminalDefaultProfile(profile); 70 85 } 71 86 }
+32 -22
readme.md
··· 10 10 11 11 ## API 12 12 13 + ### `getTerminalDefaultProfile()` 14 + 15 + ```typescript 16 + function getTerminalDefaultProfile(): Promise<string>; 17 + ``` 18 + 19 + #### Example 20 + 21 + ```javascript 22 + import {getTerminalDefaultProfile} from 'mac-terminal'; 23 + 24 + await getTerminalDefaultProfile(); // 'Clear Dark' 25 + ``` 26 + 13 27 ### `getTerminalProfiles()` 14 28 15 29 ```typescript ··· 20 34 21 35 #### Example 22 36 23 - ```js 37 + ```javascript 24 38 import {getTerminalProfiles} from 'mac-terminal'; 25 39 26 40 await getTerminalProfiles(); // ['Basic', 'Clear Dark', 'Clear Light', ...] ··· 42 56 await isTerminalRunning(); // true 43 57 ``` 44 58 59 + ### `setTerminalDefaultProfile()` 60 + 61 + ```typescript 62 + function setTerminalDefaultProfile(profile: string): Promise<void>; 63 + ``` 64 + 65 + Set the default Terminal profile for new windows / tabs 66 + 67 + #### Example 68 + 69 + ```javascript 70 + import {setTerminalDefaultProfile} from 'mac-terminal'; 71 + 72 + await setTerminalDefaultProfile('Clear Dark'); 73 + ``` 74 + 45 75 ### `setTerminalProfile()` 46 76 47 77 ```typescript ··· 66 96 }); 67 97 ``` 68 98 69 - ### `setTerminalDefaultProfile()` 70 - 71 - ```typescript 72 - function setTerminalDefaultProfile(profile: string): Promise<void>; 73 - ``` 74 - 75 - Set the default Terminal profile for new windows / tabs 76 - 77 - #### Example 78 - 79 - ```javascript 80 - import {setTerminalDefaultProfile} from 'mac-terminal'; 81 - 82 - await setTerminalDefaultProfile('Clear Dark'); 83 - ``` 84 - 85 99 ## Related 86 100 87 - - [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 88 - 89 - ## Acknowledgements 90 - 91 - Thanks to [Jimmy Bosse](https://github.com/jbosse) for his [Stack Overflow answer](https://stackoverflow.com/a/66080297/4411309) 101 + - [auto-terminal-profile](https://github.com/patrik-csak/auto-terminal-profile) – Automatically switch Terminal profiles when macOS dark/light mode changes