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

Script Work Only Outside the Function!

Enthusiast ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

Hi Experts, 

I Wrote this Function to Refresh the Arrays of the Styles, but it work only outside a function!, i dont know why? and i added a button to execute this fucntion (refresh and show styles arrays with groups) in the dropdown lists, any solution? thanks in advance.

 

//Refresh Styles Button - All CS-PS-OS Must Refreshed
Button3.onClick = function() {RefreshStyles();};
function RefreshStyles() {
//---------------------------------------------------------
//Adding Character Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allCharacterStyles;
var myCharsStyles = [];
for (i = 0; i < myStyles.length; i ++){
myCharsStyles[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "CharacterStyleGroup") 
myCharsStyles[i]+="["+myStyles[i].parent.name+"]";
}
//---------------------------------------------------------
//Adding Paragraph Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allParagraphStyles;
var myParStylesList = [];
for (i = 0; i < myStyles.length; i ++){
myParStylesList[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "ParagraphStyleGroup") 
myParStylesList[i]+="["+myStyles[i].parent.name+"]";
}
//---------------------------------------------------------
//Adding Obejct Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allObjectStyles;
var myObjStylesList = [];
for (i = 0; i < myStyles.length; i ++){
myObjStylesList[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "ObjectStyleGroup") 
myObjStylesList[i]+="["+myStyles[i].parent.name+"]";
}
}

 

 

Best
Mohammad Hasanin
TOPICS
Scripting

Views

317

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 1 Correct answer

Community Expert , Jul 08, 2021 Jul 08, 2021

Lots of issues in your implementation

  • You passed in a variable csNewItems to the method, why is it needed? You are not using its value but assigning its value only to pass that value to a method. Technically it's not wrong but it is unnecessary code which doesn't seem to serve any purpose
  • You add a new dropdownlist to your panel, add new elements to it. But what you had to do is add elements to the already existing dropdown list.
  • You add elements to the new dropdownlist but remove elements of t
...

Votes

Translate

Translate
Community Expert ,
Jul 07, 2021 Jul 07, 2021

Copy link to clipboard

Copied

Hi @M.Hasanin,

Post the whole code, it seems you are adding the results of these arrays to a UI element. Share that part of the code as well. Also please explain what does not work exactly, is it that you add a new style and that style is not added to your collection or you get some errors?

-Manan

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
Enthusiast ,
Jul 08, 2021 Jul 08, 2021

Copy link to clipboard

Copied

Hi @Manan Joshi ,

Thanks for asking, here it is the UI Script, and thanks in advance :

//Check-First
Main();

function Main(){
	// Check to see whether any InDesign documents are open.
	// If no documents are open, display an error message.
	if (app.documents.length > 0) {
		var myDoc = app.activeDocument;
	}
	else {
		// No documents are open, so display an error message.
		alert("No InDesign documents are open. Please open a document and try again.");
	}
}

//Preparing All Styles with Groups
//---------------------------------------------------------
//Adding Character Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allCharacterStyles;
var myCharsStyles = [];
for (i = 0; i < myStyles.length; i ++){
myCharsStyles[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "CharacterStyleGroup") 
myCharsStyles[i]+=" ["+myStyles[i].parent.name+"]";
}
//---------------------------------------------------------
//Adding Paragraph Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allParagraphStyles;
var myParStylesList = [];
for (i = 0; i < myStyles.length; i ++){
myParStylesList[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "ParagraphStyleGroup") 
myParStylesList[i]+=" ["+myStyles[i].parent.name+"]";
}
//---------------------------------------------------------
//Adding Obejct Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allObjectStyles;
var myObjStylesList = [];
for (i = 0; i < myStyles.length; i ++){
myObjStylesList[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "ObjectStyleGroup") 
myObjStylesList[i]+=" ["+myStyles[i].parent.name+"]";
}

//---------------------------------------------------------
//Making Palettes Windows
//---------------------------------------------------------
#targetengine "session1";
var w = new Window ("palette");
var gqmanager = new Window ("palette");
//gqmanager is the (GREP Query Manager) outside the main Function

w.text = "Assigned GREP Results to Styles - Version 2.15";
w.preferredSize.width = 800;
w.alignChildren = ["center","center"];  //"left";
w.orientation =  "column"; //"row"; 
w.spacing = 10; 
w.margins = 16; 

