I'm not going to answer everything in the original question since the answer you posted with the code examples seemed have an actual issue that needed explaining.
I'm also taking a somewhat general approach so that you can use this knowledge to hunt down other things you might need.
Looking at the code you posted, you're skipping over a few things. Your variable
myParagraph points to a page with the index 1, not an item in the page. In other words, you are trying to add the paragraph style to the page.
This could be a good reference site for you. It seems to have a search functionality. That said, I personally prefer this one.
So to kinda break it down:
- In this page you can, for example, see that
pages.item() method "Returns the Page with the specified index or name." and not a
pageItem, like your code seems to assume.
- So to get a specific page, we could do:
app.activeDocument.pages. Note that
pages is equal to
pages.item(0). The method
item() may be confusing to someone who is new to INDD scripting, so decided to use the shorter
- This page is littered with green links that lead to the single
page object. That's where we need to go to find out how to get the items in a page ↓
- From here we can find
allPageItems object, with this description:
Lists all page items contained by the Page.
- There's also
textFrames object, that we could use if we only needed to find text frames in the page, but perhaps not this time since you want to target other item types too.
- So right now the path we probably would've taken is something like
- You can find out what its type is like this:
alert( app.activeDocument.pages.allPageItems.constructor.name );. This is kind of important so you can find out what to do next.
- A little side note: with
allPageItems we can get all page items in the page, but you could also for example loop through all items inside a layer and check the page they are on. This could make the script a bit faster if you have like 3000 pages and you know the layer only contains 50-100 items. In which case the difference is looping through 3000 pages or 50-100 items. I wouldn't worry about it though.
- Now to dive into the single
pageItem object ↓
- Good to know: In this page at the bottom under the heading
Object of, we can find hint towards how to get more specific with the
- In this case though, if we find out that the pageItem is a textframe using this:
alert( app.activeDocument.pages.allPageItems.constructor.name );... We can start figuring out how to access methods and properties that are specific to this item type.
- You could search this page for "TextFrame" and find a link, but you could also use the search functionality to look for
TextFrame. Either way your next destination is decided ↓
- Here we can find
TextFrame specific properties and methods.
- From here we can find
Texts, which says:
A collection of text objects.
- Ok, sounds like the right place.
- Now the code would be something like
- Moving on ↓
- In here there's a method called
Returns every Text in the collection.. This is probably what we want.
- Then let's dive inside the single
text object ↓
- Well well... What do we have here... A method called
- At this point you might be wondering how on earth could anyone even get to this point without someone's guidance. Well, if the starting point is that you want apply a paragraph style and you found out, let's say via googling or perhaps the Indesign scripting guide, that there is such a method as
applyParagraphStyle. The easiest method is to use the search functionality of the site on
applyParagraphStyle. If you do that, one of the results is the
text object and that's pretty much all the hint you need.
With all of this combined, we can finally use
applyParagraphStyle like so:
var doc = app.activeDocument;
var page1 = doc.pages;
var textFrame1 = page1.allPageItems;
var text = textFrame1.texts.everyItem();
var datum = doc.paragraphStyles.item('datum');
text.applyParagraphStyle( datum, true );
I hope this helps.
Just a general reminder that: Indesign scripting guide also has quite a few good examples on how to do this and other things.