33const { exec : _exec } = require ( 'child_process' ) ;
44const { resolve } = require ( 'path' ) ;
55const { promisify } = require ( 'util' ) ;
6+
7+ const debug = require ( 'debug' ) ( 'install' ) ;
8+
69const { 'interaction.pressKeys' : pressKeys } = require ( '../modules/macos/interaction' ) ;
710
811const LSREGISTER_EXECUTABLE_PATH =
@@ -137,17 +140,20 @@ const getExecOptions = async function () {
137140 * https://support.apple.com/en-us/101987
138141 *
139142 * @param {ExecOptions } options
140- * @returns {Promise<void> }
143+ * @returns {Promise<boolean> } Whether a change took place
141144 */
142145async function removeQuarantine ( options ) {
146+ debug ( 'Removing macOS quarantine' ) ;
143147 await exec ( `xattr -r -d com.apple.quarantine ${ APPLICATION_NAME } ` , options ) ;
148+ return true ;
144149}
145150
146151/**
147152 * @param {ExecOptions } options
148153 * @returns {Promise<void> }
149154 */
150155async function registerExtensions ( options ) {
156+ debug ( 'Registering trusted macOS extension' ) ;
151157 await exec ( `${ LSREGISTER_EXECUTABLE_PATH } -f -R -trusted ${ APPLICATION_NAME } ` , options ) ;
152158}
153159
@@ -156,19 +162,33 @@ async function registerExtensions(options) {
156162 * @returns {Promise<void> }
157163 */
158164async function unregisterExtensions ( options ) {
165+ debug ( 'Unregistering trusted macOS extension' ) ;
159166 await exec ( `${ LSREGISTER_EXECUTABLE_PATH } -f -R -trusted -u ${ APPLICATION_NAME } ` , options ) ;
160167}
161168
162169async function enableExtension ( ) {
170+ debug ( 'Enabling macOS extension' ) ;
163171 await exec ( `pluginkit -e use -i ${ EXTENSION_IDENTIFIER } ` ) ;
164172}
165173
166174/**
167- * @param {string } voice
175+ * @param {string } newValue the identifier for the voice to set
168176 * @returns {Promise<void> }
169177 */
170- async function setSystemVoice ( voice ) {
178+ async function setSystemVoice ( newValue ) {
179+ debug ( `Setting macOS system voice to "${ newValue } "` ) ;
180+
181+ const { stdout} = await exec ( 'defaults read com.apple.Accessibility SpeechVoiceIdentifierForLanguage' ) ;
182+ const currentValue = stdout . replace ( / [ \s ] / g, '' ) . match ( / 2 = { en= " ( [ ^ " ] + ) " ; } ; / ) ;
183+
184+ debug ( `Current value: ${ currentValue ? JSON . stringify ( currentValue [ 1 ] ) : '(unset)' } ` ) ;
185+
186+ if ( currentValue && currentValue [ 1 ] === newValue ) {
187+ debug ( 'Already set.' ) ;
188+ return ;
189+ }
190+
171191 await exec (
172- `defaults write com.apple.Accessibility SpeechVoiceIdentifierForLanguage '{2 = {en = "${ voice } ";};}'` ,
192+ `defaults write com.apple.Accessibility SpeechVoiceIdentifierForLanguage '{2 = {en = "${ newValue } ";};}'` ,
173193 ) ;
174194}
0 commit comments