// Effective Progress bar (added but not shown in Object Style)
var myDoc = app.activeDocument;
w.prg = w.add('progressBar');
w.prg.maxvalue = myDoc.pages.length;
w.prg.value = 0;
w.prg.size = [870, 5];
w.prg.visible = false;
    
//Document Object Styles Dropdown List--Source
var myInputGroup1 = w.add ("group");

//Parent - Input Panel Prepare
var InputPanel = w.add("panel", undefined, undefined, {name: "panel1"});
InputPanel.text = " GREP Code and Target Style : ";
InputPanel.preferredSize.width = 500;
InputPanel.orientation = "row";
InputPanel.alignChildren = ["center","center"];
InputPanel.spacing = 10;
InputPanel.margins = 16;

//Children -  input Panel Inside Prepare
var myInputPanelInside = InputPanel.add("group", undefined, {name: "myInput"});
myInputPanelInside.add ("statictext", undefined, "Use GREP:");
myInputPanelInside.alignment = "left";
var myGREPString = myInputPanelInside.add ("edittext", undefined, "\\(.+?\\)");
myGREPString.characters = 20;
myGREPString.enabled = true;
myGREPString.preferredSize.width = 350;
myInputPanelInside.add ("statictext", undefined, "Select Target Style:");
//Adding Radio Buttons 
var radio1 = myInputPanelInside.add ("radiobutton", undefined, "Character Styles");
var radio2 = myInputPanelInside.add ("radiobutton", undefined, "Paragraph Styles");
var radio3 = myInputPanelInside.add ("radiobutton", undefined, "Object Style");
//Previous Default Condition -- All Above Styles
radio1.value = true;

//Parent - DropDown List Prepare
var DropDownPanel = w.add("panel", undefined, undefined, {name: "panel1"});
DropDownPanel.text = " Select Desired Style: ";
DropDownPanel.preferredSize.width = 800;
DropDownPanel.orientation = "row";
DropDownPanel.alignChildren = ["center","center"];
DropDownPanel.spacing = 10;
DropDownPanel.margins = 16;

//Children -  DropDown Inside Prepare
var myDropDownInside = DropDownPanel.add("group", undefined, {name: "myInput"});
//myDropDownInside.add ("statictext", undefined, "Styles:");
myDropDownInside.alignment = "center";

//Adding the Character Styles Found
myDropDownInside.add ("statictext", undefined, "Character styles :");
myDropDownInside.alignment = "center";
var myDropdown1 = myDropDownInside.add ("dropdownlist", undefined, myCharsStyles);
myDropdown1.preferredSize.width = 182;
myDropdown1.selection = 0 ;
//DropDown Visibiliy
myDropdown1.visible = true;

//Adding the Paragraph Styles Found
myDropDownInside.add ("statictext", undefined, "Paragraph styles :");
myDropDownInside.alignment = "center";
var myDropdown2 = myDropDownInside.add ("dropdownlist", undefined, myParStylesList);
myDropdown2.preferredSize.width = 182;
myDropdown2.selection = 0 ;
//DropDown Visibiliy
myDropdown2.visible = false;

//Adding the Object Styles Found
myDropDownInside.add ("statictext", undefined, "Object styles :");
myDropDownInside.alignment = "center";
var myDropdown3 = myDropDownInside.add ("dropdownlist", undefined, myObjStylesList);
myDropdown3.preferredSize.width = 182;
myDropdown3.selection = 0 ;
//DropDown Visibiliy
myDropdown3.visible = false;

//OK and Cancel
//Adding OK Button
var myButtonGroup = w.add ("group");
myButtonGroup.alignment = "center";
var Button1 = myButtonGroup.add ("button", undefined, "Find GREP and Apply Style");
//Add Tooltip for Button 1
Button1.helpTip = "Find Writtened GREP"
var Button2 = myButtonGroup.add ("button", undefined, "GREP Query Manager");
//Add Tooltip for Button 2
Button2.helpTip = "form Manage GREP Queries and Save or Load"
var Button3 = myButtonGroup.add ("button", undefined, "Refresh Styles");
//Add Tooltip for Button 3
Button3.helpTip = "Refresh all Character, Paragraphs, and Object Styles Lists"
var Button4 = myButtonGroup.add ("button", undefined, "About");
//Add Tooltip for Button 4
Button4.helpTip = "About the Script"
var Button5 = myButtonGroup.add ("button", undefined, "Exit");
//Add Tooltip for Button 5
Button5.helpTip = "Exit Script"

