• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Anchor position based on columns (scripting)

Explorer ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Hi, is it possible to know in which column you find something using scripting, I then want to anchor an object but it's location depends on the location of the text I find.

Thanks!

TOPICS
Scripting

Views

327

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Aug 04, 2022 Aug 04, 2022

And if your columns are text columns, not table columns, you can use something like this:

sel = app.selection[0];
frame = sel.parentTextFrames[0];
$.writeln (frame.insertionPoints.itemByRange (0, sel.index).textColumns.length-1);

P.

Votes

Translate

Translate
Community Expert , Aug 05, 2022 Aug 05, 2022

Quite right, I used just one text frame. Here's the correct version:

sel = app.selection[0];
frame = sel.parentTextFrames[0];
$.writeln (frame.parentStory.insertionPoints.itemByRange (
  frame.insertionPoints[0].index, 
  sel.index)
.textColumns.length-1);

 P.

Votes

Translate

Translate
Community Expert ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Hi @yeshayac,

Try the following code by selecting some text in a cell, it will alert the index of the column in which the selection was made. The col variable contains the reference to the column object

var sel = app.selection[0]
if(sel.parent instanceof Cell){
	var col = a.parent.columns[0]
	alert(col.index)
}

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

And if your columns are text columns, not table columns, you can use something like this:

sel = app.selection[0];
frame = sel.parentTextFrames[0];
$.writeln (frame.insertionPoints.itemByRange (0, sel.index).textColumns.length-1);

P.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Yes they are text columns, thanks! I tried this code but I couldn't get it to work passed the first page and I think it's because sel.index is based on the story so it goes out of the range of the frame.insertionpoints

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

I just realized Story also considers textColumns so I put that instead of frame, I'm just thinking of a way to get the first insertion point of a frame from the story itself so the range isn't from 0 to the selection...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

LATEST

Quite right, I used just one text frame. Here's the correct version:

sel = app.selection[0];
frame = sel.parentTextFrames[0];
$.writeln (frame.parentStory.insertionPoints.itemByRange (
  frame.insertionPoints[0].index, 
  sel.index)
.textColumns.length-1);

 P.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Hi @yeshayac 

Loop over the columns of the frame.

In my example I loop over the colums of a selected frame and I find/change in a even column, changing the properties of the searched word.

sel = app.selection[0];
var numCols = sel.textColumns.length;
var mySearch = "IndesignScript";
var myResult;

//Set the find options.
app.findChangeTextOptions.includeFootnotes = false;
app.findChangeTextOptions.includeHiddenLayers = false;
app.findChangeTextOptions.includeLockedLayersForFind = false;
app.findChangeTextOptions.includeLockedStoriesForFind = false;
app.findChangeTextOptions.includeMasterPages = false;

for (i = 0; i < numCols; i++) {
  indexCol = i + 1;
  // Find&Change only in the even columns
  if (indexCol % 2 == 0) {
    app.findTextPreferences.findWhat = mySearch;
    myResult = sel.textColumns[i].findText();
    //Set the properties of myResult
    app.changeTextPreferences.fontStyle = "Bold";
    app.changeTextPreferences.capitalization = Capitalization.ALL_CAPS;
    app.changeTextPreferences.underline = true;
    //Change
    sel.textColumns[i].changeText();
    //Reset find/change
    app.findTextPreferences = app.changeTextPreferences = null;
  }
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

Thanks! this seems like a good solution, I'm going to try it

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 04, 2022 Aug 04, 2022

Copy link to clipboard

Copied

I ended up going with one of the solutions above because it was easier to implement in what I already had, thanks anyways!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Aug 05, 2022 Aug 05, 2022

Copy link to clipboard

Copied

@Peter Kahrel solution it's always the best option, but just for fun I improve my script:

My case: You want to anchor an object to a word or a paragraph style and it depends if the result of the search it is in an even or odd column your object it's different.

In my example you want add a bullet if the word "IndesignScript" has the paragraph style "Title" applied and, like i said before, the bullet you anchor depends if the column it is odd or even.

The bullets are in an external library and has its own object style applied.

var myDoc = app.documents[0];
var myResult;
var sel;
// var numCols = sel.textColumns.length;
//Text Frames of the story of the selected frame
var myStory_arr;

//--Searching criteria--
//If you want to search a word activate next line
//if you don't comment next line
var mySearch = "IndesignScript";
var allMyParagraphStyles = myDoc.paragraphStyles;
var myParagraphStyle = allMyParagraphStyles.itemByName("Title");


//--Open Library--
//path to your own library
var myLibrary = app.open("/Users/yourUser/Documents/library.indl", true, OpenOptions.OPEN_ORIGINAL);
//get the objects of the library
var myEvenBullet = myLibrary.assets.item("bulletEven");
var myOddBullet = myLibrary.assets.item("bulletOdd");

//--Set the find options--
app.findChangeTextOptions.includeFootnotes = false;
app.findChangeTextOptions.includeHiddenLayers = false;
app.findChangeTextOptions.includeLockedLayersForFind = false;
app.findChangeTextOptions.includeLockedStoriesForFind = false;
app.findChangeTextOptions.includeMasterPages = false;
app.findChangeTextOptions.wholeWord = true;

//Check if selection
if (app.selection.length == 1) {
  sel = app.selection[0];
  //Check if selection is a Text Frame
  if (sel instanceof TextFrame) {
    //Place the TextFrames of the story in an array
    myStory_arr = sel.parentStory.textContainers;
    //--Loop over the text frames of the story of the selected frame
    for (txtFr = 0; txtFr < myStory_arr.length; txtFr++) {
      //number of columns of every TextFrame
      var numColsTextFrame = myStory_arr[txtFr].textColumns.length;
      //Iterate over the columns
      for (m = 0; m < numColsTextFrame; m++) {
        indexCol = m + 1;
        app.findTextPreferences.appliedParagraphStyle = myParagraphStyle;
        //If you search a word activate next line if you don't comment it
        app.findTextPreferences.findWhat = mySearch;
        myResult = myStory_arr[txtFr].textColumns[m].findText();
        // Find&Change in the even columns
        if (indexCol % 2 == 0) {
          for (k = 0; k < myResult.length; k++) {
            //get the insertion point
            var insertionPoint = myResult[k].insertionPoints[0];
            //place the anchor object
            myEvenBullet.placeAsset(insertionPoint);
          }
        } else {
        //Find&Change in the odd columns
          for (j = 0; j < myResult.length; j++) {
            var insertionPoint = myResult[j].insertionPoints[0];
            myOddBullet.placeAsset(insertionPoint);
          }
        }
        app.findTextPreferences = app.changeTextPreferences = null;
      }
    }
  } else {
    alert("Please select a Text Frame");
  }
} else {
  alert("Please select a Text Frame");
}

Captura de pantalla 2022-08-05 a las 13.24.23.pngCaptura de pantalla 2022-08-05 a las 13.24.36.png

I'm sure that my script it can be improve to be more efficient but that's another history and it's summer holidays

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines