Copy link to clipboard
Copied
I don't have a lot of Indesign experience to say the least but I was asked to investigate if it was possible (using indesign server of scripting) to start a new document, apply a master spread page, insert some paragraphs and applying some paragraph styles.
The solution I came up is this
// define template
var indesignTemplate = new File("/e/boek_NL.indt");
// open the template
var doc = app.open(indesignTemplate);
// get master page
var masterPage = doc.masterSpreads.item("A-Master");
// get first page
var page = doc.pages.item(0);
// apply master page to our first page
page.appliedMaster = masterPage
// get paragraph style
var paragraphStyle = doc.paragraphStyles.item("_2.ondertitel_bladzijde");
for (var i = 0; i < masterPage.textFrames.length; i++) {
var textframe = masterPage.textFrames.item(i);
if (textframe.label === "flow") {
for (var x = 0; x < 5; x++) {
// insert another new paragraph
textframe.parentStory.insertionPoints.item(-1).contents = "text \r\r";
}
for (var x = 0; x < textframe.paragraphs.length; x++) {
textframe.paragraphs.item(0).applyParagraphStyle(paragraphStyle);
}
}
}
//Save the document (fill in a valid file path).
doc.save(new File("/c/boek_nl.indd"));
// Save the document as an PDF
doc.exportFile(ExportFormat.pdfType, new File("/c/boek_nl.pdf"));
// close the document.
app.documents.item(0).close();
This works and can see my text when I comment out the applyParagraphStyle code.
From the moment that I try to apply a paragraphStyle the text gets hidden, that in the generated PDF as the INDD file. When I then open the saved indd file in Indesign and command+shift click in the empty text frame, the text appears and I also see an extra layer appearing.
I also tried applying CharacterStyles in more or less the same way but that doesn't give any problems.
I assume that the problem lays in the fact that I'm may approaching this in the wrong way ?
Copy link to clipboard
Copied
Is it the texts overflowed?
Copy link to clipboard
Copied
Hi,
If your goal is to apply the same paraStyle to every para (entire story) - use:
textframe.parentStory.appliedParagraphStyle = paragraphStyle;
Basically it does not look like a wrong way (iterate through paragraphs using 'x' instead of '0').
Search for the reason in your paraStyle definition or textFrame properties.
Jarek
Copy link to clipboard
Copied
Hi,
text is not overflowwing and setting for example the paragraph style through parentStory.apliedParagraphStyle results also in the same behaviour (Although in reality it will be that I may need to set each paragraph with a different paragraph style)
I will try to investigate further
Copy link to clipboard
Copied
Is your goal to apply and format text on MasterSpread?
Do it first and apply Master to page after, maybe...
Jarek
Copy link to clipboard
Copied
No, I must stress that I'm a total noob in the indesign department so I could be doing something horrible wrong.
My idea is that I select a master spread, apply it to a page, fill in the text frames (with a scripting label flow) that came with the spread, alter some paragraphs and save it as a PDF for now.
.recompose() was also something I tried but didn't fix my problem unfortunately.
Copy link to clipboard
Copied
In the end I came up with the following which fixed my problem. I don't know if i'm overengineering a bit.
I do have some questions as I couldn't find a reference to override in the scripting guide. What is the meaning of override on a page object ? I was under the impression that appliedMaster would insert al the frames defined in the master spread.
var indesignTemplate = new File("/e/boek_nl.indt");
// open the template
var doc = app.open(indesignTemplate);
// get master page
var masterPage = doc.masterSpreads.item("A-Master");
// get first page
var page = doc.pages.item(0);
// apply master page to our first page
page.appliedMaster = masterPage
// get paragraph style
var paragraphStyle = doc.paragraphStyles.item("_2.ondertitel_bladzijde");
// get text frame
function getTextFrame(pageObj, frameName) {
var allItems = pageObj.appliedMaster.pageItems.everyItem().getElements();
for(var j=0;j<allItems.length;j++)
{
if(allItems
.label === frameName) { return allItems
.override(pageObj); }
}
}
var textFrame = getTextFrame(page, "flow")
// insert a paragraph 5 times
for (var x = 0; x < 5; x++) {
textFrame.parentStory.insertionPoints.item(-1).contents = "text \r\r";
}
// apply paragraph style
textFrame.paragraphs.item(0).applyParagraphStyle(paragraphStyle);
//Save the document (fill in a valid file path).
doc.save(new File("/c/boek_nl.indd"));
// Save the document as an PDF
doc.exportFile(ExportFormat.pdfType, new File("/c/boek_nl.pdf"));
// close the document.
app.documents.item(0).close();
Copy link to clipboard
Copied
Hi,
pageItem.override(page) ==> means 'do Command+Shift+Click' on pageItem at page
Items from master are displayed on page (not moved).
pageItem.override(page) method copies pageItem to page
So in current version script is adding content and applying style inside textFrame on the page - not on the master.
(looks like it works only for 1st textFrame with label "flow", right?)
Jarek
Copy link to clipboard
Copied
Aha thanks for the explanation, I really appreciate it!
Yes, I have a page provided by me with 2 text frames with scripting labels called flow. They seem to be linked/threaded?
What happens with the script now is that it fills the first column and when there is no room left it continues in the second on. That is more or less the behaviour that I wanted and I can also apply paragraph styles without making my text disappear.
This part works but I'm not sure this is under the rules of the art.
Copy link to clipboard
Copied
Hi Glenn,
whatever fit your needs "is under the rules of the art".
If you want to use master spreads as repository for template frames you could also store the frames on masters that will not be used in the document and simply duplicate the frames to the pages where they are needed. Or you could do a library file with template frames and add assets to a page. There are various alternatives. Also open a "master" document and duplicating frames to a new document will be a valid workflow.
Cheers,
Uwe
Copy link to clipboard
Copied
What is the Command+Click for? Just to select the text frame, so it will redraw?
If that's all that you need, maybe InDesign Server needs an explicit kick to recompose the page after changing: TextFrame : recompose
Try with adding this after ln. 29:
textframe.recompose();