{"record":{"id":"a02fe63b1db926e6","repo":"LykosAI/StabilityMatrix","slug":"the-applescript-script-failed-with-exit-code-process","errorCode":null,"errorMessage":"The AppleScript script failed with exit code {process.ExitCode}: (StdOut = {stdOut}, StdErr = {stdErr})","messagePattern":"The AppleScript script failed with exit code (.+?): \\(StdOut = (.+?), StdErr = (.+?)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Native.macOS/NativeRecycleBinProvider.cs","lineNumber":101,"sourceCode":"        process.StartInfo = new ProcessStartInfo\n        {\n            FileName = \"/usr/bin/osascript\",\n            Arguments = $\"-e \\\"{script}\\\"\",\n            RedirectStandardOutput = true,\n            RedirectStandardError = true,\n            UseShellExecute = false,\n            CreateNoWindow = true\n        };\n\n        process.Start();\n\n        await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);\n\n        if (process.ExitCode != 0)\n        {\n            var stdOut = await process.StandardOutput.ReadToEndAsync(cancellationToken).ConfigureAwait(false);\n            var stdErr = await process.StandardError.ReadToEndAsync(cancellationToken).ConfigureAwait(false);\n            throw new InvalidOperationException(\n                $\"The AppleScript script failed with exit code {process.ExitCode}: (StdOut = {stdOut}, StdErr = {stdErr})\"\n            );\n        }\n    }\n}\n","sourceCodeStart":83,"sourceCodeEnd":107,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Native.macOS/NativeRecycleBinProvider.cs#L83-L107","documentation":"NativeRecycleBinProvider on macOS moves items to Trash by running an osascript AppleScript. RunAppleScriptAsync waits for the process and, if osascript exits non-zero, throws this InvalidOperationException including exit code, stdout and stderr. It signals the Trash move did not happen.","triggerScenarios":"MoveFileToRecycleBinAsync / MoveFilesToRecycleBinAsync / MoveDirectoryToRecycleBinAsync / MoveDirectoriesToRecycleBinAsync invoke RunAppleScriptAsync and osascript returns a non-zero exit code — script syntax error, target item missing, permission denied, or Finder automation blocked.","commonSituations":"The file was deleted/moved before the script ran; Finder automation is denied in System Settings (Privacy & Security > Automation) causing error -1743; macOS running in a headless/SSH session where Finder is unavailable; a macOS update breaking the scripted Finder interface.","solutions":["Read StdErr in the exception: error -1743 means grant the app Finder/Automation permission in System Settings > Privacy & Security.","Confirm the target path still exists right before calling the recycle API.","Ensure a logged-in GUI session with Finder is available (not a headless SSH context).","Re-run the operation; if the script itself is failing, update the app in case the AppleScript needs changes for your macOS version."],"exampleFix":"// before: app lacks automation permission, osascript exits -1743\nawait recycleBin.MoveFileToRecycleBinAsync(path);\n// after: verify existence and handle failure\nif (File.Exists(path))\n    try { await recycleBin.MoveFileToRecycleBinAsync(path); }\n    catch (InvalidOperationException ex) { logger.Warning(ex, \"Trash move failed\"); }","handlingStrategy":"try-catch","validationCode":"if (!(File.Exists(path) || Directory.Exists(path)))\n    throw new FileNotFoundException($\"Cannot recycle, item missing: {path}\");","typeGuard":"static bool CanTrashOnMacOS() =>\n    OperatingSystem.IsMacOS() && Directory.Exists(\"/System/Library/CoreServices/Finder.app\");","tryCatchPattern":"try { await recycleBin.MoveFileToRecycleBinAsync(path); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"AppleScript\"))\n{\n    if (ex.Message.Contains(\"-1743\"))\n        logger.Warning(\"Finder automation denied; ask user to grant Automation permission\");\n    else\n        logger.Warning(ex, \"AppleScript trash move failed\");\n}","preventionTips":["Grant the app Finder/Automation permission in Privacy & Security settings.","Check the item still exists immediately before trashing.","Run inside a logged-in GUI session, not headless SSH.","Log StdErr from failures to distinguish permission vs. missing-item causes."],"tags":["macos","applescript","recycle-bin","process"],"backgroundTag":"command-not-found","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}