If theres no such function in IDCS2, is there a script that can do it?
Best regards,
Martin
Since ID has no alpha sorting feature I don't know how a script would be
written to accomplish this. [note: I couln't write a script to save my
life, so don't go by me. <g>]
I usually just copy to Word in the rare instances that I need to do this.
Bob
Dave
Bob
This one will. I wrote it about a month ago and had already just about forgotten it! It only does alphabetic sorts. If you want a numeric sort, you'll need to modify the compare function.
It sorts based on the column of the table that holds the active insertion point.
//DESCRIPTION: Sort Table by Selected Column
if (app.documents.length == 0 || app.selection.length == 0) { exit() }
SortSelectedTable(app.selection[0]);
function SortSelectedTable(sel) {
var myTable = findTable(sel);
var myCol = findColumn(sel);
doDaSort(myTable, myCol);
}
function doDaSort(tab, col) {
var data = tab.rows.everyItem().contents.sort(compare);
for( var r=0; r<tab.rows.length; r++ ) {
tab.rows[r].contents = data[r];
}
function compare(a,b) {
return a[col] > b[col]
}
/*
function findColumn(obj) {
if (obj.constructor.name == "Table") { return 0 }
while (obj.constructor.name != "Cell") {
obj = obj.parent;
if (obj.constructor.name == "Application") {
throw "Can't find table"
}
}
return Number(obj.name.split(":")[0])
}
function findTable(obj) {
while (obj.constructor.name != "Table") {
obj = obj.parent;
if (obj.constructor.name == "Application") {
throw "Can't find table"
}
}
return obj
}
To use the script, copy and paste to ExtendScript Toolkit (ESTK) and save with an appropriate name in the form ScriptName.jsx in the Scripts folder of the Presets folder of your InDesign CS2 folder (you could put it in a subfolder if you wish). Then to run the script, set-up the appropriate initial conditions and double-click the script's name in the Scripts palette. ESTK is in the Adobe Utilities folder.
The script will not deal well with merged cells. Indeed, if you have merged cells, I strongly advise against running this script.
Dave
Dave
This is wonderful! I don't need to sort inside tables (not for now at least), so SortParagraphs.jsx will do fine for the moment.
However, I will save your script too Dave, just in case!
There's one thing though, about SortParagraphs.jsx
It does not sort swedish or hungarian or other alphabets with special letters, in a correct way. It throws all those special letters at the end.
Is there some way to fix this? I have my language set to Swedish in InD, but that does not seem to fix the problem.
Regards,
Martin
Here's a link:
Harbs
It should rather sort it:
a á b c i í o ó u ú ü v w x y z å ä ö
but right now it sorts it like this (which is wrong):
a b c i o u v w x y z á ä å í ó ö ú ü
I need an international sorting script, which recognizes all those letters, foremost swedish and hungarian.
I'm looking and looking, but can't find anything that does this.
Regards,
Martin
...so back to manual sorting I guess :D
Thanks anyway!
Regards
Martin
can you contact with me at my email soh...@continentad.com, becoz I have problem with scripting and sure you can solve it
Thanks
All you need to do is to search for all letter - diatric combinations
and replace them with the appropriate encoding. All of them should be
the double encoding sequence except for å, ä, and ö which should be the
combined encoding. A script could further simplify this - maybe I can
write one when I'm bored ;)
Harbs
I don't know extended latin languages, so I have no way of knowing if I
did all the necessary changes. I just used the list you provided. (and
added Caps)
I didn't test this at all. So make sure to test it well before using it
on any important docs!
Please let me know if it does the job.
if(app.documents.length == 0){exit()}
doc = app.activeDocument;
app.findPreferences = app.changePreferences = null;
doc.search("á", false, false, "á");
doc.search("í", false, false, "í");
doc.search("ó", false, false, "ó");
doc.search("ú", false, false, "ú");
doc.search("ü", false, false, "ü");
doc.search("å", false, false, "å");
doc.search("ä", false, false, "ä");
doc.search("ö", false, false, "ö");
doc.search("Á", false, false, "Á");
doc.search("Í", false, false, "Í");
doc.search("Ó", false, false, "Ó");
doc.search("Ú", false, false, "Ú");
doc.search("Ü", false, false, "Ü");
doc.search("Å", false, false, "Å");
doc.search("Ä", false, false, "Ä");
doc.search("Ö", false, false, "Ö");
(It's javascript - save as plain text with a .js or .jsx - for CS2
extention)
Harbs
if(app.documents.length == 0){exit()}
doc = app.activeDocument;
app.findPreferences = app.changePreferences = null;
doc.search("á", false, false, "á");
doc.search("í", false, false, "í");
doc.search("ó", false, false, "ó");
doc.search("ú", false, false, "ú");
doc.search("ü", false, false, "ü");
doc.search("å", false, false, "å");
doc.search("ä", false, false, "ä");
doc.search("ö", false, false, "ö");
doc.search("Á", false, false, "Á");
doc.search("Í", false, false, "Í");
doc.search("Ó", false, false, "Ó");
doc.search("Ú", false, false, "Ú");
doc.search("Ü", false, false, "Ü");
doc.search("Å", false, false, "Å");
doc.search("Ä", false, false, "Ä");
doc.search("Ö", false, false, "Ö");
<edit> Didn't work again :( All the combinations get converted to the combined encoding!
Harbs
You are wonderful, thanks alot for the great effort!
So now it's time for me to test it!
I guess I should run your script first, and then the paragraph sort script.
Let's try it! I'll be back with the results soon :)
Regards,
Martin
ó, ö, ú, ä, é, å, ü, e, á, u, a, o
ends up in:
ó, ö, ú, ä, é, å, ü, e, á, u, a, o
Which actually seems okay here, but in IDCS2, the ó is only a o with an ´ after, same with the ú and the á, the ü is only a u with a little square after.
When I sort it with SortParagraphs.jsx, I get:
a, á, e, o, ó, u, ú, ü, ä, å, é, ö
ä å and é is misaligned. é should be after e, and ä å ö should be: å ä ö.
However, the biggest problem is probably that the script you wrote doesn't translate the letters into the "real" letter, but rather a combination of o and ´ for an example. It looks good when it's pasted in here though :D
My big question would rather be:
Is there a way to add a line to SortParagrahps.jsx, or write a new one, where one can set the alphabet manually? I mean, so one could write that the script should sort the paragraphs after THIS alphabet: a,á,b,c,d,e,é,f,g,h,i,í,j,k,l,m,n,o,ó,ö,ő,p,q,r,s,t,u,ú,ü,ű,v,w,x,y,z
and another script which sort after:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,å,ä,ö
I mean so one could write an own list of what to sort the paragraphs after. This would be very useful for other languages as well, as the french etcetera.
Is this possible? It would solve everything :)
Regards,
Martin
Harbs
I tried with Minion Pro now when you said it, but it seems to be even worse. Now I get the squares even after a when its trying to display an á, and I get the squares after the u too.
The é seems to be fine though.
When using SortParagraph.jsx after running your script, it still sort it by ä å ö, instead of å ä ö at the very end.
Wierd.
Is there a way to add a line to SortParagrahps.jsx, or write a new one, where one can set the alphabet manually? I mean, so one could write that the script should sort the paragraphs after THIS alphabet: a,á,b,c,d,e,é,f,g,h,i,í,j,k,l,m,n,o,ó,ö,ő,p,q,r,s,t,u,ú,ü,ű,v,w,x,y,z
and another script which sort after:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,å,ä,ö
A script that the would sort the paragraphs after a custom alphabet would be very useful for other languages as well, like the french for example.
Is this possible? It would solve everything
Martin
Is there a way to add a line to SortParagrahps.jsx, or write a new one,
where one can set the alphabet manually?
There is. The method in the script, below, is due to www.rawdata.net/developer/php_examples/arrays/languagesort.php
I translated the PHP function there into Javascript and it has worked fine for me so far. You can influence sort order easily. For example, the Polish soft L (L-barred) normally sorts after the Z, but if you want it to sort with the 'plain' L, insert it in the list after the plain L.
The script doesn't do any error checking, so make sure that you have a text frame selected or that the cursor is anywhere in the list to be sorted. Also, the script makes a mess of formatting like italics, bold, and small caps; use it on unformatted lists only.
Peter
list = app.selection[0].parentStory.contents.split('\r')
list.sort( compare );
app.selection[0].parentStory.contents = list.join('\r')
function compare( a, b )
{
if( a == b)
return 0;
else
{
var stop = Math.min( a.length, b.length );
for ( var i = 0; i < stop; i++ )
{
var cmp = compareChar( a[i], b[i] );
if ( cmp != 0 )
return cmp;
}
return ( a.length > b.length ) ? 1 : 0;
}
}
function compareChar( a, b )
{
var characters = 'AŔÁÄBCÇDEČÉFGHIĚÍJKLMNOŇÓÖPQRSTUŮÚÜVWXYZ';
characters += 'aŕáäbcçdečéfghiěíjklmnoňóöpqrstuůúüvwxyz';
var pos_a = characters.indexOf( a );
var pos_b = characters.indexOf( b );
if ( pos_a < 0 && pos_b < 0 ) return 0; // a and b not in chars
if ( pos_a < 0 && pos_b >= 0 ) return 1; // a not in chars, b is
if ( pos_a >= 0 && pos_b < 0 ) return -1; // a in chars, b not
return pos_a - pos_b; // a and b in chars
}
look for 'sort' in indesign and you 'll find this script
Wow, this is what I'm talking about.
I put the text you wrote, in a text file and named it abc_sort.js and also tried with jsx.
However, I get this ERROR when I am running it in IDCS2:
JavaScript Fel.
Felkod: 25
Felsträng: Expected: ;
Rad: 3
Källa: app.selection[0].parentStory.contents = list.join('\r') function compare( a, b )
Feltext: function
Swedish to English
Felkod = Bad code?
Felsträng = Bad string?
Rad = Row
Källa = Source
Feltext = Bad text?
Am I doing something wrong?
Martin
It looks as if you need to add a semicolon at the end of the first and third lines (there is one at the end of the second line), so that the first three lines look like this:
list = app.selection[0].parentStory.contents.split('\r');
list.sort( compare );
app.selection[0].parentStory.contents = list.join('\r');
Sorry about that!
Peter
It's ok, I've made it to run now, it had to look like this in the beginning:
list = app.selection[0].parentStory.contents.split('\r');
list.sort( compare );
app.selection[0].parentStory.contents = list.join('\r');
function compare( a, b ) {
if( a == b)
However, as you said yourself it makes a mess of formatting and as I have several different fonts in each paragraph, it messes it up, deleting tabs and so on.
Is there a way to merge this script with SortParagraphs.jsx so it will sort as good, but with the function of yours, which can sort after a specified alphabet?
I mean, somehow merging SortParagraphs.jsx with your script?
Is that possible?
It would sure solve many problems!
Best regards,
Martin
The script below is a variant of one I use to sort formated paragraphs. It uses a sorting algorithm called Quicksort (which dates back to the 1960s), and is much quicker than the Bubblesort algorithm. It is usually quicker than the Heapsort algorithm used in the script mentioned by Harbs and doopie, especially on long and very randomly sorted lists. On short lists and partially sorted lists, Heapsort may outperform Quicksort. The most efficient sorters are in fact hybrids: they use Quicksort (or a variant) on long lists or long parts of lists, and another sorter, a Heapsort maybe, on short lists or short sections.
Anyway, give it a try. After adding the accent-insensitive bit I've tested it on a few lists and it looks as if it works ok. I've had some problems with selected paragraphs, but that seems to be ok now, though I haven't tried it much that way. Let me know how you fare.
Peter
//Sort selected paragraphs or, with an insertionPoint
//or text frame selected, sort the whole story.
// check() returns a two-item object:
// hrt: check whether a hrt has been added
// par: the selected paragraphs
var result = check();
quicksort( result.par, 0, result.par.length-1 );
// If the check added a hrt at the end,
// then remove it.
if( result.hrt == false )
result.par[-1].characters[-1].remove();
// end ---------------------------------------
// Sort case-insensitively. To sort case-sensitively,
// remove .toLowerCase() (twice)
function quicksort( array, first, last )
{
var i = first; var j = last;
var pivot = array[Math.floor( (first+last) / 2)].contents.toLowerCase();
while( i < j )
{
while( larger_than ( pivot, array[i].contents.toLowerCase() ) > 0 )
i++;
while( larger_than ( array[j].contents.toLowerCase(), pivot ) > 0 )
j--;
if( i <= j )
{
swap( array, i, j );
i++;
j--;
}
}
if( first < j ) quicksort( array, first, j );
if( i < last ) quicksort( array, i, last );
}
function swap( array, p1, p2 )
{
if( p1 != p2)
{
if( p2 - p1 == 1)
array[p1].move( LocationOptions.after, array[p2] );
else
{
array[p2].move( LocationOptions.after, array[p1] );
array[p1].move( LocationOptions.after, array[p2] );
}
}
}
function larger_than( a, b )
{
if( a == b )
return 0;
else
{
var stop = Math.min( a.length, b.length );
for ( var i = 0; i < stop; i++ )
{
var cmp = compareChar( a[i], b[i] );
if ( cmp != 0 )
return cmp;
}
return ( a.length > b.length ) ? 1 : 0;
}
}
function compareChar ( a, b )
{
var characters = 'AŔÁÄBCÇDEČÉFGHIĚÍJKLMNOŇÓÖPQRSTUŮÚÜVWXYZ';
characters += 'aŕáäbcçdečéfghiěíjklmnoňóöpqrstuůúüvwxyz';
var pos_a = characters.indexOf( a );
var pos_b = characters.indexOf( b );
if ( pos_a < 0 && pos_b < 0 ) return 0; // a and b not in chars
if ( pos_a < 0 && pos_b >= 0 ) return 1; // a not in chars, b is
if ( pos_a >= 0 && pos_b < 0 ) return -1; // a in chars, b not
return pos_a - pos_b; // a and b in chars
}
function check()
{
switch( app.selection[0].constructor.name )
{
case 'InsertionPoint' :
case 'TextFrame' : var p = app.selection[0].parentStory.paragraphs; break;
case 'Text' :
// if some paragraphs are selected,
// we need to select whole paragraphs
var p = app.selection[0].paragraphs;
p[0].parentStory.paragraphs.itemByRange( p[0], p[-1] ).select();
return { par: app.selection[0].paragraphs, hrt: true }
default : exit();
}
var temp = true;
if( p[-1].characters[-1].contents != '\r' )
{
p[-1].insertionPoints[-1].contents = '\r';
temp = false;
}
return { par: p, hrt: temp };
}
This sounds really great and I tried it as soon as I read this post. I put your code in a file that ended with .js
But I get an error which says:
JavaScript Error.
Felkod: 2
Felsträng: result is undefined
Rad: 6
Källa: if( result.hrt == false )
Swedish to English
Felkod = Errorcode?
Felsträng = Bad string?
Rad = Row
Källa = Source
I really don't know anything about scripting, so I don't really know what to fix :D I would only make a mess of everything.
Do you know what might be wrong?
I can't reproduce that error, the script works ok for me. What is your selection -- text frame, insertion point, or some paragraphs? What are you trying to sort? If you wish, you can send your problem file to me at kahrel at kahrel plus com.
Peter
I am using InDesign CS2, and whatever I try to sort inside a text frame which are separated by paragraphs (to sort rows with other words), I just get this little error message. It does simply not work here. It looks almost like the first error I got when the ";" was missing, and when I fixed that, plus another thing, it worked all fine. So it seems that something is not right here.
May this be due to that the code you put in this forum has been "re formatted" or "altered" by the forum somehow?
I just copied and pasted what you wrote and put it in a text file with extension: .js ...maybe that is wrong too?
Regards,
Martin
I have seen cases in the past where certain editors don't handle the non-breaking spaces in the forum properly. Perhaps that's what's happening to you. If you're using InDesign CS2, then find ExtendScript Toolkit and paste into a new script there.
Dave
Like Dave, I copied the script from the forum and ran it without any problem, so there's no problem with reformatted text. I've checked the line endings, they're fine. All I can say now is what Dave said: fire up the ESTK and use that. You can start that in two ways. (1) Go to Window > Automation > Scripts to display the Scripts palette, highlight any script you see there, and pick Edit Script from the fly-out. This will start the ESTK. There, Start a new script window (File > New Javascript) and paste the script you copied from the forum into that window. Save the script and try running it again. (2) Find the program in c:\program files\Adobe\Adobe Utilities\ExtendScript Toolkit\ExtendScript Toolkit.exe and double click it (better create s shortcut somewhere).
Good luck with it,
Peter
You guys have to be kidding me :D ...I did just as you told me, I started ESTK, started a New javascript, copied the code from this forum, pasted it in ESTK, and ran it.
Still, it says: "result is undefined"
and it stops on the same row, row 6.
How can this be? I am using WinXP and IDCS2 Swedish.
Martin
I think there might be something wrong with how these forums are displayed for me, as I can clearly see you get the script to work but I don't. There probably is some setting which makes it not-working for me.
Could any of you be kind to send the javascript file, the one you for sure know works to my e-mail? I would be very grateful.
my e-mail:
martin...@hotmail.com
I hope this is not too much trouble for you guys. I really tried everything with copying, pasting and so on, to make this script work, but it still wont work.
Regards,
Martin
Dave
The error message ("result is undefined") is strange because that check function either aborts the script if something is selected that it cannot cope with or it comes up with something. It's also strange that the error is produced in line 6.
So it looks as if the script fails on the line that starts with "quicksort" because it doesn't see the first real line, which says "var result = check()". Looks suspiciously as if something with the line breaks has gone wrong.
Try deleting all the commented lines at the start, i.e. all the lines that start with // so that the first line in the script is
var result = check();
and see if that makes a difference. There are some more commented lines there but if the script produces a different error with those first comments stripped out we know that that's the problem. Good versions have been sent to you anyway, but it is always good to what goes wrong.
Peter
Thanks so much for sending me the original script file, it works like a charm! I just put it in the script folder of InD and ran the script! No problems at all, everything worked just perfect!
So I guess there is some problem with how my web browser displays the forums here. (I use the default Internet Explorer v6)
Anyway, the script is just totally wonderful and sorts my paragraphs after my custom alphabet.
Thanks alot Peter for writing the script, it is a huge success!!!
I should go get some sleep now, thanks once again guys!
...I can't wait to start working tomorrow with the working script :)
Good night,
Martin