After another long gap I'm coming back to scripting again and I've probably forgotten most of what I learned here before. I'm trying to capture the contents of the first paragraph (which identifies what sort of article it is) so I can then analyse it and do different things depending on what it finds. I think I can do the analysis (famous last words!) but I'm stuck at the first hurdle.
If I use a para style to identify the first para (which obviously means I'd have to ask the client to style it) it works okay;
var myDoc = app.activeDocument;
//Find the first para in the story (which contains the article type)
app.findGrepPreferences.appliedParagraphStyle = "Identifier";
firstPara = myDoc.findGrep();
articleType = firstPara.contents;
But if I try to identify the first para using GREP (using app.findGrepPreferences.findWhat = "\A^.+$";), which would be better as the para wouldn' thave to be styled, I get an "undefined is not an object error". Running a manual GREP search in ID with \A^.+$ finds the first para so it must be the syntax is wrong in my code.
Can anyone point out where I am going wrong please?
I'm not sure I've understood, but if you're just trying to get the first
paragraph of something, then, for example, given a story myStory, the
first paragraph would be
or given a text selection:
mySelection = app.activeDocument.selection
myFirstPara = mySelection.parentStory.paragraphs
Should be app.findGrepPreferences.findWhat = "\\A^.+$";
Aaargh! Thanks, I'd forgotten about having to escape that again. It works now.
Thanks also to Arïel - I'd forgotten about that method.