//Radio Button Listener -  to Show DropDowns Lists
//---------------------------------------------------------------------
radio1.onClick = function (){
   myDropdown1.visible = true;
   myDropdown2.visible = false;
   myDropdown3.visible = false;
}
radio2.onClick = function (){
   myDropdown1.visible = false;
   myDropdown2.visible = true;
   myDropdown3.visible = false;
}

radio3.onClick = function (){
   myDropdown1.visible = false;
   myDropdown2.visible = false;
   myDropdown3.visible = true;
}
//---------------------------------------------------------------------

//After Drawing Interface
//Showing the Dialog - We Will Show as Varaible So We Can Correctly Use Cancel Button (w.Show() are One time in Code)
var a = w.show();

//What Happened if User Hit About Button - All the OnClick Callers Must Came Before Showing Dialogs
Button1.onClick = function() {FindGrp();};
function FindGrp() {
doRadioButtonOpt();
}

//GREP Query Manager Button - All the OnClick Callers Must Came Before Showing Dialogs
Button2.onClick = function() {Options();};
function Options() {
CallTheQueryGManager();
}

//Refresh Styles Button - All CS-PS-OS Must Refreshed
Button3.onClick = function() {RefreshStyles();};
function RefreshStyles() {
//---------------------------------------------------------
//Adding Character Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allCharacterStyles;
var myCharsStyles = [];
for (i = 0; i < myStyles.length; i ++){
myCharsStyles[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "CharacterStyleGroup") 
myCharsStyles[i]+="["+myStyles[i].parent.name+"]";
}
//---------------------------------------------------------
//Adding Paragraph Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allParagraphStyles;
var myParStylesList = [];
for (i = 0; i < myStyles.length; i ++){
myParStylesList[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "ParagraphStyleGroup") 
myParStylesList[i]+="["+myStyles[i].parent.name+"]";
}
//---------------------------------------------------------
//Adding Obejct Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allObjectStyles;
var myObjStylesList = [];
for (i = 0; i < myStyles.length; i ++){
myObjStylesList[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "ObjectStyleGroup") 
myObjStylesList[i]+="["+myStyles[i].parent.name+"]";
}
}

 

Best
Mohammad Hasanin

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 ,
Jul 08, 2021 Jul 08, 2021

Copy link to clipboard

Copied

The problem that I see in your code is as follows

  • In your RefreshStyles method, you repopulate the variables myCharsStyles, myParStylesList, and myObjStylesList but you never add the elements of these arrays to the dropdown list this is the reason the UI dropdown is never updated. Calling the add method of each dropdown list should resolve the problem

-Manan

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
Enthusiast ,
Jul 08, 2021 Jul 08, 2021

Copy link to clipboard

Copied

Hi @Manan Joshi 

I tried the Following Update (focus only in character style to test) :

function RefreshStyles(csNewItems) {
//---------------------------------------------------------
//Adding Character Styles with Sub Groups
//---------------------------------------------------------
var mydoc = app.activeDocument;
myStyles = mydoc.allCharacterStyles;
var myCharsStyles = [];
for (i = 0; i < myStyles.length; i ++){
myCharsStyles[i] = myStyles[i].name;
if (myStyles[i].parent.constructor.name == "CharacterStyleGroup") 
myCharsStyles[i]+="["+myStyles[i].parent.name+"]";
}

//Now Refres the CS DropDown Lists!
    csNewItems = myCharsStyles;
    var myDropdown1new = myDropDownInside.add ("dropdownlist", undefined, csNewItems);
    myDropdown1.removeAll();
    myDropdown1new = myDropdown1;
}

But Unlucky not working! also no erros!, what the mistake i done!

Best
Mohammad Hasanin

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 ,
Jul 08, 2021 Jul 08, 2021

Copy link to clipboard

Copied

Lots of issues in your implementation

  • You passed in a variable csNewItems to the method, why is it needed? You are not using its value but assigning its value only to pass that value to a method. Technically it's not wrong but it is unnecessary code which doesn't seem to serve any purpose
  • You add a new dropdownlist to your panel, add new elements to it. But what you had to do is add elements to the already existing dropdown list.
  • You add elements to the new dropdownlist but remove elements of the old one. The effect in my guess would be an empty dropdown(old one) and a new dropdown with the new elements. Mind that you will keep on adding a new dropdown every time you press the refresh button. So if you press the button five times you will have 6 dropdowns five new and one original
  • The newly added dropdowns may be added at a location where it's possible it might not be visible. Hence you would not know how to interact with it.

