fix: write AppleScript to temp .scpt file instead of using -e flag

The -e flag approach breaks on multi-line AppleScript and on file paths
containing spaces or quote characters. Write the script to a temp file
in os.tmpdir() and invoke osascript with the file path instead. The
temp file is cleaned up after execution.
This commit is contained in:
Scott Idem
2026-04-20 14:26:34 -04:00
parent 5a5814b2bc
commit 2af6b3954b
3 changed files with 27 additions and 5 deletions

View File

@@ -181,9 +181,22 @@ function registerFileHandlers() {
}
if (script) {
console.log(`Native: Launching ${ext} via AppleScript for ${target}`);
// Write to a temp .scpt file instead of passing via -e flag.
// The -e approach breaks on multi-line scripts and paths with spaces or quotes.
const tmp_script_path = path.join(os.tmpdir(), `ae_launch_${Date.now()}.scpt`);
return new Promise((resolve) => {
const escapedScript = script.trim().replace(/"/g, '\\\\\\"').replace(/\\n/g, ' -e "') + '"';
(0, child_process_1.exec)(`osascript -e ${escapedScript}`, (err) => {
try {
fs.writeFileSync(tmp_script_path, script.trim());
}
catch (e) {
resolve({ success: false, error: `Failed to write AppleScript temp file: ${e.message}` });
return;
}
(0, child_process_1.exec)(`osascript "${tmp_script_path}"`, (err) => {
try {
fs.unlinkSync(tmp_script_path);
}
catch { }
if (err)
resolve({ success: false, error: err.message });
else

File diff suppressed because one or more lines are too long

View File

@@ -154,9 +154,18 @@ export function registerFileHandlers() {
if (script) {
console.log(`Native: Launching ${ext} via AppleScript for ${target}`);
// Write to a temp .scpt file instead of passing via -e flag.
// The -e approach breaks on multi-line scripts and paths with spaces or quotes.
const tmp_script_path = path.join(os.tmpdir(), `ae_launch_${Date.now()}.scpt`);
return new Promise((resolve) => {
const escapedScript = script.trim().replace(/"/g, '\\\\\\"').replace(/\\n/g, ' -e "') + '"';
exec(`osascript -e ${escapedScript}`, (err) => {
try {
fs.writeFileSync(tmp_script_path, script.trim());
} catch (e: any) {
resolve({ success: false, error: `Failed to write AppleScript temp file: ${e.message}` });
return;
}
exec(`osascript "${tmp_script_path}"`, (err) => {
try { fs.unlinkSync(tmp_script_path); } catch {}
if (err) resolve({ success: false, error: err.message });
else resolve({ success: true });
});