Skip to content

Commit

Permalink
Add Sort Paragraph Line length
Browse files Browse the repository at this point in the history
Based on x location of last character in paragraph
  • Loading branch information
Bruno Herfst committed Jul 22, 2019
1 parent 87c93d8 commit 8798aa6
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
133 changes: 133 additions & 0 deletions Scripts/Story_Sort_Paragraph_LastCharX.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

/*
Story_Sort_Paragraph_LastCharX.jsx
Version 1.0
Indesign CS5 javascript
Bruno Herfst 2019
*/

#target "InDesign"


function compareEndX(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}

function bubbleSortLastX( story ) {
if (story.overflows) {
alert("Skipped story " + story.id + ": Contains overflow text." );
return false;
}

myParagraphs = story.paragraphs;

// Make sure we end with a new line char
if(myParagraphs[0].parentStory.insertionPoints[-1].index == myParagraphs[-1].insertionPoints[-1].index){
myParagraphs[-1].insertionPoints[-1].contents = "\r";
myCleanUp = true;
} else {
myCleanUp = false;
}

do{
myItemMoved = false;
myCounter = 0;
do{
locA = myParagraphs.item(myCounter).characters.item(-1).horizontalOffset;
locB = myParagraphs.item(myCounter+1).characters.item(-1).horizontalOffset;
if(locA > locB){
myParagraphs.item(myCounter).move(LocationOptions.after, myParagraphs.item(myCounter+1));
myItemMoved = true;
}
myCounter ++;
}while (myCounter < myParagraphs.length-1);
myCounter = myParagraphs.length-1;
do{
locA = myParagraphs.item(myCounter).characters.item(-1).horizontalOffset;
locB = myParagraphs.item(myCounter-1).characters.item(-1).horizontalOffset;
if(locA < locB){
myParagraphs.item(myCounter).move(LocationOptions.before, myParagraphs.item(myCounter-1));
myItemMoved = true;
}
myCounter --;
}while(myCounter > 1);
}while(myItemMoved != false);

if(myCleanUp == true){
myParagraphs[0].parentStory.characters[-1].remove();
}

return true;
}

function getStories( doc ) {
var mySelection = doc.selection;
var selected_stories = [];

if( mySelection.length === 0){
// Do all stories
for(var i = 0; i < doc.stories.length; i++){
selected_stories.push(doc.stories.item(i));
};
} else {
// Do stories in selection
for(var i = 0; i < mySelection.length; i++){
switch(mySelection[i].constructor.name){
case "TextFrame":
case "Paragraph":
case "Text":
case "Line":
case "Word":
case "Character":
case "TextColumn":
case "TextStyleRange":
case "InsertionPoint":
var pStory = mySelection[i].parentStory;
if(notInArray(pStory, selected_stories)){
selected_stories.push(pStory);
};
break;
default:
alert("Could not find a solution for " + mySelection[i].constructor.name);
break;
};
};
};

return selected_stories;
};

function notInArray(needle, array) {
for(var i = 0; i < array.length; i++) {
if(array[i] === needle) {
return false;
}
}
return true;
};

function main() {
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if (app.documents.length != 0) {
var doc = app.activeDocument;
var stories = getStories( doc );
var storyLen = stories.length;
if (storyLen == 0) alert("Could not find any stories active document.");
for(var i = 0; i < storyLen; i++) {
bubbleSortLastX( stories[i] );
}
} else {
alert("Please open a document and try again.");
}
}

main();
1 change: 1 addition & 0 deletions Scripts/TextFrame-Inline_Remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function main() {
case "Text":
case "TextStyleRange":
case "TextColumn":
case "Paragraph":
app.selection[0].textFrames;
break;
case "TextFrame":
Expand Down

0 comments on commit 8798aa6

Please sign in to comment.