The command "changeGrep" does a "change all" -- there is no equivalent of the 'find next' and 'change, find next' buttons.
Ordinarily, all that's needed is to remove the first paragraph from an active story is this:
(and I'm pretty sure you can work it out, since you are already copying from it) but just for fun you could try this Pure GREP solution:
app.findGrepPreferences.findWhat = "\A.*\r";
again replacing with nothing.
(It's better to use the 'remove' function, though.)
If your first two paragraphs has unique paragraph style names then you can inlcude the following in your script,
app.findGrepPreferences.appliedParagraphStyle = "parastyle name";
Be sure that this style is applied only to the the paragraph that you want to remove.
If you this command this is equal to "Change All " option in the find/change palette.
Jongware - thanks, I think that's what I was looking for. I couldn't remember whether there was a 'find start of story' symbol (\A) in ID although I thought there was one - it's not on Mike Witherell's handy crib sheet (at least not the CS3 one). Thanks - I think you've saved me again! I couldn't think how to do it in JS but I've got your snippet to work on an isolated test document but when I try it on my main test doc I've still got a problem. If I add the following code
it removes my footer and running head paragraphs. If I do an alert(myStory.paragraphs.contents) before they are removed it doesn't just show the first paragraph in my document it shows the copy for the running heads three times and then the footer copy. I tried to get it to make myStory equal only the stories in the first text frame in the document but I couldn't get that to work. Any thoughts?
Green4ever - unfortunately at the stage this script will be run nothing will be styled. In fact this script contains various GREPs that will apply styles throughout the file automatically. Mind you now I know how to find the first paragraph I can now apply that style here!
I can do it with GREP of course but I'd like to learn why Jongware's method isn't working in this situation.
1 person found this helpful
the variable "myStory" won't point to just a single story, but ... to "every story". So that's why the next line removes more than you intended!
What the "everyItem" thingy internally does is still unclear to me, but that doesn't keep me from using it -- but only when you absolutely, positively want to perform an action on every item
Now the usual examples say something like
myStory = app.activeDocument.stories.item(0); // access first story
but that's No Good. Even if your first story is the one you are targeting today, tomorrow, in a different document, it'll be story #1, or #5, or #255. There is no general rule that states the first story is always the largest one, or something like that.
If this is going to be used with a document template, the best way to identify the target story is by giving the first text frame a unique label. Otherwise, you'll have to think of something else ... A couple of other strategies:
1. You are already using GREP to find stuff. If one of those searches is guaranteed to return just text in the one story you are interested in, you can use this.
2. You mention headers and footers. If your main text is the only one that runs through multiple text frames, you can loop over all stories and check which one runs through than a single text frame.
3. ... (I'm pretty sure there are some more strategies ...)
That's the trouble with cherry-picking bits of script I find from the net. It often works but can have unforeseen consequences if I don't fully understand what's going on. I need to spend a week in a dark room learning JS from the ground up but that's never going to happen! Just can't quite get my head round the object model, how different things relate and keep enough methods etc in my head for when I need them.
Anyhoo, your code does find the first para but it is the running head copy on the master page. It will always be the first para on the first page (not a master page item) that I am trying to delete. Can we get it to look at the first page of the document so myStory is taken from there? I tried but I couldn't get the syntax (or more likely my understanding of the object model) correct. If not, I have done it with GREP and it works fine.
(like the Jackie Brown reference!)
That would be something like
myStory = app.activeDocument.pages.textFrames.parentStory;
(like the Jackie Brown reference!)
Ooo you're sharp today. Bet most readers didn't pick that one up!
Or just add this:
app.findChangeGrepOptions.includeMasterpages = false;
(off the top of my head, can't remember the exact formulation).
That's it! It works now. I have got as far as myStory = app.activeDocument.pages.textFrames but I didn't have parentStory.
Thanks Jongware - greatly appreciated.
I initially thought the reference was to Pulp Fiction but I watched JB again just a couple of months ago and it popped into my head. Great film - very underrated. What can I say - I'm a film geek!
Thanks Peter - I did use that when I did it with GREP and it works. I just wanted to try and get my head round the JS side of things.