@@ -303,7 +303,22 @@ function printInstructions(fileName: string, errorMessage: string | null) {
303
303
console . log ( )
304
304
}
305
305
306
- function launchEditor ( fileName : string , lineNumber : number , colNumber : number ) {
306
+ export function escapeApplescriptStringFragment ( input : string ) : string {
307
+ // The only two special characters in a quoted applescript string are
308
+ // backslash and double quote. Both are escaped with a preceeding backslash.
309
+ //
310
+ // Some whitespace characters (like newlines) can be escaped (as `\n`), but
311
+ // aren't required to be escaped (so we're not bothering to do that).
312
+ //
313
+ // https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html#//apple_ref/doc/uid/TP40000983-CH1g-BBCIAHJF:~:text=Special%20String%20Characters
314
+ return input . replaceAll ( / [ \\ " ] / g, ( original ) => `\\${ original } ` )
315
+ }
316
+
317
+ export function launchEditor (
318
+ fileName : string ,
319
+ lineNumber : number ,
320
+ colNumber : number
321
+ ) {
307
322
if ( ! fs . existsSync ( fileName ) ) {
308
323
return
309
324
}
@@ -387,15 +402,12 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) {
387
402
} )
388
403
} else if ( isTerminalEditor ( editor ) ) {
389
404
if ( process . platform === 'darwin' ) {
405
+ const escapedScript = escapeApplescriptStringFragment (
406
+ shellQuote . quote ( [ editor , ...args ] )
407
+ )
390
408
p = child_process . spawn (
391
409
'osascript' ,
392
- [
393
- '-e' ,
394
- `tell application "Terminal" to do script "${ shellQuote . quote ( [
395
- editor ,
396
- ...args ,
397
- ] ) } "`,
398
- ] ,
410
+ [ '-e' , `tell application "Terminal" to do script "${ escapedScript } "` ] ,
399
411
{ stdio : 'ignore' }
400
412
)
401
413
} else {
@@ -416,5 +428,3 @@ function launchEditor(fileName: string, lineNumber: number, colNumber: number) {
416
428
} )
417
429
}
418
430
}
419
-
420
- export { launchEditor }
0 commit comments