Copy link to clipboard
Copied
I want to check whether the currently selected frame contains a frame separator (~R) or a page separator (~P).
But it seems like I'm not getting any results.
var s = app.selection[0].texts[0].contents;
alert(s);
var what = "~R|~P"
var res = getGrepSearch(what, app.activeDocument);
alert(res);
function getGrepSearch(fp, s) {
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
app.findChangeGrepOptions.properties = { includeHiddenLayers: true, includeLockedLayersForFind: true, includeLockedStoriesForFind: true, includeMasterPages: true }
app.findGrepPreferences.findWhat = fp;
//return app.findGrep()
return s.findGrep()
}
Set s to the text not the contents, try this:
//var s = app.selection[0].texts[0].contents;
//get the text frame not the contents
var s = app.selection[0].texts[0];
//returns an empty array
//var what = "~R|~P"
var what = "~R"
var res = getGrepSearch(what, s);
//the first found item
alert(res[0].contents)
function getGrepSearch(fp, s) {
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
app.findChangeGrepOptions.properties = { includeHiddenLayer
Copy link to clipboard
Copied
Set s to the text not the contents, try this:
//var s = app.selection[0].texts[0].contents;
//get the text frame not the contents
var s = app.selection[0].texts[0];
//returns an empty array
//var what = "~R|~P"
var what = "~R"
var res = getGrepSearch(what, s);
//the first found item
alert(res[0].contents)
function getGrepSearch(fp, s) {
app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
app.findChangeGrepOptions.properties = { includeHiddenLayers: true, includeLockedLayersForFind: true, includeLockedStoriesForFind: true, includeMasterPages: true }
app.findGrepPreferences.findWhat = fp;
return s.findGrep()
}
Copy link to clipboard
Copied
Hi rob day.
Thank you very much.
So I needed to search the text.
Also, I had the wrong search scope.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
If it can be found, no problem.
When there is no ~R to find, there is an error.
There's also alert(resP[0].contents); which pops up the image below.
But alert(resP[0].contents == "FRAME_BREAK"); pops up false.
Originally it was going to be
resP[0].contents==SpecialCharacters.FRAME_BREAK
Copy link to clipboard
Copied
You have to check if the array is empty:
var res = getGrepSearch(what, s);
if (res.length) {
alert(res[0].contents)
} else {alert("Nothing Found")}
Copy link to clipboard
Copied
Okay, I'll take your word for it. I'm voiding these.
//try {
// if (resR[0].contents == SpecialCharacters.FRAME_BREAK) {
// var fb = 1
// }
//}
//catch (e) { }
//try {
// if (resP[0].contents == SpecialCharacters.PAGE_BREAK) {
// var pb = 1
// }
//}
//catch (e) { } //}
Copy link to clipboard
Copied
I suddenly realized that res.length isn't always reliable either.
When the selected text box is a table, it will also throw an error.
At this point, var s = app.selection[0].texts[0]; is invalid.
Is there a way to make the function `getGrepSearch(fp, s)` support null?
Copy link to clipboard
Copied
For the error that is reported when it is not found.
I just catch it off, probably not a good idea.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now