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

Script to automate the function of buttons

Participant ,
May 06, 2024 May 06, 2024

Copy link to clipboard

Copied

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

Views

208

Translate

Translate

Report

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

correct answers 2 Correct answers

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 = 
...

Votes

Translate

Translate
Participant , May 07, 2024 May 07, 2024

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!

Votes

Translate

Translate
Community Expert ,
May 06, 2024 May 06, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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()");

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

How do I know if it spans all pages?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

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