Skip to content

Fix macOS AppleScript helper compilation (empty/corrupt scripts, osascript -1758) - #113

Open
Bounga wants to merge 2 commits into
tecosaur:masterfrom
Bounga:fix/macos-osascript-compile
Open

Bounga wants to merge 2 commits into
tecosaur:masterfrom
Bounga:fix/macos-osascript-compile

Conversation

@Bounga

@Bounga Bounga commented Jul 7, 2026 •

Copy link
Copy Markdown

Problem

The macOS AppleScript helpers (app-name, window-geometry, window-title) can remain unreadable, causing osascript to report error -1758 instead of returning application and window information.

Closes #112.

The existing compilation used legacy resource-fork options, checked for files relative to the caller's directory, and could retain obsolete resource-fork data when overwriting an output. The original fix also skipped regeneration when all three files existed, even if they were corrupt. This explains why an existing installation could still require manual deletion of the helpers.

Change

  • Resolve helper paths inside the package directory.
  • Check existing helpers with osadecompile, so unreadable files are regenerated automatically.
  • Compile without the legacy resource-fork options, using process arguments rather than shell interpolation.
  • Compile into a temporary file and check both the compiler's exit status and the resulting script's readability before replacing the existing helper.
  • Remove the old output before installing the new file, including any obsolete resource fork.
  • Report compiler failures with the helper name and compiler diagnostics, preserve the existing helper on compilation failure, and clean up temporary output.

Valid helpers are reused unless forced regeneration is requested. Readability checks add three osadecompile calls when all cached helpers are valid.

Validation

Seven ERT tests passed on macOS 26.5.1 with Emacs 31.0.91:

  • Fresh compilation from a caller directory different from the package directory, with spaces in the package path.
  • Repair of existing corrupt files.
  • Replacement of real legacy resource-fork scripts.
  • Reuse of valid helpers and forced recompilation.
  • Compiler failure with partial output: diagnostic reported, old helpers preserved, temporary output removed.
  • A native osacompile syntax error: failure reported and the existing helper preserved.
  • Invalid output despite a successful compiler exit: rejected and cleaned up.
emacs --batch -Q -L . -l test/emacs-everywhere-test.el -f ert-run-tests-batch-and-exit

Byte compilation also completed; it reported warnings in existing code outside this change. Other macOS versions have not been tested in this update.

On modern macOS `osacompile -r scpt:128 -t osas` produces empty or
corrupt files, so osascript fails with "script error -1758" and app
detection breaks. Bind default-directory around the existence guard so
it stops recompiling on every call, drop the obsolete flags, and remove
any stale output file before recompiling.
@mbarton98

Copy link
Copy Markdown

FYI, I was having the same problem. I applied this fix and it worked for me, but I had to remove the compiled script files after restarting Emacs with the patched version and restart again (just to have a clean slate. Could be my configuration, but it appears to be working fine again now.

@Bounga

Bounga commented Nov 1, 2026

Copy link
Copy Markdown
Author

@mbarton98 I updated the PR to cover the case you reported: helper setup now checks whether existing scripts can actually be read, rather than only checking whether the files exist. With the updated code loaded, the next invocation automatically regenerates unreadable helpers, without manually deleting them.

Compilation errors are now reported explicitly, and an existing helper is preserved if its replacement fails to compile. The regression tests cover existing corrupt files, legacy resource-fork scripts, and compiler failures. All seven tests pass on macOS 26.5.1 with Emacs 31.0.91.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: compiled AppleScript helpers are empty/corrupt (osascript error -1758), breaking app detection

2 participants