appliedLanguage question

Participant ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

I'm trying to set an applied language to a paragraph style.

When I use this app.activeDocument.allParagraphStyles[20].appliedLanguage = app.languagesWithVendors.itemByID(81); on it's own the code works fine. When I put it into a function, as shown below, it fails.

Please can someone help and point me in the right direction.

Thank you.

function processThis() {
    for (ii=1; ii<paraStylesToProcess.length; ii++) {
        //alert(paraStylesToProcess[ii])
        listIndex = paraStylesToProcess[ii];
        app.activeDocument.allParagraphStyles[listIndex].appliedLanguage = app.languagesWithVendors.itemByID(81);
    }
w.close();
}
TOPICS
Scripting

Views

200

Likes

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 ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

Hi @deckarduk , my suggestion to start a new thread probably confused things. Here’s the code I posted in the other thread:

 

var paraStylesToProcess = app.activeDocument.allParagraphStyles
processThis()

function processThis() {
    for (ii=1; ii<paraStylesToProcess.length; ii++) {
        //alert(paraStylesToProcess[ii])
        //listIndex = paraStylesToProcess[ii];
        //app.activeDocument.allParagraphStyles[listIndex].appliedLanguage = app.languagesWithVendors.itemByID(81);
        paraStylesToProcess[ii].appliedLanguage = app.languagesWithVendors.itemByID(81)
    }
    //w.close();
}

 

https://community.adobe.com/t5/indesign-discussions/appliedlanguage-gt-list-of-available-languages/t...

Likes

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 ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

Hi Rob,

Thanks again for the help and for the code which works for me.

I can see that runs through all the paragraph styles and changes them.

What I'd done elsewhere in my code was create a list of paragraph styles that aren't set to my required language.

I was then trying to use that list of numbers to process the only those paragraph styles.

When I loop through the list of numbers, using an alert dialog, they're all correct.

 

What's odd is that this line of code works on it's own app.activeDocument.allParagraphStyles[20].appliedLanguage = app.languagesWithVendors.itemByID(81); yet when I put it into a function it fails?

 

Thanks again for your help.

 

Likes

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 ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

Without seeing all of your code—it could be allParagraphStyles, which is an array of all the document styles including paragraphStyleGroups, vs. paragraphStyles which is a collection of styles not including paragraphStyleGroups. Collections and Arrays have different methods.

 

 

 

var as = app.activeDocument.allParagraphStyles;
var ps = app.activeDocument.paragraphStyles;

//these might be different
$.writeln(as.length)
$.writeln(ps.count())

 

 

 

 

Screen Shot 43.png

 

Screen Shot 42.png

 

 

 

 

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi Rob,

Thanks for the help and info.

 

Yeah, I'd seen that difference when creating a similar script however I'd not seen the different methods. It could be that difference between length and count.

I'll have to dig deeper to see if I can sort the niggle.

 

Thanks once again for the help Rob 🙂

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi everyone,

Still trying to sort this, please can someone help.

If I exexute the code below this line is ok and does what it should - myParaStyles[20].appliedLanguage = thisLang;

However if the function is defined either inside or outside of a custom dialog and called via onClick, from a button, the line above fails?

Please can someone point me in the right direction as to why.

Thanks in advance.

 

Here's the code:

var myDoc = app.activeDocument;
var myParaStyles = myDoc.allParagraphStyles;
var thisLang = app.languagesWithVendors.itemByID(81);

processThis()

function processThis() {
    alert(thisLang.name)
    alert(myParaStyles[20].appliedLanguage.name);
    myParaStyles[20].appliedLanguage = thisLang;
    }

 

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Can you post the dialog code?

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi Rob,

Thanks for the reply.

I'd have to create a trimmed down version of the dialog and I'm not sure if that would be any good.

 

Am I correct with the code below when it comes to adding the function to the onClick event?

I've tried adding alerts within the function and when it's triggered they work fine.

It's just that 1 line above that errors even when I've programmed in the relevant paragraph style number which I know is correct.

 

Here's that code: (I've used the xxx to try eliminate errors)

