1- import { execFileSync , spawn } from "node:child_process" ;
1+ import { execFileSync , execSync } from "node:child_process" ;
22import { existsSync } from "node:fs" ;
33import { homedir } from "node:os" ;
44import { join } from "node:path" ;
@@ -28,18 +28,19 @@ function getPreferencesDirectory(): string {
2828 return join ( homedir ( ) , "Library" , "Preferences" ) ;
2929}
3030
31- function startVoiceOver ( ) : void {
32- spawn (
33- "/System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter" ,
34- [ ] ,
35- { detached : true , stdio : "ignore" } ,
36- ) . unref ( ) ;
31+ async function startVoiceOver ( ) : Promise < void > {
32+ execSync (
33+ "/System/Library/CoreServices/VoiceOver.app/Contents/MacOS/VoiceOverStarter &" ,
34+ { stdio : "ignore" , timeout : 2000 } ,
35+ ) ;
36+
37+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
3738}
3839
3940function isRunning ( ) : boolean {
4041 try {
4142 return (
42- execFileSync ( "pgrep" , [ "-f" , "VoiceOver" ] , {
43+ execFileSync ( "pgrep" , [ "-f" , "VoiceOver launchd -s " ] , {
4344 encoding : "utf8" ,
4445 timeout : 2000 ,
4546 } ) . length > 0
@@ -50,10 +51,8 @@ function isRunning(): boolean {
5051}
5152
5253const stopVoiceOverApplescript = `
53- tell application "VoiceOver"
54- with transaction
55- quit
56- end transaction
54+ tell application "System Events"
55+ key code 96 using {command down}
5756end tell` ;
5857
5958async function stopVoiceOver ( ) : Promise < void > {
@@ -63,6 +62,8 @@ async function stopVoiceOver(): Promise<void> {
6362 // Swallow
6463 }
6564
65+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
66+
6667 try {
6768 execFileSync (
6869 "pkill" ,
@@ -76,6 +77,8 @@ async function stopVoiceOver(): Promise<void> {
7677 // Swallow
7778 }
7879
80+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
81+
7982 if ( isRunning ( ) ) {
8083 throw new Error ( ERR_SETUP_MACOS_UNABLE_TO_STOP_VOICEOVER ) ;
8184 }
@@ -99,7 +102,7 @@ export async function ensureLocalPreferencesExist(): Promise<void> {
99102 return ;
100103 }
101104
102- startVoiceOver ( ) ;
105+ await startVoiceOver ( ) ;
103106
104107 await retryOnError (
105108 ( ) => {
@@ -115,5 +118,5 @@ export async function ensureLocalPreferencesExist(): Promise<void> {
115118 delay : 100 ,
116119 } ) ;
117120
118- await retryOnError ( ( ) => stopVoiceOver ( ) , { retries : 10 , delay : 200 } ) ;
121+ await retryOnError ( ( ) => stopVoiceOver ( ) , { retries : 10 , delay : 100 } ) ;
119122}
0 commit comments