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:
17
dist/main/file_handlers.js
vendored
17
dist/main/file_handlers.js
vendored
@@ -181,9 +181,22 @@ function registerFileHandlers() {
|
|||||||
}
|
}
|
||||||
if (script) {
|
if (script) {
|
||||||
console.log(`Native: Launching ${ext} via AppleScript for ${target}`);
|
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) => {
|
return new Promise((resolve) => {
|
||||||
const escapedScript = script.trim().replace(/"/g, '\\\\\\"').replace(/\\n/g, ' -e "') + '"';
|
try {
|
||||||
(0, child_process_1.exec)(`osascript -e ${escapedScript}`, (err) => {
|
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)
|
if (err)
|
||||||
resolve({ success: false, error: err.message });
|
resolve({ success: false, error: err.message });
|
||||||
else
|
else
|
||||||
|
|||||||
2
dist/main/file_handlers.js.map
vendored
2
dist/main/file_handlers.js.map
vendored
File diff suppressed because one or more lines are too long
@@ -154,9 +154,18 @@ export function registerFileHandlers() {
|
|||||||
|
|
||||||
if (script) {
|
if (script) {
|
||||||
console.log(`Native: Launching ${ext} via AppleScript for ${target}`);
|
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) => {
|
return new Promise((resolve) => {
|
||||||
const escapedScript = script.trim().replace(/"/g, '\\\\\\"').replace(/\\n/g, ' -e "') + '"';
|
try {
|
||||||
exec(`osascript -e ${escapedScript}`, (err) => {
|
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 });
|
if (err) resolve({ success: false, error: err.message });
|
||||||
else resolve({ success: true });
|
else resolve({ success: true });
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user