I am trying to add some JS to my pdf creation process to save some time. My goal is to basically click a button generate a PDF and have it print.
Right now I have:
var pp = this.getPrintParams();
pp.interactive = pp.constants.interactionLevel.full;
this.print(pp);
The only problem is that I don't know how to set the number of copies to print. I want pass a variable and print that many copies. The problem is that I can't really find any documentation discusses the methods in this class.
I know, this question is old. But I was looking for a solution, too. And I found here, that it should work with the following:
//no of copies
var n = 3;
var pp = this.getPrintParams();
//here is the magic
pp.NumCopies=eval(n);
this.print(pp);
For me, this is working with tcpdf and Adobe Reader.
Try this
var n = 3;
var pp = this.getPrintParams();
pp.NumCopies=eval(n);
this.print({bUI: false,bSilent: true,bShrinkToFit: true,printParams:pp});
Not possible, which is a good thing; since otherwise some websites would specify a high number and people who don't expect this would accidentally print lots of pages instead of just a single one.
You should call print method twice or more times, like the following:
this.print({bUI: false,bSilent: true,bShrinkToFit: true});
this.print({bUI: false,bSilent: true,bShrinkToFit: true});
I think it's not possible to set the number of copies.
See also:
Adobe's Print Production documentation
Related
In Javascript or PHP this is fairly easy but doesn't seem to work for extendscript.
I have a search function that stores it's results in a var. I want to see what is in the var after the function runs. So usually I use a alert() but InDesign comes up with a box containing [object Word].
Here is what I do:
var myFound = myDoc.findGrep();
alert(myFound);
InDesign throws me this box:
If I do the following:
alert(myFound.length);
Any ideas how to "reveal" my content?
If someone wants to know how to fix it:
Create an array around the for loop and gather the information in it.
After that alert the array like this:
var myFound = myDoc.findGrep();
var result = new Array();
for(i=0; i<myFound.length; i++)
{
result[result.length] = myFound[i].contents;
}
alert(result);
The result of a grep search is an array. You need loop through it and access the word.contents
In this case, I entirely use $.writeln() instead of alert().
or use
milligramme/scriptui_scrollable_alert
I have found this site (http://jongware.mit.edu/idcs6js) immensely useful in understanding ExtendScripts InDesign objects. In particular, your Words object is here, which is an array of Word instances.
In addition, I've used Use Visual Studio Code with the ExtendScript Debugger which helpful for watching variables.
I've seen a lot of different answers to this question and have tried applying their code to my project but none of these solutions seem to work for the data I have.
I need to turn this output into several objects:
[{"creature":{"id":1,"name":"R.I.P.","sprite_location":null,"health_points":0,"attack":0,"defense":0,"action_points":0,"attack_cost":0}},{"creature":{"id":2,"name":"R.I.P.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/rip.gif","health_points":0,"attack":0,"defense":0,"action_points":0,"attack_cost":0}},{"creature":{"id":3,"name":"Bull.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/bull.gif","health_points":50,"attack":8,"defense":20,"action_points":9,"attack_cost":5}},{"creature":{"id":4,"name":"Swallow.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/swallow.gif","health_points":30,"attack":12,"defense":10,"action_points":13,"attack_cost":5}},{"creature":{"id":5,"name":"Kappa.","sprite_location":"http://chunkofwhat.com/games/Parousia/sprites/kappa.gif","health_points":40,"attack":6,"defense":15,"action_points":9,"attack_cost":3}},{"creature":{"id":6,"name":null,"sprite_location":null,"health_points":null,"attack":null,"defense":null,"action_points":null,"attack_cost":null}}]
When I try jQuery.parseJSON(), it just gives me a bunch of [object Object]s but I can't refer to creature[1].id etc.
Again, I know this is a frequently asked question. I really have been through many other examples but they just didn't work out for me.
Thank you.
Each object has one property (creature) with another object as it's value.
result_of_parsing_json[1].creature.id
var creatures = JSON.parse('big_json_string');
for (var i = 0; i < creatures.length; i++) {
var creature = creatures[i].creature; // this is how your object is formatted
console.log(creature.name);
}
/*
R.I.P.
R.I.P.
Bull.
Swallow.
Kappa.
null
*/
Each creature is nested within another object, and since it's an array of objects (that contain the creature), you have to iterate over it with a for loop, to make use of it.
Your parsing of the JSON, then, was most likely correct, but the logic that came afterwards was not (at a total guess).
Your code seems perfectly valid. Try this jsfiddle.
var creatures = $.parseJSON(yourJSONString);
alert(creatures[0].creature.name); // alerts "R.I.P"
Do you need any specific clarifications?
I'm trying to follow this tutorial
http://eloquentjavascript.net/chapter8.html
It mentions a function inPlacePrinter. I cannot find the source code for this in the actual text. I have tried poking around in view source since the interactive version seems to work but cannot locate the raw code. How do I find it?
It is defined in http://eloquentjavascript.net/js/chapter/oo.js.
In oo.js there is this line:
var div = __ENV.parent.DIV();
__ENV is not defined elsewhere, nor in any of the other js files, maybe is part of the MochiKit framework used on page.
You can replace inPlacePrinter() with this 2 functions in order to show the output in browser console:
function show(x){
var show = "";
for (var y = 0; y < arguments.length; y++) {
show += arguments[y];
}
console.log(show);
}
function showTerrarium(terrarium){
this.show(terrarium);
return function() {
console.clear();
this.showTerrarium(terrarium);
}
}
And used them in this way
/*replace:*/ terrarium.onStep = partial(inPlacePrinter(), terrarium);
/*with:*/ terrarium.onStep = partial(showTerrarium(terrarium), terrarium);
Maybe there is a better solution, but this worked.
thats a big tutorial. About half way down the page is this statement:
But all these extra variables can get messy. Another good solution is to use a function similar to partial from chapter 6. Instead of adding arguments to a function, this one adds a this object, using the first argument to the function's apply method:
Is the function inPlacePrinter in the object called partial in chapter 6?
Try using something like Firebug to look at the JavaScript files.
I am trying to count the number of Tabs that are open in my google chrome browswer with javascript. Does anyone know how to do this?
I wrote some javascript that I want repeated 10 times and then stop. Upon the completion of 1 iteration, I open a new window using:
window.open("http://www.test.com");
I want to do this 10 times than stop. Maybe there is a better way than what I am thinking...
It's a good thing that webpages are sandboxed so that other websites can't access them. If they're windows that you've opened using window.open you can save the reference you receive to the window:
var win = window.open(url);
of course you could push this to an array if you're opening a large number of windows.
var wins = [];
//looping stuff here
wins.push(window.open(url[i]);
I think the loop is just fine, but if you want to keep track,
var winList = new Array();
var count = 10;
for(var i=0; i < count; i++){
winList[i] = window.open("http://www.test.com");
}
This way, you can keep references to your windows.
hth
So I couldn't figure out a way doing what I wanted so I took a different approach. I said, lets see if google has the option in chrome to limit the number of tabs opened and I found someone wrote an extension to do exactly that. I don't know how he did but it definitely works.
Controlled multi-tab browsing
Look at the Google Chrome Extensions Developer Guide , in particular the Tabs page and the getAllInWindow function
chrome.tabs.getAllInWindow(integer windowId, function callback)
Where the callback function receives an array of tabs. Meaning you can get its length.
And if the tabs you want to keep track of are possibly in different windows, then you need to look at the Windows page and the getAll function
chrome.windows.getAll(object getInfo, function callback)
Use this to iterate over all windows and call getAllInWindow. And you're all set.
If all you want to do is open ten tabs:
var i;
for (i = 0; i < 10; i++) {
window.open("http://www.test.com");
}
But no, I don't believe you can count the number of open tabs since that could disclose information to websites that you may not want them knowing. (Do you want random websites you visit to know how many tabs you have open?)
On one page of my website the user has the ability to choose and remove up to 2000 items through selecting multiple string representations of them in a dropdown list.
On page load, the objects are loaded onto the page from a previous session into 7 different drop-down lists.
In the window.onload event, the function looping through the items in the drop-downs makes an internal collection of the objects by adding them to a global array - This makes the page ridiculously slow to load, so, I'm fairly certain probably doing it wrong!
How else am I supposed to store these variables?
This is their internal representation:
function Permission(PName, DCID, ID) {
this.PName = PName;
this.DCID = DCID;
this.ID = ID;
}
where: PName is string. DCID is int. ID is int.
EDIT:
Thanks for the quick replies! I appreciate the help, I'm not great with JS! Here is more information:
'selectChangeEvent' is added to the Change and Click event of the Drop down list.
function selectChangeEvent(e) {
//...
addListItem(id);
//...
}
'addListItem(id)' sets up the visual representation of the objects and then calls :
function addListObject(x, idOfCaller) {
var arIDOfCaller = idOfCaller.toString().split('-');
if (arIDOfCaller[0] == "selLocs") {
var loc = new AccessLocation(x, arIDOfCaller[1]);
arrayLocations[GlobalIndexLocations] = loc;
GlobalIndexLocations++;
totalLocations++;
}
else {
var perm = new Permission(x, arIDOfCaller[1], arIDOfCaller[2]);
arrayPermissions[GlobalIndexPermissions] = perm;
GlobalIndexPermissions++;
totalPermissions++;
}
}
Still not enough to go on, but there are some small improvements I can see.
Instead of this pattern:
var loc = new AccessLocation(x, arIDOfCaller[1]);
arrayLocations[GlobalIndexLocations] = loc;
GlobalIndexLocations++;
totalLocations++;
which seems to involve redundant counters and has surplus assignment operations, try:
arrayLocations[arrayLocations.length] = new AccessLocation(x, arIDOfCaller[1]);
and just use arrayLocations.length where you would refer to GlobalIndexLocations or totalLocations (which fromt he code above would seem to always be the same value).
That should gain you a little boost, but this is not your main problem. I suggest you add some debugging Date objects to work out where the bottleneck is.
You may want to consider a design change to support the load. Some sort of paged result set or similar, to cut down on the number of concurrent records being modified.
As much as we desperately want them to be, browsers aren't quite there yet in terms of script execution speed that allow us to do certain types of heavy lifting on the client.
While I haven't tested this idea, I figured I'd throw it out there - might it be faster to return a JSON string from the server side, where your array is fully calculated on that side?
From that point, I'd wager that eval()'ing it (as evil as this may be) might be fast enough to where you could then write the contents onto the page, and your array setup would already be taken care of.
Then again, I suppose the amount of work it'd take the browser to construct the 2k new objects and inject them into the DOM wouldn't necessarily help the speed side of things in the end. At the end of the day, a design change is probably necessary, but sometimes we're stuck with what we've got, eh?