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

Cut and Anchor after a particular unicode char

Enthusiast ,
Sep 18, 2023 Sep 18, 2023

Copy link to clipboard

Copied

 

@Peter Kahrel @m1b 

Hi

Textframe named as "source" has 17 paragraphs. (It can be seen in the screenshot marked with red in source.png)

 

Page 1 has none applied

Page 2 and 3 has Master Page B applied

Page 4 onwards has Master Page A applied

From Page 1 till the last page, textframes are named as "destination". (screenshot destination.png)

 

I am looking for a particular unicode value (\x{0612}) in the "destination" textframes. There are exactly 17 of those characters. The count of these characters matches exactly with the paragraphs of the "source" textframe.  (screenshot unicode_value.png)

 

I want to cut one paragraph from "source" and anchor it just after the char \x{0612} and apply object style of "rukunoframe". Screenshot attached of how it should look. (screenshot final.png)

 

And when all 17 of them are anchored, the source will be deleted.

 

Here is the script which I am using (took help of a gentleman in this group long time back).

The issue is that its only anchoring one paragraph from the "source" to page "one".

 

Here is the code:

 

 

//get handle to document
var doc = app.documents[0];

//unicode string format
var unicode = /\u0612/;
var unicodeTxt = '\\x{0612}';

//get handle to textFrames
var txtFrame = doc.textFrames;

//get handle to paragraphs
var des = txtFrame.itemByName('destination');

//get paragraphs with unicode character and its position via findGrep() method
myResults = findUnicodeGrep(des);
//alert(myResults.length);
//loop through paragraphs with character '0612'
for (var i = myResults.length - 1; i >= 0; i--) {
  //insertion point
  var point = myResults[i][0].insertionPoints.item(myResults[i][1]);
  // add frame at insertion point
  try {
    tf = point.textFrames.add();
    tf.properties = 
    {
      geometricBounds :[0,0,10,30],
      strokewidth: 0,
      fillColor : "None",
      name : "test"+(i+1).toString(),
      contents : txtFrame.itemByName('source').paragraphs[i].contents,
      appliedObjectStyle: doc.objectStyles.item("RukuNoFrame")
    };
    tf.anchoredObjectSettings.properties = {
      anchoredPosition: AnchorPosition.ANCHORED,
      anchorPoint : AnchorPoint.TOP_RIGHT_ANCHOR,
      verticalReferencePoint : VerticallyRelativeTo.LINE_XHEIGHT
    }; 

//Clear object style overrides if any
tf.clearObjectStyleOverrides();
  } catch (error) { }
}

//Finally remove the source text frame
txtFrame.itemByName('source').remove();

// search for unicode character in given frame
// return a multidimensional array with the paragraph and the unicode character position
// index[0], paragraph
// index[1], position
function findUnicodeGrep(txtFrame) {
  app.findGrepPreferences = app.changeGrepPreferences = null;
  app.findGrepPreferences.findWhat = unicodeTxt;
  
var myFound = txtFrame.findGrep();
  
  //alert(myFound.length);
  var tp = [];
  for (var i = 0; i < myFound.length; i++) {
    var temp = [];
    //object paragraph
    var p = myFound[i].paragraphs.item(0);
    //content of the paragraph
    var t = p.contents;
    //unicode character index in the paragraph
    var result = unicode.exec(t);
    temp[0] = p;
    temp[1] = result.index;
    tp.push(temp);
  }
  return tp;
}

 

TOPICS
How to , Scripting

Views

308

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 1 Correct answer

Community Expert , Sep 19, 2023 Sep 19, 2023

Votes

Translate

Translate
Community Expert ,
Sep 18, 2023 Sep 18, 2023

Copy link to clipboard

Copied

This may fix it. Change this:

var myFound = txtFrame.findGrep();

to

var myFound = txtFrame.parentStory.findGrep();

If that doesn't work, maybe ask the gentleman who wrote the script originally. He may still be around.

(Also, I don't think it's correct to address a post to specific people. This is a public forum. And you're sure to get more feedback when you don't address individuals.)

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
Enthusiast ,
Sep 18, 2023 Sep 18, 2023

Copy link to clipboard

Copied

@Peter Kahrel My apologies. I should not have done that.

I made the change but its anchoring all at one place. Screenshot attached.

Actually I forgot that gentleman's name. I will look again.

Thanks once again and sorry for the trouble.

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 ,
Sep 19, 2023 Sep 19, 2023

Copy link to clipboard

Copied

LATEST

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