You probably have this problem if:
- A few specific songs are missing from a lot of your playlists.
- You can still find those songs in your library and play them.
- You didn't remove them yourself.
- The affected songs tend to be old iTunes Store purchases.
You probably don't have this problem if:
- Whole playlists are gone (different issue).
- The missing songs are missing from your library entirely (also different).
- Only ripped/imported songs are affected (probably different).
The fix is to take a "before" picture of your library, take an "after" picture, compare them, and have the playlists restored to match.
-
Find a "before" snapshot. Music.app keeps automatic library backups in
~/Music/Music/Previous Libraries.localized/. Each subfolder is dated. Pick one from before you noticed the problem. The actual snapshot is a file calledLibrary.musicdbinside that folder. Copy that backup somewhere safe before parsing or experimenting with it. -
Re-authorize your computer if needed. In Music.app: Account → Authorizations → Authorize This Computer. This step matters if any of your missing files were iTunes Store purchases — without it, you can't download them again.
-
Re-download any songs that lost their files. In Music.app, songs with a cloud icon next to them are not downloaded locally. You can ask the agent to do this for you in bulk, or click the cloud icons by hand. If your library is large, you may want to do this after the agent has identified which songs actually need it, instead of re-downloading every cloud-only track.
-
Export your current library. In Music.app: File → Library → Export Library…. Save the XML file.
-
Back up your current library before any changes. Quit Music.app and copy the
~/Music/Music/Music Library.musiclibraryfolder somewhere safe. -
Hand the two snapshots to an AI coding agent. Tell it:
- Where the "before"
Library.musicdbfile is. - Where the "after" XML export is.
- That you want it to find what's missing from your playlists and write a script to restore it.
- Where the "before"
-
Have the agent confirm before changing anything. The re-download often rewrites song info — artist name, album, recording year, genre. Ask the agent to show you what it plans to change and let you pick which fields to revert. Same for any system playlists, like "Purchased", that may have disappeared — you may or may not want them recreated.
-
Review generated scripts before running them. A safe restore script should only create playlists, add tracks to playlists, and optionally update specific metadata fields you approved. It should not delete tracks, delete playlists, run shell commands, touch unrelated files, or access the network.
- Smart playlists, the ones that update themselves, usually don't need fixing because their contents are rule-based. They may still help confirm whether the tracks are present in the library.
- This only works if the missing songs are still in your library. If they're actually gone, you'll need Time Machine or a re-import instead.
- Music.app's playlist IDs sometimes regenerate during major upgrades, so the agent should match playlists by name, not by ID.
- Songs usually have stable Persistent IDs within the same library, as long as the song entry was not deleted and recreated.
- A simple set comparison restores missing playlist membership, but not necessarily the original playlist order. If playlist order matters, the agent needs to compare ordered playlist item lists instead of just sets.
- The same song can legitimately appear in a playlist more than once — silence-spacer tracks, intentional repeats in a tanda, etc. If the agent dedupes by Persistent ID when reading or comparing playlists, those duplicate occurrences will be silently dropped from the restore.
A few specifics that make this job easier:
-
Parsing the backup.
~/Music/Music/Previous Libraries.localized/<dated folder>/Library.musicdbis a private Apple binary database format. Some parts may be compressed and/or encrypted depending on Music.app version, so use a purpose-built parser rather than trying to read it as SQLite. The open-source tooljsharkey13/musicdb-to-jsoncan parse an Apple MusicLibrary.musicdbfile and convert it to JSON with track, artist, album, and playlist data. It requires Python 3.12+, thepycryptodomepackage, and a decryption key that is not included in the repo. The README notes that the key is the same one used by the legacy.itlformat and has been published in similar projects. Other related tools includerinsuki/musicdb2sqlite(note: at time of writing it does not handle playlists, so it isn't enough on its own for this task) andpitetb/AppMusicLibParser. -
Parsing the current export. Python's built-in
plistlibreads the Music.app XML export directly. -
Comparing. Each song usually has a stable Persistent ID. For every user playlist matched by name, compare the Persistent IDs it had in the "before" snapshot with the Persistent IDs it has now. Anything still present in the current library is restorable. Use an ordered comparison if preserving playlist order matters; use set comparison if restoring membership is enough. Do not dedupe Persistent IDs while reading or comparing playlist contents — the same Persistent ID can legitimately repeat within a playlist, and a set-based or first-occurrence-wins approach silently drops those duplicates from the rebuilt playlist.
-
What may have changed during re-download. Apple often rewrites metadata when re-fetching: Artist may switch from your tagging convention such as "Lastname, Firstname" to Apple's full ensemble name; Album Artist often becomes "Various Artists"; Year switches from original recording year to reissue year; Genre may be flattened to a generic value. Surface these diffs to the user before reverting.
-
Restoring. AppleScript via
osascriptworks well. Address tracks by Persistent ID, for example:first track of library playlist 1 whose persistent ID is "...". Make scripts idempotent — check membership before adding to playlists, and only create playlists if missing. Note thatduplicatealways appends to the end of a user playlist; if order matters, plan to rebuild the playlist's contents in the correct order rather than appending. -
Re-downloading via AppleScript. Music.app supports the
downloadcommand on tracks whose cloud status indicates they're available for download. Worth offering as an alternative to clicking by hand. -
Things to ask the user before running anything destructive:
- Which metadata fields to revert and which to leave alone.
- Whether to recreate any disappeared system playlists, like "Purchased", as user playlists, and what to name them.
- Whether any of the playlists that look "lost" were actually duplicates the user deleted intentionally.