Copy link to clipboard
Copied
Hi Experts,
Im trying to sort the arabic text and some letters are colored in green and others are black , it works but after sorting some words green letters color changed to white! , it looks like it dissapeared!, i dont know why! , here is the script and screen shots :
//UNDO Enabled
app.doScript(CatchMyError, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Sort Story");
//Error Catcher for Function
function CatchMyError(){
try {
CheckSelection();
} catch (myError) {
alert (myError,"ERROR!");
exit();
}
}
function CheckSelection(){
// Check that a story is selected
var myCurrentSelection = app.selection[0];
if(app.selection.length < 1 && myCurrentSelection != Story) {
alert ("Plese Select Text Frame First!","ERROR!");exit();
}else{
SortSelection();
}
}
function SortSelection(){
//Create an array of paragraphs by splitting the Story
myArray = app.selection[0].parentStory.contents.split ('\r');
//Sort the array
myArray.sort();
//Join the array as one string separated by hard returns
myString = myArray.join ('\r');
//Replace the contents of the selected story with myString
app.documents[0].textFrames[0].parentStory.texts[0].contents = myString;
}here is screen shots for before and after :
Before Shot
After Shot
here is link for the file and video in weTransfer :
Project Folder and Video
did i make something wrong? please help to fix and thanks in advance
Hi @M.Hasanin, okay please try this script. It is similar to my other, but more streamlined and it sorts every paragraph of the selected story. Please try it out. - Mark
/**
* @discussion https://community.adobe.com/t5/indesign-discussions/sorting-selected-text-frame-or-document-cause-changing-some-word-letters-colors/m-p/13443724
*/
function main() {
var story = sortParagraphsOfStory(app.activeDocument.selection[0].parentStory);
if (story == undefined) {
alert('Please select...
Copy link to clipboard
Copied
Because you are sorting "plain text" - not "text objects" - you are loosing formatting in the before-last line of the code.
Not sure how exactly do this in JavaScript - but you need to:
1) create list of paragraphs - your array - with their indexes,
some text[tab]1
another text[tab]2
...
You can use any character as a separator - [tab] - as long as it would be UNIQUE
3) sort this array,
4) copy WHOLE paragraphs using stored indexes - as text objects and not only their text contents - from the original story to a new story.
Copy link to clipboard
Copied
Thank you for your gudieness @Robert at ID-Tasker
Copy link to clipboard
Copied
Hi @M.Hasanin, Robert has hit the nail on the head! When you change a text object's contents then you will revert the text attributes to whatever was applied to the first character. So we use the contents for sorting, but we don't set the contents (except when I want to remove the contents altogether).
I've written a script to attempt to do what you ask. I haven't tested with Arabic text, so please let me know how it goes. The way it works is to first duplicate the whole text frame, then sort the original paragraphs, then put them into the duplicated text frame (in the new order), and finally move them all back into the original text frame and clean up afterwards. There are various ways to approach this problem, but this is what I came up with.
- Mark
/**
* @discussion https://community.adobe.com/t5/indesign-discussions/sorting-selected-text-frame-or-document-cause-changing-some-word-letters-colors/m-p/13443724
*/
function main() {
var paras = sortParagraphsInTextFrame(app.activeDocument.selection[0]);
if (paras == undefined) {
alert('Please select a text frame and try again.')
return;
}
/**
* Sort Paragraphs of TextFrame.
* @author m1b
* @version 2022-12-25
* @param {TextFrame} textFrame - an Indesign TextFrame.
*/
function sortParagraphsInTextFrame(textFrame) {
if (
textFrame == undefined
|| !textFrame.isValid
|| textFrame.constructor.name != 'TextFrame'
)
return;
// better remove text wrap here or the duplicate
// will push away the paragraphs we want
var wrap = textFrame.textWrapPreferences.textWrapMode;
textFrame.textWrapPreferences.textWrapMode = TextWrapModes.NONE;
// make an empty duplicate text frame
var dup = textFrame.duplicate();
dup.contents = '';
// get all the paragraphs
var paras = textFrame.paragraphs.everyItem().getElements();
// make sure every paragraph has a return before sorting
var returnWasAdded = false;
if (paras[paras.length - 1].contents.slice(-1) != '\r') {
paras[paras.length - 1].insertionPoints[-1].contents = '\r';
returnWasAdded = true;
}
// sort paragraphs
paras.sort(function (a, b) {
var al = a.contents.toLocaleLowerCase(),
bl = b.contents.toLocaleLowerCase();
if (al < bl)
return -1;
else if (al > bl)
return 1;
return 0;
});
// duplicate paragraphs in sorted order into the duplicated text frame
for (var i = 0; i < paras.length; i++)
paras[i].duplicate(LocationOptions.AFTER, dup.insertionPoints[0]);
// empty the original textFrame, and move the sorted paragraphs back in
textFrame.parentStory.contents = '';
dup.paragraphs.everyItem().move(LocationOptions.AT_BEGINNING, textFrame);
// clean up
dup.remove();
textFrame.textWrapPreferences.textWrapMode = wrap;
if (returnWasAdded)
textFrame.paragraphs[-1].characters[-1].remove();
return textFrame.paragraphs.everyItem().getElements();
};
} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Sort Paragraphs');
Edit: Oops! I looked at your sample file, and realised that my script won't work right for your case, because the text you want to sort spans multiple text frames. When I get time I will change my approach to work. - Mark
Copy link to clipboard
Copied
Hi @M.Hasanin, okay please try this script. It is similar to my other, but more streamlined and it sorts every paragraph of the selected story. Please try it out. - Mark
/**
* @discussion https://community.adobe.com/t5/indesign-discussions/sorting-selected-text-frame-or-document-cause-changing-some-word-letters-colors/m-p/13443724
*/
function main() {
var story = sortParagraphsOfStory(app.activeDocument.selection[0].parentStory);
if (story == undefined) {
alert('Please select a text frame and try again.')
return;
}
/**
* Sort Paragraphs of supplied story.
* @author m1b
* @version 2022-12-25
* @param {Story} story - an Indesign Story.
*/
function sortParagraphsOfStory(story) {
if (
story == undefined
|| !story.isValid
|| story.constructor.name != 'Story'
)
return;
// get all the paragraphs
var paras = story.paragraphs.everyItem().getElements();
// add return after last paragraph if necessary
if (paras[paras.length - 1].characters.item(-1).contents != '\u000D')
paras[paras.length - 1].insertionPoints.item(-1).contents = '\u000D';
// sort paragraphs
paras.sort(function (a, b) {
var al = a.contents.toLocaleLowerCase(),
bl = b.contents.toLocaleLowerCase();
if (al < bl)
return -1;
else if (al > bl)
return 1;
return 0;
});
for (var i = paras.length - 1; i >= 0; i--) {
paras[i].move(LocationOptions.AT_BEGINNING, story.textContainers[0]);
}
if (story.paragraphs[-1].characters.item(-1).contents == '\u000D')
story.paragraphs[-1].characters.item(-1).remove();
return true;
};
} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Sort Paragraphs');
Copy link to clipboard
Copied
Thank you very much @m1b
Copy link to clipboard
Copied
You're welcome!
Copy link to clipboard
Copied
Here's a multi-lingual sorter for InDesign:
https://www.indiscripts.com/post/2020/06/introducing-smart-sort-for-indesign-cc-cs
Copy link to clipboard
Copied
Thanks a lot @Peter Kahrel
Find more inspiration, events, and resources on the new Adobe Community
Explore Now