Skip to content

macOS: open files from Finder and open -a via application:openURLs: - #18

Closed
smkly wants to merge 1 commit into
storytold:mainfrom
smkly:macos-open-urls
Closed

smkly wants to merge 1 commit into
storytold:mainfrom
smkly:macos-open-urls

Conversation

@smkly

@smkly smkly commented Oct 8, 2026

Copy link
Copy Markdown

Same fix as the WordCraft PR: storytold/wordcraft#33

Problem

On macOS, Finder "Open With" and double-click on a .xlsx show "GridCraft cannot open files in the … format". open -a GridCraft file.xlsx also opens nothing. File > Open and drag-and-drop work.

Cause

GridCraft reads files to open only from argv. Finder and open send files through the application:openURLs: Apple Event on the NSApplicationDelegate. Nothing handled that method.

Approach

  • New crate crates/macos-open (gridcraft-macos-open). It adds application:openURLs: to the class of winit's own delegate with class_addMethod. It does this at NSApplicationWillFinishLaunchingNotification: the delegate exists, and AppKit has not yet delivered the launch documents.
  • Why not a replacement delegate: winit 0.30.13 registers its own delegate in EventLoop::new and panics if it is replaced: tried to get a delegate that was not the one Winit has registered. Its doc comment that says otherwise is stale.
  • apps/gridcraft installs the handler before eframe::run_native. The handler queues the paths and requests a repaint. raw_input_hook drains the queue through open_path, the same path as argv and drag-and-drop.
  • No new external crates. objc2, block2, objc2-foundation and objc2-app-kit were already in Cargo.lock. The lockfile diff adds only the new workspace package and one dependency edge.
  • xtask/src/layers.rs classifies macos-open as L6.

Testing

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings, cargo xtask layers, cargo build --release -p gridcraft: pass.
  • cargo xtask ci: fmt, clippy, test, assets and layers pass. The wasm step did not run locally (no wasm32 target installed). The new crate has only macOS-target dependencies and is not a wasm crate.
  • Cold open: with the app not running, open -a on an .xlsx launched the app with that workbook open. Confirmed through the JSON control channel.
  • Warm open: with the app running, open -a on a second file opened it in the running app. Confirmed through the control channel.

Notes

  • crates/macos-open does not inherit the workspace unsafe_code = "forbid", because Objective-C interop needs unsafe. It is the only crate that opts out. It keeps the no-panic clippy deny list. AGENTS.md / CLAUDE.md may want to record this exception.
  • macOS only. On other platforms install is a no-op and the app does not depend on the crate.

🤖 Generated with Claude Code

GridCraft read files to open only from argv. Finder "Open With",
double-click and `open -a GridCraft file.xlsx` deliver files through the
application:openURLs: Apple Event on the NSApplicationDelegate instead, so
macOS reported that GridCraft cannot open files in that format.

winit 0.30 registers its own application delegate and panics if it is
replaced. So the new crates/macos-open adds application:openURLs: to the
class of winit's delegate at NSApplicationWillFinishLaunchingNotification,
before AppKit delivers the launch documents. The app queues the paths,
wakes egui, and drains the queue in raw_input_hook through the same open
path as argv and drag-and-drop. No-op on other platforms.

crates/macos-open does not inherit the workspace unsafe_code = "forbid",
because Objective-C interop needs unsafe. It is the only crate that opts
out. It keeps the no-panic clippy deny list. No new external crates: objc2,
block2, objc2-foundation and objc2-app-kit were already in Cargo.lock.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@echelon

echelon commented Oct 11, 2026

Copy link
Copy Markdown
Member

Thank you for this! macOS open-documents support has since landed via #164 (apps/gridcraft/src/apple_events.rs), so we're closing this as superseded. (For future reference: we keep unsafe_code = "forbid" across the workspace, so a crate exempt from it with transmute/class_addMethod wouldn't be accepted anyway.)

If you haven't already, come join us on the ArtCraft Discord: https://discord.gg/artcraft

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.

2 participants