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

How can I make the function parameters in the main program automatically same as in the library?

Guide ,
Jun 20, 2025 Jun 20, 2025
I have multiple parameters in my library function like this:
function creatStyle(paraStn, tabStn, contiParaStn, bodyParaStn, headerParaStn, bodyStn, headerStn, contiStn, footerStn, capLStn, capCStn, capCSDhtn)

I have multiple main programs, and every time I've changed a parameter in the library, I have to go and change each of them, forgetting parts and getting errors that will make me look for half a day.
Is there any way to make:

creatStyle(......) ;

with

function creatStyle(......) ;

always be automation consistent.
I change the parameters of the function in the library, and it is automatically updated in the main program.

TOPICS
Bug , Feature request , How to , Scripting
338
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 ,
Jun 20, 2025 Jun 20, 2025

Hi @dublove consider this:

function createStyle(paraStn, tabStn, contiParaStn, bodyParaStn, headerParaStn, bodyStn, headerStn, contiStn, footerStn, capLStn, capCStn, capCSDhtn) {

    // ... do something with all the params

    return newStyle;
};

function createDefaultStyle() {

    var paraStn = 1;
    var tabStn = 5;
    var contiParaStn = 16;
    var bodyParaStn = 20;
    var headerParaStn = 2;
    var bodyStn = 15;
    var headerStn = 30;
    var contiStn = 123;
    var footerStn = 4356;
    var capLStn = 1233;
    var capCStn = 3;
    var capCSDhtn = 4;

    var newStyle = createStyle(paraStn, tabStn, contiParaStn, bodyParaStn, headerParaStn, bodyStn, headerStn, contiStn, footerStn, capLStn, capCStn, capCSDhtn);

    return newStyle;
};

 

You can make one function that wants every parameter. Then you can make another function that *calls* the first function, and supplies it with preset values for the parameters. So if you want to reuse a particular createStyle, make this second kind of function and to call it (from this script or the top-level script) just use:

var myStyle = createDefaultStyle();

 

Does that help?

- Mark

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
Guide ,
Jun 20, 2025 Jun 20, 2025

Hi m1b.

Thank you very much.

 

It's a little complicated. I'm afraid to move now.
Feels like I'll be cross-referencing a couple of my files now.
I'm intrigued and will definitely try it when I have time.

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
Guide ,
Jun 20, 2025 Jun 20, 2025

@m1b 

I still couldn't hold back my curiosity and tried it.
It suggests that newStyle is undefined, another variable issue across functions?

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 ,
Jun 20, 2025 Jun 20, 2025

I still couldn't hold back my curiosity and tried it. It suggests that newStyle is undefined, another variable issue across functions?

 

I didn't give you complete code, just a structural option. You would have to write the code to "create the style". I just showed you one way to do it without passing any parameters.

- Mark

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
Guide ,
Jun 20, 2025 Jun 20, 2025

Hi m1b.

Thank you very much.
I will try this program of yours again sometime.
There are some urgent matters that need to be taken care of right now.

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
Guide ,
Jun 20, 2025 Jun 20, 2025

in my main program

creatStyle(paraStn, tabStn, contiParaStn, bodyParaStn, headerParaStn, bodyStn, headerStn, contiStn, footerStn, capLStn, capCStn, capCSDhtn);

and in the library publicLib.jsx the

fuction creatStyle(paraStn, tabStn, contiParaStn, bodyParaStn, headerParaStn, bodyStn, headerStn, contiStn, footerStn, capLStn, capCStn, capCSDhtn);

with the same same parameter name.


Can I just pass it in a variable?

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 ,
Jun 20, 2025 Jun 20, 2025
fuction creatStyle(paraStn, tabStn, contiParaStn, bodyParaStn, headerParaStn, bodyStn, headerStn, contiStn, footerStn, capLStn, capCStn, capCSDhtn);

 

Is not a function, (function is misspelled as fuction) , there are no opening and closing braces, and no code to run:

 

Screen Shot 54.pngScreen Shot 56.png

 

 

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
Guide ,
Jun 20, 2025 Jun 20, 2025

Hi rob day.

Thank you

The creatStyle misspelling has been corrected.
I have a complicated situation, and I'm not going to look into this issue for now, or it will get more complicated.

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 ,
Jun 20, 2025 Jun 20, 2025

I have multiple main programs, and every time I've changed a parameter in the library,

 

If you are loading libraries the loaded functions should never change. So a global function might create a named style and check if it already exists. Then you would set the properties after the style is constructed:

 

var ps = makeParaStyle(app.activeDocument, "MyStyle")
//set the style object’s properties after it is constructed
ps.properties = {fillColor:"Black", fillTint:50}


/**
* Makes a new named Paragraph Style—if a style named n exists use that style
* @ param the document to add the style to 
* @ param style style’s name 
* @ return the new or existing paragraph style 
*/

function makeParaStyle(d, n){
    if (d.paragraphStyles.itemByName(n).isValid) {
        return d.paragraphStyles.itemByName(n);
    } else {
        return d.paragraphStyles.add({name:n});
    }
}

 

 

Screen Shot 52.png

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
Guide ,
Jun 20, 2025 Jun 20, 2025

Hi @rob day 

I'm not getting what you're saying.
What I'm asking is that there are too many function parameters, and I only want those parameters to appear once to minimize the problem of omissions due to modifications.

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 ,
Jun 20, 2025 Jun 20, 2025

If your function has 12 parameters, so you have to include all 12 when you call the function.

 

This throws an error because I did not include the 2nd parameter (a string) at run time.

 

Screen Shot 53.png

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
Guide ,
Jun 20, 2025 Jun 20, 2025
LATEST

Hi rob day.
Thanks a lot.
Yes, I realized later that the number of parameters must be the same.
I have some urgent things to take care of right now.
I have used the traditional program. I will try again when I have time.

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