To make it easier: I am pretty sure that the "The" is not necessary any more to sort better.
So I would suggest that you use an action of the type "Format value" or Convert>Tag-Tag for ALBUMARTISTSORT
Format string: $regexp(%albumartist%,^The ,)
This simply
Please watch out for artists that seem to need the "The" like "The The" or "The Band", "The XX".
Have you tried it?
It removes any leading "The " and leaves the rest.
And as I said: if you add the trailing "The" will not make any difference for sorting.
The other problem that I have is a lot of albumartistsort fields are something like "Drake, Nick" and I would like it to be "Nick Drake.
I can see how your script would solve my problem for the "The" though. But how do I solve the first problem? All my Album Artist fields are the way I want them, I just wanted to port over what is in Album Artist to albumartistsort.
This is a simple Convert Tag-Tag and set Albumartistsort to be Albumartist.
But why would you want these to be identical? It defeats the purpose of having the sort field. It is intended to allow you to have the Albumartist displayed as expected but sorted under a different way. Removing articles like A, An, and The are the most obvious ones in English. Your other example of Drake, Nick is to have that artist sorted under the D's as you would find them in store.
Otherwise you can leave this field u filled and most players and media managers will default back to the standard field.
For what would you need such an if-then construction?
The regular expression works without that.
The condition is "built-in", so to say, as the regular expression returns the original string if the pattern has not been found.
It would be extremely tricky to separate the artists that have swapped names from those that actually have a comma as they consist of a list.
E.g.
Bell, Book and Candle,
Earth, Wind and Fire
Dave Dee, Dozy, Beaky, Mick & Tich
Would turn into something like "Book Bell and Candle" or "Bell and Candle Book", "Earth, Wind and Fire" into "Wind Earth and Fire" or "Wind and Fire Earth" ...
The easiest way would be to delete all ALBUMARTISTSORT fields and start from scratch. And "scratch" would mean: fill only those ALBUMARTISTSORT fields that really have to be different from the ALBUMARTIST field.
You could filter for files with a starting "The " with %albumartist% MATCHES "^The "