Copy link to clipboard
Copied
Suppose, in a two-column textframe, how to find whether a particular word or something is in the first or second column using Indesign javascript?
Copy link to clipboard
Copied
Hi,
var myTextSel = app.selection[0],
columnContentOfSel = myTextSel.textColumns[0].texts[0].contents,
myParentTextFrames = myTextSel.parentTextFrames;
for(var i = 0; i < myParentTextFrames.length; i++){
myColumn = 1;
var currTf = myParentTextFrames;
for(var c = 0; c < currTf.textColumns.length; c++)
{
if(columnContentOfSel == currTf.textColumns
. texts[0].contents) {
alert('column: ' + myColumn);
exit();
}
myColumn = myColumn + 1
}
}
let's start from any textselection.
This'll give you the startcolumn of it.
There may be better solutions
Copy link to clipboard
Copied
Great Hans,
Many thanks for your answer. I had been searching for properties and methods in the Object Model.
Copy link to clipboard
Copied
thx
you may also compare horizontalOffset and / or insertionPoints[0] ... + any other property which is relevant.
Happy scripting
Hans
Copy link to clipboard
Copied
Haha - Hans posted a clue while I was at it.
Copy link to clipboard
Copied
That can be done easier:
var myTextSel = app.selection[0];
if (myTextSel.index < myTextSel.parentTextFrames[0].textColumns[1].insertionPoints[0].index){
// we're in column 1
} else {
// we're in column 2
}
myTextSel.index returns the index of the first insertionPoint of myTextSel. If you compare that with the index of the first insertionPoint of column 2, you know in which column the text selection is.
Peter
Copy link to clipboard
Copied
Superb Peter,
These are really helpful. I will try to modify it so that it can work for customised number of columns, say, one, two or three.
Copy link to clipboard
Copied
Hi Lucky Siva,
I ran across this post while searching for a solution to finding the column position of the text. Did you ever find a solution for customized number of columns?
Would love to hear the solve.
Thanks
-Tushar
Copy link to clipboard
Copied
Hi,
try this as a solution:
var mBegin = app.selection[0].insertionPoints[0],
mFrame = mBegin.parentTextFrames[0],
fBegin = mFrame.insertionPoints[0],
sBegin = mBegin.parentStory.insertionPoints[0],
mColumns = mBegin.parentStory.insertionPoints.itemByRange(sBegin, mBegin).texts[0].textColumns.length,
fColumns = mFrame.insertionPoints.itemByRange(fBegin, mBegin).texts[0].textColumns.length,
sFrames = mBegin.parentStory.textContainers.length,
mFrames = mBegin.parentStory.insertionPoints.itemByRange(sBegin, mBegin).texts[0].parentTextFrames[0].length;
alert("Selection starts\rat column: " + fColumns + " (" + mColumns + " in entire story)\r" + "in a frame no: " + mFrames + " (from " + sFrames + ")");
Jarek
Copy link to clipboard
Copied
That is awesome. Thanks Jarek. Works perfectly.
Copy link to clipboard
Copied
Hi Jarek,
I applied the above solution to my script. But in some instances of using the script i am running across this error. This usually happens in documents with lot of pages where the paragraph style has been applied to a few text frame. While i dont think that should be a problem, I am not able to find the exact reason for this issue. Would you happen to have any clue?
Thanks for your help again
-Tushar
Copy link to clipboard
Copied
Here my complete code for reference:
main();
function main() {
var doc = app.activeDocument;
var sel = doc.selection;
var frame = sel[0];
var story;
var charStyleNames = doc.characterStyles.everyItem().name;
var chosen_cStyle;
var arr=[];
var textFrameXpos = app.selection[0].geometricBounds[1];
var currPara = app.selection[0].paragraphs[0]
var textPointSize = currPara.pointSize;
var longestDialogue;
var tabStopPos;
if (frame && frame.constructor.name === "TextFrame") {
story = frame.parentStory;
} else {
throw "No text frame selected";
}
if (currPara.appliedParagraphStyle.index == 0 || currPara.appliedParagraphStyle.index == 1) {
throw "Please assign a new Paragraph Style to this text. Changing properties of the Root Paragraph Style or Basic Paragraph Style is not recommended.";
}
//Scales frame if the text is overset.
if(story.overflows) {
sel[0].fit(FitOptions.FRAME_TO_CONTENT);
}
// Create dialog box
dialogBox = app.dialogs.add({name:"Indent Preferences", canCancel:true});
with(dialogBox)
{
with(dialogColumns.add())
{
with(dialogRows.add())
{
staticTexts.add ({ staticLabel:"Select a Character Style to apply to the dialog"});
}
with(dialogRows.add())
{
with(dialogRows.add())
{
with(dialogColumns.add())
{
staticTexts.add ({ staticLabel:"Character Style"});
}
with (dialogColumns.add())
{
var dd_charStyle = dropdowns.add({stringList:charStyleNames, selectedIndex:0})
}
}
}
}
}
if (!dialogBox.show())
{
dialogBox.destroy();
exit(0);
}
chosen_cStyle = doc.characterStyles.item(dd_charStyle.selectedIndex);
if (chosen_cStyle.index != 0) {
runBasicGrep(story);
charStyleGrep(story);
setTab();
} else {
runBasicGrep(story);
setTab();
}
function runBasicGrep(story) {
// Reset the Find/Change options
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
// Indent script dialogue
app.findGrepPreferences.findWhat = ":((\\t+)?(\\s+)?)?";
app.changeGrepPreferences.changeTo = ":\\t~i";
story.changeGrep();
// Fix the TV:30 indenting.
app.findGrepPreferences.findWhat = "(?i)(tv)(\\s*?):(\\s*?)(\\d+)";
app.changeGrepPreferences.changeTo = "$1:$4";
story.changeGrep();
//Fix the http web url break
app.findGrepPreferences.findWhat = "(http|ftp|https)(\\s*?)(:)(\\s*?)(\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?)";
app.changeGrepPreferences.changeTo = "$1$3$5";
story.changeGrep();
}
function charStyleGrep(story) {
// Apply the character style only to dialog. Avoids the "TV:30" or web urls.
app.findGrepPreferences.findWhat = "^(?!(?i)tv\\s*?:\\s*?\\d+)(?!(http|ftp|https)(\\s*?)(:)(\\s*?)(\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?)).+:.+";
app.changeGrepPreferences.changeTo = "";
app.changeGrepPreferences.appliedParagraphStyle = app.selection[0].paragraphs[0].appliedParagraphStyle;
app.changeGrepPreferences.appliedCharacterStyle = chosen_cStyle;
story.changeGrep();
}
// if (frame && frame.constructor.name === "TextFrame") {
// story = frame.parentStory;
// } else {
// throw "No text frame selected";
// }
function setTab() {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = ":((\\t+)?(\\s+)?)?";//type the product code here
found = app.activeDocument.findGrep (true);
// Search for all the dialogue characters and store in an array
if(found.length>0)
{
for (j = 0; j < found.length; j++)
{
found
.select(); var mBegin = app.selection[0].insertionPoints[0],
mFrame = mBegin.parentTextFrames[0],
fBegin = mFrame.insertionPoints[0],
sBegin = mBegin.parentStory.insertionPoints[0],
mColumns = mBegin.parentStory.insertionPoints.itemByRange(sBegin, mBegin).texts[0].textColumns.length,
fColumns = mFrame.insertionPoints.itemByRange(fBegin, mBegin).texts[0].textColumns.length,
sFrames = mBegin.parentStory.textContainers.length,
mFrames = mBegin.parentStory.insertionPoints.itemByRange(sBegin, mBegin).texts[0].parentTextFrames[0].length;
// alert("Selection starts\rat column: " + fColumns + " (" + mColumns + " in entire story)\r" + "in a frame no: " + mFrames + " (from " + sFrames + ")");
var mFramePosX = mFrame.geometricBounds[1],
currColWidth = mFrame.textFramePreferences.textColumnFixedWidth,
currGutterWidth = mFrame.textFramePreferences.textColumnGutter;
currGrepSelPos = app.selection[0].characters[0].horizontalOffset,
dialoguePos = currGrepSelPos - (mFramePosX + (currColWidth * (fColumns - 1)) + ( currGutterWidth * (fColumns - 1)));
arr.push(dialoguePos);
}
}
else
{
alert("false");
}
// Check which of the elements in the array is the longest dialogue.
longestDialogue = arr[0];
for (var i = 0; i < arr.length; i++) {
if(arr>longestDialogue) {
longestDialogue = arr;
}
};
// Calculate the tabStopPos
tabStopPos = longestDialogue + (textPointSize/2);
// Check if there is tab stop. Reassign value to it. Else create a new tabstop.
if (currPara.appliedParagraphStyle.tabStops[0] != null) {
currPara.appliedParagraphStyle.tabStops[0].alignment = TabStopAlignment.LEFT_ALIGN;
currPara.appliedParagraphStyle.tabStops[0].position = tabStopPos;
} else {
currPara.appliedParagraphStyle.tabStops.add({alignment:TabStopAlignment.LEFT_ALIGN, position:tabStopPos});
}
}
// Reset document selection and tool.
doc.select(NothingEnum.NOTHING);
app.toolBoxTools.currentTool = UITools.SELECTION_TOOL;
}