A Python script that reads the Safari bookmarks, lets Claude Haiku choose a folder for each one and writes the result as a bookmarks HTML file that I import into Safari.
Status
Working. The whole collection (296 distinct bookmarks) was restructured with --all and imported. A first Inbox run sorted 50 of 76 bookmarks; the rest stays in the Inbox for manual sorting.
How it works
- Read
~/Library/Safari/Bookmarks.plist, read-only. The terminal needs Full Disk Access for this. - Fetch the meta description of every page, in parallel.
- Ask Claude Haiku through the Claude API for a folder per bookmark, with a confidence value. Only answers from 0.7 upward are applied.
- With
--apply, write an import file with all bookmarks in their new order and a second file with the old state as a backup.
Without --apply every run is a dry run. The API key comes from the macOS Keychain.
Modes
- Default: sorts the folder "Inbox" into existing folders or new, specific ones such as "AI / Claude Code". Format folders such as Videos or Podcasts are allowed; platforms such as YouTube or GitHub are not topics.
--folder PATH: splits one folder into subfolders.--all: rebuilds the whole collection in two stages. First the model designs a folder structure from all titles, domains and old folders (8 to 20 top folders, at most four levels), and a second call checks only the hierarchy. Then every bookmark is sorted into that structure. The plan is saved, and--all --applyapplies exactly that plan without new API calls.
Workflow
I decided against swapping single folders. Every --apply produces a complete import file, and the routine is always the same:
- Run the script with
--apply. This has to happen before deleting, because the script reads the bookmarks from Safari. - Delete all bookmarks in Safari except the Reading List.
- Wait until Mac and iPhone are both empty.
- Import the file and drag the folders into place.
The script counts the bookmarks before writing and aborts if any are missing.
Safari bookmarks and iCloud
What the project found out about Safari bookmarks and iCloud.
Plist edits do not sync
Safari keeps its bookmarks in ~/Library/Safari/Bookmarks.plist. A script can change the file directly while Safari is closed. Safari on the Mac then shows the change, and it survives a restart. iCloud never transfers it to the iPhone, though. In the same test, a bookmark added by hand in Safari did sync, so iCloud itself worked: Safari only uploads changes it tracked itself.
Restoring a plist copy with cp has the same problem: Mac and iCloud drift apart.
What works: the HTML import
A bookmarks file in the Netscape HTML format, imported with File → Import From → Bookmarks HTML File, counts as a change by Safari and reaches the iPhone. This was tested first with a small test folder and then with the whole collection.
- Safari never merges an import into existing folders. It creates a new import folder that contains everything.
- Favorites and Bookmarks Menu arrive as ordinary folders. Their content has to be dragged to the right place by hand.
Structure of the plist
BookmarksBaris the Favorites bar,BookmarksMenuthe Bookmarks Menu.- Folders at the top level of the sidebar sit directly under the root.
com.apple.ReadingListis the Reading List. It is not part of the generated import file, so it must never be deleted along with the bookmarks.
Recovery
Older states of the bookmarks can be restored on iCloud.com under Data Recovery, Restore Bookmarks. iCloud then pushes them to all devices.
Lessons from building it
- Writing the plist does not sync. See Safari bookmarks and iCloud above.
- Fetching descriptions: Kaggle returns 404 without a real Safari user agent. YouTube puts the description about 800 KB into the page, so the script reads up to 3 MB in chunks. Still, 30 of 76 Inbox bookmarks had no description.
- Hierarchy: The first structure put Bangkok, Thailand, Singapore and Southeast Asia side by side. An explicit part-of rule (a city inside its country, a country inside its region), a second pass that checks only the hierarchy and a depth of four levels fixed it.
- Shortened paths: The model sometimes leaves out a level of a path. The script accepts a shortened path if exactly one folder matches it.
- Weak matches: As long as new folders were allowed only when no folder fits, the model chose a roughly fitting existing folder with low confidence, and the bookmark stayed in the Inbox. The rule "a roughly fitting existing folder is worse than a new one that fits exactly" raised the sorted share from 27 to 50 of 76.
- Threshold: Lowering it from 0.7 to 0.6 would also let wrong matches through, so I sort the unclear rest by hand.
- Duplicates: Bookmarks with the same URL are kept only once.
- Topic
-
- AI / Language Models What I use language models for: agents, local models, RAG and small programs in which a model handles one task.
- Uses
-
- Claude API Anthropic's programming interface to the Claude models. Billed separately from a claude.ai subscription, with keys from the Claude Console.
- macOS Keychain Password store built into macOS. Scripts can keep API keys there instead of in files or the shell profile and read them with the security command.
- Python General-purpose programming language with highly readable syntax. The standard for data analysis, automation and quick prototypes.
- Tags