LykosAI/StabilityMatrix · error · InvalidOperationException

The AppleScript script failed with exit code

Error message

The AppleScript script failed with exit code {process.ExitCode}: (StdOut = {stdOut}, StdErr = {stdErr})

What it means

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.

Solutions

  1. Read StdErr in the exception: error -1743 means grant the app Finder/Automation permission in System Settings > Privacy & Security.
  2. Confirm the target path still exists right before calling the recycle API.
  3. Ensure a logged-in GUI session with Finder is available (not a headless SSH context).
  4. Re-run the operation; if the script itself is failing, update the app in case the AppleScript needs changes for your macOS version.

Example fix

// before: app lacks automation permission, osascript exits -1743
await recycleBin.MoveFileToRecycleBinAsync(path);
// after: verify existence and handle failure
if (File.Exists(path))
    try { await recycleBin.MoveFileToRecycleBinAsync(path); }
    catch (InvalidOperationException ex) { logger.Warning(ex, "Trash move failed"); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (!(File.Exists(path) || Directory.Exists(path)))
    throw new FileNotFoundException($"Cannot recycle, item missing: {path}");

Type guard

static bool CanTrashOnMacOS() =>
    OperatingSystem.IsMacOS() && Directory.Exists("/System/Library/CoreServices/Finder.app");

Try / catch

try { await recycleBin.MoveFileToRecycleBinAsync(path); }
catch (InvalidOperationException ex) when (ex.Message.Contains("AppleScript"))
{
    if (ex.Message.Contains("-1743"))
        logger.Warning("Finder automation denied; ask user to grant Automation permission");
    else
        logger.Warning(ex, "AppleScript trash move failed");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/a02fe63b1db926e6. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Native.macOS/NativeRecycleBinProvider.cs:101

        process.StartInfo = new ProcessStartInfo
        {
            FileName = "/usr/bin/osascript",
            Arguments = $"-e \"{script}\"",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        process.Start();

        await process.WaitForExitAsync(cancellationToken).ConfigureAwait(false);

        if (process.ExitCode != 0)
        {
            var stdOut = await process.StandardOutput.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
            var stdErr = await process.StandardError.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
            throw new InvalidOperationException(
                $"The AppleScript script failed with exit code {process.ExitCode}: (StdOut = {stdOut}, StdErr = {stdErr})"
            );
        }
    }
}

View on GitHub (pinned to af93d6ef57)