copy_from_cache_to_temp IPC handler was registered in file_handlers.ts but never added to the preload bridge, making it unreachable from Svelte despite being the documented preferred primitive for custom launch flows. launch_presentation was the last handler still using osascript -e with inline path injection. Converted to the temp-.scpt-file approach already used by run_osascript and launch_from_cache — prevents breakage on presentation filenames with spaces, quotes, or parentheses. Also adds a pre-copy existence check to launch_from_cache so a missing cache entry returns a meaningful error instead of a raw ENOENT. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
237 lines
9.0 KiB
TypeScript
237 lines
9.0 KiB
TypeScript
import { ipcMain, shell } from 'electron';
|
|
import { exec, execSync } from 'child_process';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
import { expandPath } from './file_utils';
|
|
|
|
export function registerShellHandlers() {
|
|
ipcMain.handle('native:open-folder', async (event, folderPath: string) => {
|
|
const cleanPath = expandPath(folderPath);
|
|
const error = await shell.openPath(cleanPath);
|
|
return { success: !error, error };
|
|
});
|
|
|
|
ipcMain.handle('native:run-cmd', async (event, { cmd, timeout = 30000 }) => {
|
|
const cleanCmd = expandPath(cmd);
|
|
return new Promise((resolve) => {
|
|
exec(cleanCmd, { timeout }, (error, stdout, stderr) => {
|
|
resolve({ success: !error, stdout: stdout.trim(), stderr: stderr.trim(), error: error ? error.message : null });
|
|
});
|
|
});
|
|
});
|
|
|
|
ipcMain.handle('native:run-cmd-sync', async (event, { cmd }) => {
|
|
const cleanCmd = expandPath(cmd);
|
|
try {
|
|
const stdout = execSync(cleanCmd).toString();
|
|
return { success: true, stdout: stdout.trim() };
|
|
} catch (error: any) {
|
|
return { success: false, error: error.message, stderr: error.stderr?.toString() };
|
|
}
|
|
});
|
|
|
|
ipcMain.handle('native:run-osascript', async (event, script: string) => {
|
|
if (os.platform() !== 'darwin') return { success: false, error: 'AppleScript is only available on macOS' };
|
|
|
|
// HARDENED: Write script to a temp .scpt file rather than passing inline via -e.
|
|
// The old -e approach (`osascript -e "..."`) has two fatal flaws:
|
|
// 1. It breaks on multi-line scripts.
|
|
// 2. It breaks on paths containing spaces or special characters (quotes, parens, etc.)
|
|
// Writing to a file sidesteps both — no shell escaping needed at all.
|
|
// The .scpt file is deleted immediately after execution (success or failure).
|
|
// Worst case on crash: a stale .scpt in /tmp, cleared on next OS reboot.
|
|
//
|
|
// LEGACY (removed): const cmd = `osascript -e "${script.replace(/"/g, '\\"')}"`;
|
|
|
|
const tmp_script_path = path.join(os.tmpdir(), `ae_osa_${Date.now()}.scpt`);
|
|
return new Promise((resolve) => {
|
|
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}"`, (error, stdout, stderr) => {
|
|
try { fs.unlinkSync(tmp_script_path); } catch {}
|
|
resolve({ success: !error, stdout: stdout.trim(), stderr: stderr.trim(), error: error ? error.message : null });
|
|
});
|
|
});
|
|
});
|
|
|
|
ipcMain.handle('native:kill-processes', async (event, { process_name_li = [] }) => {
|
|
console.log(`Native: Killing processes -> `, process_name_li);
|
|
const results = [];
|
|
for (const name of process_name_li) {
|
|
const cmd = os.platform() === 'win32'
|
|
? `taskkill /F /IM ${name} /T`
|
|
: `pkill -f ${name}`;
|
|
try {
|
|
execSync(cmd);
|
|
results.push({ name, success: true });
|
|
} catch (e: any) {
|
|
results.push({ name, success: false, error: e.message });
|
|
}
|
|
}
|
|
return { success: true, results };
|
|
});
|
|
|
|
ipcMain.handle('native:open-local-file-v2', async (event, filePath: string) => {
|
|
const cleanPath = expandPath(filePath);
|
|
const error = await shell.openPath(cleanPath);
|
|
return { success: !error, error };
|
|
});
|
|
|
|
ipcMain.handle('native:launch-presentation', async (event, { path: rawPath, app: appType = 'default' }) => {
|
|
const cleanedPath = expandPath(rawPath);
|
|
console.log(`Native: Launching Presentation -> ${cleanedPath} (App: ${appType})`);
|
|
|
|
if (os.platform() === 'linux') {
|
|
const cmd = `libreoffice --impress "${cleanedPath}"`;
|
|
return new Promise((resolve) => {
|
|
exec(cmd, (err, stdout, stderr) => {
|
|
if (err) resolve({ success: false, error: err.message });
|
|
else resolve({ success: true, stdout, stderr });
|
|
});
|
|
});
|
|
}
|
|
|
|
if (os.platform() === 'darwin') {
|
|
let script = '';
|
|
if (appType === 'keynote') {
|
|
script = `
|
|
tell application "Keynote"
|
|
activate
|
|
open (POSIX file "${cleanedPath}")
|
|
delay 1
|
|
start (front document)
|
|
end tell
|
|
`.trim();
|
|
} else if (appType === 'powerpoint') {
|
|
script = `
|
|
tell application "Microsoft PowerPoint"
|
|
activate
|
|
open (POSIX file "${cleanedPath}")
|
|
delay 1
|
|
run slide show of active presentation
|
|
end tell
|
|
`.trim();
|
|
}
|
|
|
|
if (script) {
|
|
const tmp_script_path = path.join(os.tmpdir(), `ae_launch_${Date.now()}.scpt`);
|
|
return new Promise((resolve) => {
|
|
try {
|
|
fs.writeFileSync(tmp_script_path, script);
|
|
} 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 });
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
const error = await shell.openPath(cleanedPath);
|
|
return { success: !error, error };
|
|
});
|
|
|
|
ipcMain.handle('native:control-presentation', async (event, { app, action }) => {
|
|
if (os.platform() !== 'darwin') return { success: false, error: 'Presentation control is only available on macOS' };
|
|
|
|
let script = '';
|
|
if (app === 'powerpoint') {
|
|
switch (action) {
|
|
case 'next': script = 'tell application "Microsoft PowerPoint" to next slide of slide show view of active presentation'; break;
|
|
case 'prev': script = 'tell application "Microsoft PowerPoint" to previous slide of slide show view of active presentation'; break;
|
|
case 'start': script = 'tell application "Microsoft PowerPoint" to run slide show of active presentation'; break;
|
|
case 'stop': script = 'tell application "Microsoft PowerPoint" to stop slide show of active presentation'; break;
|
|
}
|
|
} else if (app === 'keynote') {
|
|
switch (action) {
|
|
case 'next': script = 'tell application "Keynote" to show next'; break;
|
|
case 'prev': script = 'tell application "Keynote" to show previous'; break;
|
|
case 'start': script = 'tell application "Keynote" to start (front document)'; break;
|
|
case 'stop': script = 'tell application "Keynote" to stop'; break;
|
|
}
|
|
}
|
|
|
|
if (!script) return { success: false, error: `Unsupported app or action: ${app}/${action}` };
|
|
|
|
return new Promise((resolve) => {
|
|
exec(`osascript -e "${script.replace(/"/g, '\\"')}"`, (error, stdout, stderr) => {
|
|
resolve({ success: !error, stdout: stdout.trim(), stderr: stderr.trim(), error: error ? error.message : null });
|
|
});
|
|
});
|
|
});
|
|
|
|
ipcMain.handle('native:list-tools', async () => {
|
|
return [
|
|
{
|
|
name: 'open_folder',
|
|
description: 'Opens a directory in the OS file explorer (Finder/Files/Explorer).',
|
|
params: { path: 'string' }
|
|
},
|
|
{
|
|
name: 'run_cmd',
|
|
description: 'Executes an asynchronous shell command with a timeout.',
|
|
params: { cmd: 'string', timeout: 'number (optional)' }
|
|
},
|
|
{
|
|
name: 'run_cmd_sync',
|
|
description: 'Executes a synchronous shell command.',
|
|
params: { cmd: 'string' }
|
|
},
|
|
{
|
|
name: 'run_osascript',
|
|
description: 'Executes a raw AppleScript string (macOS only).',
|
|
params: { script: 'string' }
|
|
},
|
|
{
|
|
name: 'kill_processes',
|
|
description: 'Forcefully terminates processes by name.',
|
|
params: { process_name_li: 'string[]' }
|
|
},
|
|
{
|
|
name: 'open_local_file_v2',
|
|
description: 'Opens a local file using the default OS handler.',
|
|
params: { filePath: 'string' }
|
|
},
|
|
{
|
|
name: 'launch_presentation',
|
|
description: 'Phase 5: Specialized launcher for PowerPoint, Keynote, and LibreOffice with auto-focus.',
|
|
params: { path: 'string', app: 'default|powerpoint|keynote' }
|
|
},
|
|
{
|
|
name: 'control_presentation',
|
|
description: 'Phase 5: Remote navigation for active slideshows.',
|
|
params: { app: 'powerpoint|keynote', action: 'next|prev|start|stop' }
|
|
},
|
|
{
|
|
name: 'check_cache',
|
|
description: 'Checks if a file exists in the local organized cache.',
|
|
params: { cache_root: 'string', hash: 'string', hash_prefix_length: 'number' }
|
|
},
|
|
{
|
|
name: 'download_to_cache',
|
|
description: 'Downloads a file from the API directly into the native cache.',
|
|
params: { url: 'string', cache_root: 'string', hash: 'string', api_key: 'string', account_id: 'string' }
|
|
},
|
|
{
|
|
name: 'launch_from_cache',
|
|
description: 'Atomic operation: Copies file from cache to temp with original name and launches via specialized handler.',
|
|
params: { cache_root: 'string', hash: 'string', temp_root: 'string', filename: 'string' }
|
|
},
|
|
{
|
|
name: 'get_device_info',
|
|
description: 'Returns hardware and OS metadata (CPUs, RAM, IP addresses, Hostname).',
|
|
params: {}
|
|
}
|
|
];
|
|
});
|
|
}
|