Skip to main content
Inspiring
March 5, 2026
Question

How can I write the output to the JavaScript console and the clipboard at the same time?

  • March 5, 2026
  • 1 reply
  • 36 views

With my JavaScript, I invert the page order and simultaneously write the new page order to the console.
To avoid the hassle of manually copying the sequence of numbers from the console, the new page order should also be immediately added to the clipboard so that it can be inserted into another program using STR+V.
To do this, I create a temporary text field, insert the sequence of numbers, and copy it, hoping to then have it in the clipboard.
Unfortunately, this only works sporadically and is more unreliable than reliable.
I would be grateful for any tips on how I can improve this!

var order = [];
for (var i = this.numPages - 1; i >= 0; i--) {
order.push(i + 1);
}
var orderStr = order.join(" ");
console.println(orderStr);
var f = this.addField("tempCopyField", "text", 0, [0, 0, 300, 20]);
f.value = orderStr;
f.setFocus();
app.execMenuItem("SelectAll");
app.execMenuItem("Copy");
this.removeField("tempCopyField");

Regards

yosimo

    1 reply

    ls_rbls
    Community Expert
    Community Expert
    March 12, 2026

    Hi ​@yosimo ,

     

    Are you planning to execute this script from a button object? Maybe a dropdown menu with a validating script instead? How many pages are in this document and does this matter?

     

    I don’t believe this method is reliable unless you use page templates to either hide them or make them visible based on a condition that triggers the script (though I could be mistaken).  Could you please elaborate more what the specific part of your script is partially working ?

     

    Moreover, if I’m correct, the old-fashioned way of copying data to the clipboard might be completely deprecated. You could face problems if it’s not run with elevated privileged context (even when using the app.execMenuItem() method). For additional insights on this topic, See this discussion:

     

    Have you considered employing a document-level global variable to hold the desired output of f.value = orderStr to be used by the other program ? 

     

    See here: