如何用AppleScript将曲目添加至iTunes当前播放列表并按顺序播放
Hey there! I’ve run into this exact quirk with Music.app (formerly iTunes) before—when you add a track to a playlist that’s already playing, the app doesn’t automatically update its active playback queue, so the new track gets left out until you restart the playlist. Here’s how to fix this without interrupting your current listening:
The Core Issue
Music uses a snapshot of the playlist as its active playback queue when you start playing. Any tracks added after playback begins aren’t included in this queue by default. We need to either refresh the queue or update the playlist’s playback state to include the new track.
Solution 1: Refresh Playback from the Current Track (Best for Order Playback)
This method re-initiates playback of the playlist starting from the track you’re currently listening to. It rebuilds the queue to include your new track, and won’t skip or interrupt the current song—it just ensures the new track comes up in order when its turn arrives.
tell application "Music" -- Define your target playlist and track path set targetPlaylistName to "My Playlist" set newTrackPath to POSIX file "/Users/YourName/Music/MyNewTrack.mp3" -- Replace with your track's path -- Add the track to the playlist set targetPlaylist to playlist targetPlaylistName set newTrack to add newTrackPath to targetPlaylist -- Check if the target playlist is actively playing if player state is playing and current playlist is targetPlaylist then -- Get the position of the currently playing track in the playlist set currentPlayingTrack to current track set currentTrackIndex to index of currentPlayingTrack in targetPlaylist -- Refresh playback starting from the current track (includes the new track in the queue) play targetPlaylist starting at currentTrackIndex end if end tell
Solution 2: Append to the User Queue (Best for Custom Queues)
If you use Music’s user queue (the custom list of tracks you’ve manually queued up), you can add the new track directly to the end of this queue. This keeps your existing queue intact and ensures the new track plays after all currently queued tracks.
tell application "Music" set targetPlaylistName to "My Playlist" set newTrackPath to POSIX file "/Users/YourName/Music/MyNewTrack.mp3" set targetPlaylist to playlist targetPlaylistName set newTrack to add newTrackPath to targetPlaylist if player state is playing and current playlist is targetPlaylist then -- Append the new track to the end of the active user queue append newTrack to end of user queue end if end tell
Quick Notes
- Double-check your track path formatting: use
POSIX filefor Unix-style paths, oraliasfor Mac-style paths (e.g.,alias "Macintosh HD:Users:YourName:Music:MyNewTrack.mp3"). - For shuffle mode: Solution 1 will re-shuffle the playlist (including the new track) starting from your current song. Solution 2 will add the new track to the end of the shuffle queue.
内容的提问来源于stack exchange,提问作者ibrewster

