Copy link to clipboard
Copied
In a previous question I asked for a list (https://forums.adobe.com/thread/146710) which "jump_over" solved fantastically.
As so often, once it works people start complaining.
I made the script work with 150 names in it, listed surname name (which is how they have to appear in the text), but with the names are sorted alphabetically on the Name (the list comes from IT dep).
Question: is it possible to get the list in the panel in the alphabetical order sorted on the Name (Name Surname), but when placed the name and surname should reversed.
Below the script. When run, you get a panel with all the names, double click inserts the selection at the cursor point.
Inverting "Name Surname" doesn't do the trick, because in mTitles the order would be oke but the insertion is not oke. I've a script that reverses two words, but sometimes names have more than two words in them (ex John Mac Doe).
Thanks already for looking into it.
Copy link to clipboard
Copied
Hi Gert,
What you need seems to be a custom sort on your array mTitles so that the <Name> part of your entries is considered first during the sorting process. As a primary approach you could simply regard the first SPACE character in the string as a separator between <Surname> and <Name>. This way the order remains relevant even when <Name> contains additional spaces. However, if the <Surname> part allows inner space too, then you need to improve your data structure so that <Surname> and <Name> can be properly identified.
A cool trick is to add hidden keys into an Array object. These keys remain transparent to ScriptUI but can be used to save ordering data for additional sort or display features. Here is an example:
var mTitles = ["Alan Turing", "Steve Jobs", "Bill Gates", "John Mac Doe"];
(function(a,i,k,p)
{
i = a.length;
while( i-- )
{
p = (k=a).indexOf(' ');
0 <= p && (k=k.substr(1+p)+', '+k.substr(0,p));
a['_'+a] = k;
}
a.sort(function(x,y){ return x=a['_'+x],y=a['_'+y],(-(x<y)||(x>y)); });
})(mTitles);
// Here your code
var w = new Window("palette", "Title multiselector", undefined);
// etc.
The above function performs two tasks:
• it creates hidden keys in the form "_<ItemString>" associated to <Name>,<Surname> values.
• it reorders mTitles items with respect to those hidden key-value pairs.
This way you can also handle the associated <Name>,<Surname> sequence of any item using the "_" (underscore) prefix.
E.g. mTitles["_John Mac Doe"] => "Mac Doe, John"
Hope that helps.
@+
Marc
Copy link to clipboard
Copied
@marc: not figured it out yet (being a novice scriptwriter), I'll post a screenshot of what should be happening when running the script.
So, in the list (I receive a xls-list from IT, alphabetically ordered Name Surname, which is good for looking up) they should stay like that but in the text they should appear in the reversed order.
I tried your example, but nothing special happens. I did insert a"_" before a name to see what it gives, but the result is simply the _+ the name (_Almaci Meyrem) gives _Almaci Meyrem after running the script.