I'm not JS expert so can't give an example code - but you can't - or at least shouldn't - use "external" search methods on the text contents of InDesign's text objects - you need to use built in "plain" and grep find methods.
Otherwise you'll end up with unexpected results - for example, if uou try to use JS's RegExp method - "\s" (any white space) will remove index markers, etc. - but if you use built in grep methods - InDesign will ignore those.
That's why I was suggesting using built in find functionality - rather than iterating through all paragraphs.
But like I've said - I'm not JS guy so won't give you a specific code examples - but there is plenty on this forum or on Google.
Hi @Bedazzled532 , a third way to search following the @Robert at ID-Tasker suggestion.
This way is using findGrep method:
1. Add a new variable with the unicode character in string format. Used for findWhat property in findGrepPreferences.
var unicodeTxt = '\\x{002A}';
2. New function with findGrep method. This function returns a multidimensional array with the paragraphs and the index of the unicode character.
function findUnicodeGrep(txtFrame) {
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = unicodeTxt;
var myFound = txtFrame.findGrep();
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;
}
The full script (you can delete the unused functions):
I'm pretty sure that this script can be improve but I think that it does its job.
//get handle to document
var doc = app.documents[0];
// var character = '*';
// unicode value for '*' character
var unicode = /\u002A/;
//unicode string format
var unicodeTxt = '\\x{002A}';
//get handle to textFrames
var txtFrame = doc.textFrames;
//get handle to paragraphs
var des = txtFrame.itemByName('destination');
//get paragraphs with character '*' and its position
// myResults = findCharacter(character, des);
//get paragraphs with unicode character and its position
// myResults = findUnicode(unicode, des);
//get paragraphs with unicode character and its position via findGrep() method
myResults = findUnicodeGrep(des);
//loop through paragraphs with character '*'
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("rukus")
};
tf.anchoredObjectSettings.properties = {
anchoredPosition: AnchorPosition.ANCHORED,
anchorPoint : AnchorPoint.TOP_RIGHT_ANCHOR,
verticalReferencePoint : VerticallyRelativeTo.LINE_XHEIGHT
};
} catch (error) { }
}
txtFrame.itemByName('source').remove();
// search for character '*' in a text frame
// return a multidimensional array with the paragraph and the '*' position
// index[0], paragraph
// index[1], position
function findCharacter(character, txtFrame) {
var p = txtFrame.paragraphs;
var tp = [];
for (var i = 0; i < p.length; i++) {
var result = p[i].contents.indexOf(character);
var temp = [];
if (result != -1) {
temp[0] = p[i];
temp[1] = result;
tp.push(temp);
}
}
return tp;
}
// search for unicode character in a text frame
// return a multidimensional array with the paragraph and the unicode position
// index[0], paragraph
// index[1], position
function findUnicode(unicode, txtFrame) {
var p = txtFrame.paragraphs;
var tp = [];
for (var i = 0; i < p.length; i++) {
var temp = [];
var result = unicode.exec(p[i].contents);
if (result !== null) {
temp[0] = p[i];
temp[1] = result.index;
tp.push(temp);
}
}
return tp;
}
// 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();
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;
}