Indesign Script for PDF Object Layer Options - javascript

I have an indesign document with multiple pages. each page has a linked pdf in it. each pdf has 3 layers within it and in order to turn these layers on or off, you have to right click, select Object Layer Options, and then manually turn on or off layers.
I would like to loop through all my pages and turn on a layer in the PDF using a script. i have been messing with graphicLayerOptions.graphicLayers but keep running into an error when telling it to turn the currentVisibilty=true;
var myDocument = app.activeDocument;
var docLength = myDocument.pages.length;
var myPages = myDocument.pages
for (var i = 0; i < docLength; i++) {
var labelPlaceholder = myDocument.allGraphics;
var labelArtwork = labelPlaceholder[0];
var artworkLayers = labelArtwork.graphicLayerOptions.graphicLayers;
artworkLayers.item("Die Copy").currentVisibility = true;
}

i got it working...l
var myDocument = app.activeDocument;
var docLength = myDocument.pages.length;
var myPages = myDocument.pages
for (var i = 0; i < docLength; i++) {
var labelPlaceholder = myPages[i].allGraphics;
var labelArtwork = labelPlaceholder[0];
var artworkLayers = labelArtwork.graphicLayerOptions.graphicLayers;
artworkLayers[0].currentVisibility = true;
}

Just in case. In InDesing (Illustrator, etc) you have two options to get an item from a collection.
By its number:
var layer = app.activeDocument.layers[0];
By its name:
var layer = app.activeDocument.layers.itemByName("Die Copy");
Later options is less reliable. Not all collections has this method. I don't know if it (PDF layers) is the case, though.

Related

Method push is not adding a value to a vector that previously had the same value

The goal of the code I'm going to present is to create a aux vector that will contain petri nets transitions, arcs and places. I'm dividing a petri net into several groups, each group is a transition with respective input arcs and places.
The issue is the following: After I put the info in the first position of the aux vector, I'm unable to put a place with the same id of the place of the previous group. For example, if I have a transition with place_id=1 and place_id=2, and the next transition have place_id=2 and place_id=3, the code doesn't write the value place_i=2 in the vector for the second group.
function conflict() {
var id = [];
var source = [];
var target = [];
var aux = [];
var cont = [];
var places = pnml.getElementsByTagName("place");
var arcs = pnml.getElementsByTagName("arc");
var transitions = pnml.getElementsByTagName("transition");
for (var i = 0; i < transitions.length; i++) {
target.push(transitions[i].getAttribute("id"));
aux.push([]);
for (var j = 0; j < arcs.length; j++) {
if (arcs[j].getAttribute("target") == transitions[i].getAttribute("id")) {
id.push(arcs[j].getAttribute("id"));
source.push(arcs[j].getAttribute("source"));
//console.log(arcs[j].getAttribute( "source" ));
}
}
//console.log(id);
//console.log(arcs);
//console.log(places);
aux[i].push(id, source, target);
//id.length=0;
target = [];
source = [];
id = [];
}
}
Image of the platform with console open
Thanks in advance
Without knowing a whole lot for the issue, try to change this
aux.push([]);
to this
aux[i]=[];
So that you initialize and fill using an index instead of a push and an index later, for consistency.
Let me know if it helps
EDIT:
Also this
aux[i].push(id, source, target);
to this (maybe? )
aux[i].push({ id: id, source:source, target:target});
You probably want to keep objects in aux, so you need to push an object, not 3 parameters like that

I am trying to get this code to copy swatch groups with the colors too

I found some code that almost works for what i need, but i need it to copy the groups as well. Any ideas?
#target Illustrator
var theFile = new
File('C:/location/colors.ai');
open(theFile,null);
var doc2 = app.activeDocument, doc = app.documents[1];
var thisSw, newSw;
for(var i=0; i<doc2.swatches.length; i++){
thisSw = doc2.swatches[i];
if(thisSw.name == "[Registration]" || thisSw.name == "[None]"){
continue;
}
newSw = doc.swatches.add();
newSw.name = thisSw.name;
newSw.color = thisSw.color;
};
doc2.close(SaveOptions.DONOTSAVECHANGES);
Here is the original code for this as well https://forums.adobe.com/thread/2285302
I have tried changing swatches out for swatchGroups, but it only grabs the groups no colors. It did not understand how to fix that. I am not sure if there is a var i need to add for that or not.

