this repo has no description
0
fork

Configure Feed

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

split and move index.js

+122 -103
-101
index.js
··· 1 - import {execFile} from 'node:child_process'; 2 - import os from 'node:os'; 3 - import {promisify} from 'node:util'; 4 - import bplist from 'bplist-parser'; 5 - import {runAppleScript} from 'run-applescript'; 6 - 7 - const execute = promisify(execFile); 8 - 9 - /** 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} = await execute('defaults', [ 20 - 'read', 21 - 'com.apple.Terminal', 22 - 'Default Window Settings', 23 - ]); 24 - 25 - return stdout.trim(); 26 - } 27 - 28 - /** 29 - * @returns {Promise<string[]>} - List of installed profiles 30 - */ 31 - export async function getTerminalProfiles() { 32 - const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`; 33 - const terminalPreferences = await bplist.parseFile(terminalPlistPath); 34 - 35 - return Object.keys(terminalPreferences[0]['Window Settings']).toSorted( 36 - new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}).compare, 37 - ); 38 - } 39 - 40 - /** 41 - * @returns {Promise<boolean>} - Whether Terminal is currently running 42 - */ 43 - export async function isTerminalRunning() { 44 - try { 45 - await execute('pgrep', ['-x', 'Terminal']); 46 - return true; 47 - } catch { 48 - return false; 49 - } 50 - } 51 - 52 - /** 53 - * Set the default Terminal profile for new windows/tabs 54 - * 55 - * @param {string} profile - Profile name, e.g. 'Clear Dark' 56 - * @return {Promise<void>} 57 - */ 58 - export async function setTerminalDefaultProfile(profile) { 59 - if (await isTerminalRunning()) { 60 - await runAppleScript(`tell application "Terminal" 61 - set default settings to settings set "${profile}" 62 - end tell`); 63 - } else { 64 - await execute('defaults', [ 65 - 'write', 66 - 'com.apple.Terminal', 67 - 'Default Window Settings', 68 - '-string', 69 - profile, 70 - ]); 71 - await execute('defaults', [ 72 - 'write', 73 - 'com.apple.Terminal', 74 - 'Startup Window Settings', 75 - '-string', 76 - profile, 77 - ]); 78 - } 79 - } 80 - 81 - /** 82 - * Update all open Terminal tabs to use the given profile 83 - * 84 - * @param {object} parameters 85 - * @param {string} parameters.profile - Profile name, e.g. 'Clear 86 - * Dark' 87 - * @param {boolean} [parameters.setDefault] - Whether to also make the 88 - * profile the default 89 - * @return {Promise<void>} 90 - */ 91 - export async function setTerminalProfile({profile, setDefault}) { 92 - if (await isTerminalRunning()) { 93 - await runAppleScript(`tell application "Terminal" 94 - set current settings of tabs of windows to settings set "${profile}" 95 - end tell`); 96 - } 97 - 98 - if (setDefault) { 99 - await setTerminalDefaultProfile(profile); 100 - } 101 - }
+2 -2
package.json
··· 10 10 "author": "Patrik Csak <p@trikcsak.com>", 11 11 "type": "module", 12 12 "exports": { 13 - ".": "./index.js" 13 + ".": "./source/index.js" 14 14 }, 15 15 "files": [ 16 - "index.js" 16 + "source" 17 17 ], 18 18 "scripts": { 19 19 "_eslint": "eslint --cache --cache-strategy=content",
+25
source/get-terminal-default-profile.js
··· 1 + import {execFile} from 'node:child_process'; 2 + import {promisify} from 'node:util'; 3 + import {runAppleScript} from 'run-applescript'; 4 + import isTerminalRunning from './is-terminal-running.js'; 5 + 6 + const execute = promisify(execFile); 7 + 8 + /** 9 + * @returns {Promise<string>} - The name of the default profile 10 + */ 11 + export default async function getTerminalDefaultProfile() { 12 + if (await isTerminalRunning()) { 13 + return runAppleScript( 14 + 'tell application "Terminal" to get name of default settings', 15 + ); 16 + } 17 + 18 + const {stdout} = await execute('defaults', [ 19 + 'read', 20 + 'com.apple.Terminal', 21 + 'Default Window Settings', 22 + ]); 23 + 24 + return stdout.trim(); 25 + }
+14
source/get-terminal-profiles.js
··· 1 + import os from 'node:os'; 2 + import bplist from 'bplist-parser'; 3 + 4 + /** 5 + * @returns {Promise<string[]>} - List of installed profiles 6 + */ 7 + export default async function getTerminalProfiles() { 8 + const terminalPlistPath = `${os.homedir()}/Library/Preferences/com.apple.Terminal.plist`; 9 + const terminalPreferences = await bplist.parseFile(terminalPlistPath); 10 + 11 + return Object.keys(terminalPreferences[0]['Window Settings']).toSorted( 12 + new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'}).compare, 13 + ); 14 + }
+5
source/index.js
··· 1 + export {default as getTerminalDefaultProfile} from './get-terminal-default-profile.js'; 2 + export {default as getTerminalProfiles} from './get-terminal-profiles.js'; 3 + export {default as isTerminalRunning} from './is-terminal-running.js'; 4 + export {default as setTerminalDefaultProfile} from './set-terminal-default-profile.js'; 5 + export {default as setTerminalProfile} from './set-terminal-profile.js';
+16
source/is-terminal-running.js
··· 1 + import {execFile} from 'node:child_process'; 2 + import {promisify} from 'node:util'; 3 + 4 + const execute = promisify(execFile); 5 + 6 + /** 7 + * @returns {Promise<boolean>} - Whether Terminal is currently running 8 + */ 9 + export default async function isTerminalRunning() { 10 + try { 11 + await execute('pgrep', ['-x', 'Terminal']); 12 + return true; 13 + } catch { 14 + return false; 15 + } 16 + }
+35
source/set-terminal-default-profile.js
··· 1 + import {execFile} from 'node:child_process'; 2 + import {promisify} from 'node:util'; 3 + import {runAppleScript} from 'run-applescript'; 4 + import isTerminalRunning from './is-terminal-running.js'; 5 + 6 + const execute = promisify(execFile); 7 + 8 + /** 9 + * Set the default Terminal profile for new windows/tabs 10 + * 11 + * @param {string} profile - Profile name, e.g. 'Clear Dark' 12 + * @return {Promise<void>} 13 + */ 14 + export default async function setTerminalDefaultProfile(profile) { 15 + if (await isTerminalRunning()) { 16 + await runAppleScript(`tell application "Terminal" 17 + set default settings to settings set "${profile}" 18 + end tell`); 19 + } else { 20 + await execute('defaults', [ 21 + 'write', 22 + 'com.apple.Terminal', 23 + 'Default Window Settings', 24 + '-string', 25 + profile, 26 + ]); 27 + await execute('defaults', [ 28 + 'write', 29 + 'com.apple.Terminal', 30 + 'Startup Window Settings', 31 + '-string', 32 + profile, 33 + ]); 34 + } 35 + }
+25
source/set-terminal-profile.js
··· 1 + import {runAppleScript} from 'run-applescript'; 2 + import isTerminalRunning from './is-terminal-running.js'; 3 + import setTerminalDefaultProfile from './set-terminal-default-profile.js'; 4 + 5 + /** 6 + * Update all open Terminal tabs to use the given profile 7 + * 8 + * @param {object} parameters 9 + * @param {string} parameters.profile - Profile name, e.g. 'Clear 10 + * Dark' 11 + * @param {boolean} [parameters.setDefault] - Whether to also make the 12 + * profile the default 13 + * @return {Promise<void>} 14 + */ 15 + export default async function setTerminalProfile({profile, setDefault}) { 16 + if (await isTerminalRunning()) { 17 + await runAppleScript(`tell application "Terminal" 18 + set current settings of tabs of windows to settings set "${profile}" 19 + end tell`); 20 + } 21 + 22 + if (setDefault) { 23 + await setTerminalDefaultProfile(profile); 24 + } 25 + }