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:
Pages object
- 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[0]
. Note that pages[0]
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 pages[0]
.
- 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 ↓
Page object
- 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
app.activeDocument.pages[0].allPageItems[0]
.
- You can find out what its type is like this:
alert( app.activeDocument.pages[0].allPageItems[0].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 ↓
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 pageItem
.
- In this case though, if we find out that the pageItem is a textframe using this:
alert( app.activeDocument.pages[0].allPageItems[0].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 ↓
TextFrame object
- 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
activeDocument.pages[0].allPageItems[0].texts
- Moving on ↓
Texts object
- In here there's a method called
everyItem()
: Returns every Text in the collection.
. This is probably what we want.
- Then let's dive inside the single
text
object ↓
Text object
- Well well... What do we have here... A method called
applyParagraphStyle()
- 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[0];
var textFrame1 = page1.allPageItems[0];
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.