I have made a working sample. I have refactored the code a bit as well, to merge the initial setup of the dropdowns and the refresh into a single code snippet. Study it based on the points I discussed and assess why this works and your does not.

P.S.:- Looking at the questions you have posted so far, I would suggest the following

  • Make extra efforts to learn debugging skills
  • Spend time to read and understand API documentation. On multiple occasions, I have noticed that you have used methods thinking they will do something when they do something totally different.
  • Most importantly spend some time learning basic coding constructs. The best way would be to read sample codes and analyse why it has been structured in a particular way.
//Check-First
Main();

function Main() {
    // Check to see whether any InDesign documents are open.
    // If no documents are open, display an error message.
    if (app.documents.length > 0) {
        var myDoc = app.activeDocument;
    }
    else {
        // No documents are open, so display an error message.
        alert("No InDesign documents are open. Please open a document and try again.");
    }
}

function populateDropDownList(ddl, dataList)
{
    ddl.removeAll()
    for (i = 0; i < dataList.length; i++) {
        tmp = dataList[i].name;
        if (dataList[i].parent.constructor.name == "CharacterStyleGroup" || dataList[i].parent.constructor.name == "ParagraphStyleGroup" || dataList[i].parent.constructor.name == "ObjectStyleGroup")
            tmp += "[" + dataList[i].parent.name + "]";

            ddl.add("item", tmp)
    }
    ddl.selection = 0;
}

function RefreshStyles() {
    var mydoc = app.activeDocument;
    populateDropDownList(myDropdown1, mydoc.allCharacterStyles);
    populateDropDownList(myDropdown2, mydoc.allParagraphStyles);
    populateDropDownList(myDropdown3, mydoc.allObjectStyles)
}

//---------------------------------------------------------
//Making Palettes Windows
//---------------------------------------------------------
#targetengine "session1";
var w = new Window("palette");
var gqmanager = new Window("palette");
//gqmanager is the (GREP Query Manager) outside the main Function

w.text = "Assigned GREP Results to Styles - Version 2.15";
w.preferredSize.width = 800;
w.alignChildren = ["center", "center"];  //"left";
w.orientation = "column"; //"row"; 
w.spacing = 10;
w.margins = 16;

// Effective Progress bar (added but not shown in Object Style)
var myDoc = app.activeDocument;
w.prg = w.add('progressBar');
w.prg.maxvalue = myDoc.pages.length;
w.prg.value = 0;
w.prg.size = [870, 5];
w.prg.visible = false;

//Document Object Styles Dropdown List--Source
var myInputGroup1 = w.add("group");

//Parent - Input Panel Prepare
var InputPanel = w.add("panel", undefined, undefined, { name: "panel1" });
InputPanel.text = " GREP Code and Target Style : ";
InputPanel.preferredSize.width = 500;
InputPanel.orientation = "row";
InputPanel.alignChildren = ["center", "center"];
InputPanel.spacing = 10;
InputPanel.margins = 16;

//Children -  input Panel Inside Prepare
var myInputPanelInside = InputPanel.add("group", undefined, { name: "myInput" });
myInputPanelInside.add("statictext", undefined, "Use GREP:");
myInputPanelInside.alignment = "left";
var myGREPString = myInputPanelInside.add("edittext", undefined, "\\(.+?\\)");
myGREPString.characters = 20;
myGREPString.enabled = true;
myGREPString.preferredSize.width = 350;
myInputPanelInside.add("statictext", undefined, "Select Target Style:");
//Adding Radio Buttons 
var radio1 = myInputPanelInside.add("radiobutton", undefined, "Character Styles");
var radio2 = myInputPanelInside.add("radiobutton", undefined, "Paragraph Styles");
var radio3 = myInputPanelInside.add("radiobutton", undefined, "Object Style");
//Previous Default Condition -- All Above Styles
radio1.value = true;

