Repository navigation
Run Music's AppleScript off the main thread - #307
Merged
Merged
Conversation
NSAppleScript.execute(_:) compiles and runs each script in one block on a serial queue, so the main thread stays free while the target app answers. A task that is cancelled before its script starts runs nothing; the queue checks again after compiling, before any Apple event is sent. Errors keep their AppleScript number and message.
Music's now-playing, control, and seek scripts now run on the shared AppleScript queue instead of blocking the main actor while Music answers.
A seek reads the current track's duration and sets the position in one script, so no other iMCP script can run between the two. They are still separate Apple events, and Music can change tracks between them on its own. The position is clamped to zero and the maximum in Swift, and to the track's duration in the script when Music knows it.
mattt
force-pushed
the
mattt/music-applescript-queue
branch
from
November 4, 2026 11:37
5e01236 to
46e02b8
Compare
mattt
marked this pull request as ready for review
November 4, 2026 11:39
The test relied on a half-second script starting before the second script was queued. On a slow runner the second script could run first. Now the test holds the serial queue with a semaphore until it has cancelled the queued script.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The tests do not verify that script execution stays off the main actor, and sleep-based cancellation timing can cause failures under CI load.
2 open issues
What changed in this PR
Moves Music’s AppleScript execution to a serial background queue so slow scripts no longer block iMCP’s main actor.
Changes:
- Adds an asynchronous script runner with cancellation checks and preserved AppleScript error details.
- Combines seek duration lookup and position updates into one script.
- Adds runner and seek-script tests and registers the new test file.
| File | Description |
|---|---|
| Tests/imcp-serverTests/MusicTests.swift | Tests combined seek-script generation and compilation. |
| Tests/imcp-serverTests/AppleScriptTests.swift | Adds execution, cancellation, and error-preservation tests. |
| iMCP.xcodeproj/project.pbxproj | Registers AppleScript tests in the test target. |
| App/Services/Music.swift | Uses asynchronous script execution and a combined seek script. |
| App/Extensions/Foundation+Extensions.swift | Adds the serial background AppleScript runner and structured errors. |
Review effort: Balanced
The test only checked the script's result. Now it holds the serial queue, starts a script from the main actor, and checks that the script has not finished until the queue is released.
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The execution-context change still needs macOS validation with real Music Apple events, Automation prompts and denials, and an unresponsive Music process, since the new tests cover only local scripts.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This PR runs Music's AppleScript on a serial background queue instead of the main thread, so a slow or unresponsive Music no longer blocks iMCP's main actor. A new
NSAppleScript.execute(_:)compiles and runs each script in one block on the queue. It skips a script whose task is cancelled before the script starts, and it keeps AppleScript's error number, so Music still reports a missing Automation permission clearly.Seeking now reads the track's duration and sets the position in one script, so no other iMCP script can run between the two steps.