Is there a script or way for batch adding Relationships to a Work from the search results page?

POV: You want to add a bunch of stray works to a Soundtrack Work page. Is there by chance a script floating around out there, or some other way to do this in bulk? I checked Userscripts and Greasyfork but didn’t see anything.

Is this what you are looking for?

It seems the bookmarklet will choose the default relationship type which is “medly of”, so you need to find a workaround to change it to “parts of”.

OK I found a workaround by temporarily modifying the bookmarklet to this by specifying the default value of parameter o:

javascript:(()=>{async function t(t){const e=await fetch("/ws/js/entity/"+t);return MB.entity(await e.json())}function e(t){const e=MB.sourceRelationshipEditor??MB.releaseRelationshipEditor;return new MB.relationshipEditor.UI.AddDialog({viewModel:e,source:e.source,target:t})}const i=prompt("MBIDs of entities which should be related to this entity:");i&&(async(i,o=281,n=!1)=>{for(let a of i){const i=e(await t(a));o&&(i.relationship().linkTypeID(o),n&&i.changeDirection()),i.accept()}})(Array.from(i.matchAll(/[0-9a-f-]{36}/gm),t=>t[0]))})();

281 is the link type ID of the “parts of” relationship.

3 Likes

Thanks, I’ll try that out in a bit. So the workflow would be to copy the MBIDs using your script, and then go to the Work page’s Relationship Editor and click on the bookmarklet to add them? Seems good enough, thanks for the help.

There seems to be a snag with this method. At first glance, it looks like it works, but you’re not actually able to submit the relationship entries because the drop-down “type” menu itself remains blank, so MB doesn’t register it when you hit the submit button.

Try this one:

javascript:(()=>{async function t(t){const e=await fetch("/ws/js/entity/"+t);return MB.entity(await e.json())}function e(t){const e=MB.sourceRelationshipEditor??MB.releaseRelationshipEditor;return new MB.relationshipEditor.UI.AddDialog({viewModel:e,source:e.source,target:t})}const i=prompt("MBIDs of entities which should be related to this entity:");i&&(async(i,o=281,n=!1)=>{for(let a of i){const i=e(await t(a));o&&(i.relationship().linkTypeID(o),n&&i.changeDirection()),i.accept()}})(Array.from(i.matchAll(/[0-9a-f-]{36}/gm),t=>t[0]))})();

I changed "ca8d3642-ce5f-49f8-91f2-125d72524e6a" to 281, it seems I misunderstood what “link type ID” means.

1 Like

I was actually surprised that your modification seemed to work using the relationship type’s MBID – but I also didn’t try to submit. Interesting that it seemingly displays the new relationships correctly nevertheless…

I hope you didn’t get crazy staring at the minified code of the bookmarklet, the source code (which is linked via the section heading on GitHub) is much more readable and better documented. Actually there was even an example on how to use a custom relationship type (using the internal numeric ID), but it hasn’t really been promoted as such:

But this doesn’t matter any longer, I’ve published a new version of the bookmarklet which checks for an active relationship dialog and tries to use its selected relationship type (before it falls back to the old behaviour of using the default relationship type). I’ve also updated the description in my bookmarklet support thread accordingly.

3 Likes

Indeed it’s amazing that it shows the right relationships despite my mistake, but I didn’t dig deeper to find out why.

Not really since the code is short enough, I did take a look at the source code but continue to work on the minified version because that’s straightforward. :rofl:

Well done! That’s really smart and convenient.

1 Like