Text frames disappearing after applying a ParagraphStyle

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/mytemplate.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 = "Lorem ipsum dolor... \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/mybook.indd"));
// Save the document as an PDF
doc.exportFile(ExportFormat.pdfType, new File("/c/mybook.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. 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 ?
In the end I came up with the following which fixed my problem. I don't know if i'm overengineering a bit or this is under the rules of the art.
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[j].label === frameName) {
return allItems[j].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();
You are applying master spread and then modifying it. I think the better approach would be to override text frames on the page. Try this:
// define template
var indesignTemplate = new File("/e/mytemplate.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; // you are missing closing semicolon here
// get paragraph style
var paragraphStyle = doc.paragraphStyles.item("_2.ondertitel_bladzijde");
for (var i = 0; i < page.textFrames.length; i++) {
var textframe = page.textFrames.item(i);
if (textframe.label === "flow") {
textframe.override(page);
for (var x = 0; x < 5; x++) {
// insert another new paragraph
textframe.parentStory.insertionPoints.item(-1).contents = "Lorem ipsum dolor... \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/mybook.indd"));
// Save the document as an PDF
If you have to change master spread for some reason, try to modify it first and then apply to the page.

Plotly data from nested for loop

In c# codebehind I define a few Lists this way:
public List<string> divs = new List<string>();
public List<List<string>> names = new List<List<string>>();
public List<List<List<string>>> labels = new List<List<List<string>>>();
public List<List<List<double>>> longitude = new List<List<List<double>>>();
Quite large lists I know but I feel it's necessary for getting all my info from my source organized correctly.
in JS I serialize these like this:
var divArr = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(divs)%>;
var names = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(names)%>;
var lbl = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(labels)%>;
var long = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(longitude)%>;
And then I try to do a function to plot all this on separate graphs. 10 graphs in total on my page that can have several lines on all of them. Trying to make my page as dynamic as possible. So I have a function to loop through all of this and try to plot it all.
function doGraph(){
for(index = 0; index < divArr.length; ++index){
(function() {
var data = [];
for(indx = 0; indx < lbl[index].length; ++indx){
var trace = {
name: names[index][indx],
x: lbl[index][indx],
y: long[index][indx],
mode:'lines'
};
data.push(trace);
}
var gd = document.getElementById(divArr[index]);
plotly.newPlot(gd,data);
})();
}
}
And it ALMOST works. Every graph seems to plot the first set of data given to it but nothing afterwords. Maybe I've been staring at this too long but I just can't see what I'm doing wrong here but I'm sure it's something I've just over looked. Or maybe I'm overreaching and I can't do this sort of thing? Any insight is appreciated!
So I found out the problem was with the serialization from my lists to js arrays. Apparently js serialize can't quite handle the level of multidimensional list I was going crazy with. So I fixed it by making the lists one level less deep and made another list to keep track of how "deep" they are in this fashion:
C# Codebehind:
public List<List<string>> names = new List<List<string>>();
public List<int> numObjs = new List<int>();
public List<List<string>> labels = new List<List<string>>();
public List<List<double>> longitude = new List<List<double>>();
JS Serialization:
var divArr = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(divs)%>;
var names = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(names)%>;
var numO = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(numObjs)%>;
var lbl = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(labels)%>;
var long = <%=new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(longitude)%>;
Then the JS function has the real changes with a loop in this way:
function doGraph(){
var cur = 0;
for(var index = 0; index < divArr.length; ++index){
var data = [];
var top = cur + numO[index];
for(var indx = cur; indx < top; ++indx){
data.push({
name: names[indx],
mode:'lines',
x: lbl[indx],
y: long[indx],
});
cur++;
}
var gd = document.getElementById(divArr[index]);
Plotly.newPlot(gd, data,
layout , {scrollZoom:true,modeBarButtonsToRemove:['sendDataToCloud'],showLink:false,displaylogo:false});
}
}
Also my function within a function was definitely unnecessary as #flipperweid said.

Parse all values from a XML element using Google Apps Script?

I am trying to parse forex values (all of them) for http://indicador.eof.cl/rss XML feed into a Gooogle Sites trough Google Apps Script.
The script as follow>
function doGet(){
var response = UrlFetchApp.fetch("http://indicador.eof.cl/rss").getContentText();
var parsedResponse = Xml.parse(response, false);
var root = parsedResponse.getElement();
var entries = root.getElement('channel').getElements("item");
for (var i=0; i<entries.length; i++) {
var e = entries[i];
var title = e.getElement("title").getText();
var description = e.getElement("description").getText();
}
var app = UiApp.createApplication();
var TopVar = app.createHorizontalPanel();
TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px"));
TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px"));
app.add(TopVar);
return app;
}
The issue is the code just bring me the first value no all of them, what i am forgetting?
Best Regards,
Try to move TopVar.add(...); lines inside for loop :
var app = UiApp.createApplication();
var TopVar = app.createHorizontalPanel();
for (var i=0; i<entries.length; i++) {
var e = entries[i];
var title = e.getElement("title").getText();
var description = e.getElement("description").getText();
TopVar.add(app.createLabel(title).setStyleAttribute("fontSize","12px"));
TopVar.add(app.createLabel(description).setStyleAttribute("fontSize","12px"));
}
Actually, I know nothing about google-apps-script. But your current code logic seems a bit off. It doesn't make use of values of local variables declare inside for loop (e, title, and description). Value of those variables changed in every iteration without any code using it.

Categories

Resources