var myButtonGroup = w.add ("group");
myButtonGroup.alignment = "right";
myButtonGroup.add ("button", undefined, "Cancel");
xxxx = myButtonGroup.add ("button", undefined, "Process");
xxxx.onClick = processThis;

w.show ();

 

Thanks Rob.

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

I wanted you to post all of the code to see where you are setting the variables used in the function. I think you need to set the variables in an onClick function, and then run  processThis() after the dialog is gone. Something like this:

 

 

 

 

//setup variables to capture here
var myDoc, myParaStyles, thisLang;

var dialog = new Window("dialog"); 
dialog.text = "Process"; 
var group1 = dialog.add("group", undefined, {name: "group1"}); 
var ok = group1.add("button", undefined, undefined, {name: "ok"}); 
ok.text = "Process"; 
var cancel = group1.add("button", undefined, undefined, {name: "cancel"}); 
cancel.text = "Cancel"; 
ok.onClick = function(){
    //set the variables on the ok button click
    myDoc = app.activeDocument;
    myParaStyles = myDoc.allParagraphStyles;
    thisLang = app.languagesWithVendors.itemByID(81);
    dialog.close(); 
}
dialog.show();

//after the dialog is closed run the main function where the captured variables can be used
processThis();

function processThis(){
    myParaStyles[20].appliedLanguage = thisLang;
    alert(thisLang.name + "\r" + myParaStyles[20].appliedLanguage.name)
}

 

 

 

 

 

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi Rob,

Thank you very much for your time on this, the code works a treat.

It was strange that the first time I executed the code it failed on this line - myParaStyles[20].appliedLanguage = thisLang; 

However, when executing again it worked ok.

 

I'd not posted all of my code because it needed a tidy up. Essentially it created a custom dialog with a listbox that lists all of the paragraph styles in the document and flags any that don't have the language set as required. That side of things worked fine, it was when it came to processing the flagged styles the error occured.

The method you have used seems to have solved the issue. My code tried to process the flagged styles whilst the custom dialog was still open not after it had closed.

 

Thank you once again Rob, nice work!   🙂

 

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

Hi Rob,

I've implemented your method into my script and it works great when processing the styles.

Thanks.

 

There is just one slight niggle in that if the Cancel button is pressed the processing still occurs.

I have tried adding an onClick event to the Cancel button to stop the code executing any further, but as yet haven't managed anything that works.

I tried moving the processThis() call inside the ok.onClick event but then the code errors.

Do you have any ideas as to how to allow the user to Cancel out of the process.

 

Thanks again for the great work 🙂

 

Likes

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 ,
Apr 29, 2022 Apr 29, 2022

Copy link to clipboard

Copied

I almost always use the InDesign dialog class instead of the JS Window class, so someone else might be able to help. Try this–you need to wrap the dialog creation in a function in order to use return and exit the script:

 

//setup variables to capture here
var myDoc, myParaStyles, thisLang;

showDialog()
function showDialog(){
    var dialog = new Window("dialog"); 
    dialog.text = "Process"; 
    var group1 = dialog.add("group", undefined, {name: "group1"}); 
    var ok = group1.add("button", undefined, undefined, {name: "ok"}); 
    ok.text = "Process"; 
    var cancel = group1.add("button", undefined, undefined, {name: "cancel"}); 
    cancel.text = "Cancel"; 
    ok.onClick = function(){
    //set the variables on the ok button click
        myDoc = app.activeDocument;
        myParaStyles = myDoc.allParagraphStyles;
        thisLang = app.languagesWithVendors.itemByID(81);
        dialog.close(); 
    }
    var res = dialog.show() 
    if (res == 2){
        alert("Clicked Cancel");
        return;
    }
    //after the dialog is closed run the main function where the onClick variables can be used
    processThis()
}


function processThis(){
    myParaStyles[2].appliedLanguage = thisLang;
    alert(thisLang.name + "\r" + myParaStyles[2].appliedLanguage.name)
}

 

Likes

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 03, 2022 May 03, 2022

Copy link to clipboard

Copied

LATEST

Hi Rob,

Fantastic!

I've implemented your solution and it works great.

Many thanks for your time on this Rob  🙂

Cheers!

Likes

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