Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Script to automate the function of buttons

Participant ,
May 06, 2024 May 06, 2024

Hello, I have written a script that assigns each button the visibility of 2 levels. The script works fine but when I reopen the PDF the function no longer works.

I have many languages in the pdfs I create and I usually use the set visibility level action in the button properties, but I wanted to automate this. How can I do this?

 

In the attached code I have only inserted 2 layers

var livBase = "Base";
var livIT = "Italiano";
var livEN = "Inglese";

var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");

var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");

function livelloInglese() {
    var ocgs = this.getOCGs();
    for (var i in ocgs) {
        ocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);
    }
}

function livelloItaliano() {
    var ocgs = this.getOCGs();
    for (var i in ocgs) {
        ocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);
    }
}

 

TOPICS
How to , JavaScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Participant ,
May 06, 2024 May 06, 2024

Yes is possible, like this:

 

var cScriptCode = "\nvar livBase = 'Base';\nvar livIT = 'Italiano';\nvar livEN = 'Inglese';\nfunction livelloInglese(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);\n}\n}\nfunction livelloItaliano(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);\n}\n}";
this.addScript("scriptPulsanti", cScriptCode);

var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");

var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");

 

I did various tests and also found suggestions in Adobe's API reference manual.

I created a "Run Javascript" action with this code and it works great

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2024 May 07, 2024
LATEST

Ok thanks, I investigated and actually the levels were set as never visible, with preflight I set an action that makes them visible if activated, now everything works, thanks again!

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2024 May 06, 2024

The setAction command shouldn't be a part of your code. You only need to run it once, from the JS Console.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2024 May 06, 2024

Thanks for reply, what I would like is to have a command that automatically does what I could do with the Set visibility layer action.
That is, for each button I associate the visibility of the layer, each button has its own specific name.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2024 May 06, 2024

Your code does that, but the inclusion of the setAction command is unnecessary and will cause it not to work outside of Acrobat.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2024 May 06, 2024
var livBase = "Base";
var livIT = "Italiano";
var livEN = "Inglese";

var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");

var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");

function livelloInglese() {
    var ocgs = this.getOCGs();
    for (var i in ocgs) {
        ocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);
    }
}

function livelloItaliano() {
    var ocgs = this.getOCGs();
    for (var i in ocgs) {
        ocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);
    }
}
SyntaxError: syntax error
1:Console:Exec
undefined
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2024 May 06, 2024

Are you running it from the Console? You need to select the full code if you do. But that's not how you should use it.

Put this code under a doc-level script:

 

// Doc-level code
var livBase = "Base";
var livIT = "Italiano";
var livEN = "Inglese";

function livelloInglese() {
    var ocgs = this.getOCGs();
    for (var i in ocgs) {
        ocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);
    }
}

function livelloItaliano() {
    var ocgs = this.getOCGs();
    for (var i in ocgs) {
        ocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);
    }
}

 

And then run this code from the JS Console, making sure to select all of it before executing it:

 

var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");

var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2024 May 06, 2024

It works! but I have a question, do I have to do this for every document? or is there a way to have it by default in every pdf?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 06, 2024 May 06, 2024

You have to add this to every document where you want this to work. You can do it as an Action and run it on multiple files, but you can't do it automatically for any file you open, no.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2024 May 06, 2024

Thanks for your help

I added an action that executes this code:

var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");

var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");

is it possible to insert the code that goes into the javascript of the document with an action?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 06, 2024 May 06, 2024

Yes is possible, like this:

 

var cScriptCode = "\nvar livBase = 'Base';\nvar livIT = 'Italiano';\nvar livEN = 'Inglese';\nfunction livelloInglese(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livEN || ocgs[i].name === livBase);\n}\n}\nfunction livelloItaliano(){\nvar ocgs = this.getOCGs();\nfor (var i in ocgs) {\nocgs[i].state = (ocgs[i].name === livIT || ocgs[i].name === livBase);\n}\n}";
this.addScript("scriptPulsanti", cScriptCode);

var pulsanteEN = this.getField("INGLESE");
pulsanteEN.setAction("MouseUp", "livelloInglese()");

var pulsanteIT = this.getField("ITALIANO");
pulsanteIT.setAction("MouseUp", "livelloItaliano()");

 

I did various tests and also found suggestions in Adobe's API reference manual.

I created a "Run Javascript" action with this code and it works great

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2024 May 07, 2024

I have an issue, when scroll the doc after press the button for language selection, the layer disappeared.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2024 May 07, 2024

Are you sure it's set up correctly, spanning all the pages in your file?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2024 May 07, 2024

How do I know if it spans all pages?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2024 May 07, 2024

There's no clear indication. You need to set it up that way when you created it. If you didn't, it will only be on one page.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2024 May 07, 2024
LATEST

Ok thanks, I investigated and actually the levels were set as never visible, with preflight I set an action that makes them visible if activated, now everything works, thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines