ok, I know I said I'd be leaving it alone, but here is the REAL final final version of the script, now that I've solved the final couple of outstanding issues:
1) If you're rebuilding all collections on your kindle with the script (the "--rebuild" command-line option) then those collections will now automatically be initially sorted into alphabetical order when viewed from the kindle's "home" page viewed by "collections". This is done by faking up the lastAccess times on the collections, and you can use the new "--sort" option to force the script to similarly "sort" all the collections on your kindle when performing an update.
2) The weird asin thing I mentioned above. This isn't "fixed" as such, but I've come up with several workarounds, which are described in the now-huge comment at the top of the script file.
Basically, I noticed that all the affected books were ones purchased from Amazon, whether they were stripped of DRM or not. So the simplest workaround is to reconvert them twice: First convert them to epub format and then convert the epubs back to mobi format. That'll strip out that embedded metadata asin and allow them to be managed as normal.
As an aside: If you use the embedded asin explicitly in the collections.json (i.e. don't do the conversion mentioned above) then Calbire can't associate that with the eBook. So presumably Calibre can't (or isn't) following that embedded-asin chain to link the books to their collections.
3) I fixed a typo in the code which was preventing creation of collection from series (I don't do this myself, which is why I didn't notice that typo earlier)
And that's DEFINITELY it: The script now does everything I wanted it to do in the first place and so, short of some workarounds to avoid having to restart the kindle and/or extract embedded asins from such files, there's nothing left for me to do to it.
[EDIT] See new thread
https://www.mobileread.com/forums/sho...47#post1255247 for script file