//Parent - DropDown List Prepare
var DropDownPanel = w.add("panel", undefined, undefined, { name: "panel1" });
DropDownPanel.text = " Select Desired Style: ";
DropDownPanel.preferredSize.width = 800;
DropDownPanel.orientation = "row";
DropDownPanel.alignChildren = ["center", "center"];
DropDownPanel.spacing = 10;
DropDownPanel.margins = 16;

//Children -  DropDown Inside Prepare
var myDropDownInside = DropDownPanel.add("group", undefined, { name: "myInput" });
//myDropDownInside.add ("statictext", undefined, "Styles:");
myDropDownInside.alignment = "center";

//Adding the Character Styles Found
myDropDownInside.add("statictext", undefined, "Character styles :");
myDropDownInside.alignment = "center";
var myDropdown1 = myDropDownInside.add("dropdownlist", undefined, "");
myDropdown1.preferredSize.width = 182;
//DropDown Visibiliy
myDropdown1.visible = true;

//Adding the Paragraph Styles Found
myDropDownInside.add("statictext", undefined, "Paragraph styles :");
myDropDownInside.alignment = "center";
var myDropdown2 = myDropDownInside.add("dropdownlist", undefined, "");
myDropdown2.preferredSize.width = 182;
//DropDown Visibiliy
myDropdown2.visible = false;

//Adding the Object Styles Found
myDropDownInside.add("statictext", undefined, "Object styles :");
myDropDownInside.alignment = "center";
var myDropdown3 = myDropDownInside.add("dropdownlist", undefined, "");
myDropdown3.preferredSize.width = 182;
//DropDown Visibiliy
myDropdown3.visible = false;

//OK and Cancel
//Adding OK Button
var myButtonGroup = w.add("group");
myButtonGroup.alignment = "center";
var Button1 = myButtonGroup.add("button", undefined, "Find GREP and Apply Style");
//Add Tooltip for Button 1
Button1.helpTip = "Find Writtened GREP"
var Button2 = myButtonGroup.add("button", undefined, "GREP Query Manager");
//Add Tooltip for Button 2
Button2.helpTip = "form Manage GREP Queries and Save or Load"
var Button3 = myButtonGroup.add("button", undefined, "Refresh Styles");
//Add Tooltip for Button 3
Button3.helpTip = "Refresh all Character, Paragraphs, and Object Styles Lists"
var Button4 = myButtonGroup.add("button", undefined, "About");
//Add Tooltip for Button 4
Button4.helpTip = "About the Script"
var Button5 = myButtonGroup.add("button", undefined, "Exit");
//Add Tooltip for Button 5
Button5.helpTip = "Exit Script"

//Radio Button Listener -  to Show DropDowns Lists
//---------------------------------------------------------------------
radio1.onClick = function () {
    myDropdown1.visible = true;
    myDropdown2.visible = false;
    myDropdown3.visible = false;
}
radio2.onClick = function () {
    myDropdown1.visible = false;
    myDropdown2.visible = true;
    myDropdown3.visible = false;
}

radio3.onClick = function () {
    myDropdown1.visible = false;
    myDropdown2.visible = false;
    myDropdown3.visible = true;
}
RefreshStyles()
//---------------------------------------------------------------------

//After Drawing Interface
//Showing the Dialog - We Will Show as Varaible So We Can Correctly Use Cancel Button (w.Show() are One time in Code)
var a = w.show();

//What Happened if User Hit About Button - All the OnClick Callers Must Came Before Showing Dialogs
Button1.onClick = function () { FindGrp(); };
function FindGrp() {
    doRadioButtonOpt();
}

//GREP Query Manager Button - All the OnClick Callers Must Came Before Showing Dialogs
Button2.onClick = function () { Options(); };
function Options() {
    CallTheQueryGManager();
}

//Refresh Styles Button - All CS-PS-OS Must Refreshed
Button3.onClick = function () { RefreshStyles(); };

-Manan

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
Enthusiast ,
Jul 09, 2021 Jul 09, 2021

Copy link to clipboard

Copied

LATEST

Thank you a lot @Manan Joshi 

I promise i will study the API deeply and Develop my coding Skills, i just was exausted this time!.

Best
Mohammad Hasanin

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
New Here ,
Jul 08, 2021 Jul 08, 2021

Copy link to clipboard

Copied

xem kêt qua

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
Enthusiast ,
Jul 08, 2021 Jul 08, 2021

Copy link to clipboard

Copied

và nó như thế nào?

Best
Mohammad Hasanin

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