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:
@@ -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 });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user