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

Structuring a window code wise..

Participant ,
Dec 01, 2016 Dec 01, 2016

Copy link to clipboard

Copied

Hi all,

I just have a quick question about the best way to structure/write a window. Until a while ago, I had been doing something along the lines of:

var w = new Window('dialog', 'whatever')

var mainPanel = w.add('panel')

var nameGroup = mainPanel.add('group')

var nameField = nameGroup.add('statictext', undefined, 'Name:')

Etc.

This obviously got incredibly confusing the more complex my dialogs became, so I've recently started changing it to this:

var w = new Window('dialog', 'whatever')

w.main = w.add('panel')

w.main.name = w.main.add('group')

w.main.name.field = w.main.name.add('statictext', undefined, 'Name:')

This is way easier to follow as I go along, although I'm still wondering if there is an even tidier/better way to do it? With this method, readability, compared to the pervious method, is great and I can quickly figure out what's going on/what I need to edit when scanning the code. The only thing I can see happening is the lines becoming really long the more layers I create. If this method is the preferred way, is this just something that is unavoidable?

Many of you are probably saying 'Duh, this is how you should have done it in the first place', but this was for a script that I'd first made about a year ago when I started learning programming and have continued to upgrade and add stuff to it (lots of people use it where I work) since without changing the way I structured it, so I thought I'd ask as I'm 'converting' it now; Can this be done even better?

TOPICS
Actions and scripting

Views

2.0K

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
Adobe
Community Expert ,
Dec 01, 2016 Dec 01, 2016

Copy link to clipboard

Copied

Frankly, I do it both ways. Normally, I do the second for clarity, but I will often intermix the use of variables just so I don't have to type as much later in the code for specific controls. Below is a sample from one of my scripts. I declared all the dialog's variable at the beginning of the script to make them global.

makeFullJpg = dlg.mainTab.file.add('checkbox',undefined,'Make Full Res Jpg'); makeFullJpg.name = 'makeFullJpg';

    makeFullJpg.value = true

    dlg.mainTab.file.fullJpgGp = dlg.mainTab.file.add('group'); dlg.mainTab.file.fullJpgGp.name = 'fullJpgGP';

        dlg.mainTab.file.fullJpgGp.orientation = 'row';

        dlg.mainTab.file.fullJpgGp.alignChildren ['left','middle'];

        dlg.mainTab.file.fullJpgGp.margins = [15,0,0,0];

        dlg.mainTab.file.fullJpgGp.qualSTxt = dlg.mainTab.file.fullJpgGp.add('statictext',undefined,'Jpg Quality:');                     

        hiJpgQual = dlg.mainTab.file.fullJpgGp.add('edittext',undefined,12); hiJpgQual.name = 'hiJpgQual';

            hiJpgQual.size = [40,20];   

            hiJpgQual.onChange = function(){textToNum(1,12,this,'int')};

        removeR = dlg.mainTab.file.fullJpgGp.add('checkbox',undefined,'Remove "R" at end of file name'); removeR.name = 'removeR';

    dlg.mainTab.file.resampleGp = dlg.mainTab.file.add('group'); dlg.mainTab.file.resampleGp.name = 'resampleGp';

        dlg.mainTab.file.resampleGp.orientation = 'row';

        dlg.mainTab.file.resampleGp.alignChildren ['left','top'];

        dlg.mainTab.file.resampleGp.margins = [15,0,0,0];

        resample = dlg.mainTab.file.resampleGp.add('radiobutton',undefined,'Resample image to 11" max dimention at 300 dpi'); resample.name = 'resample';

        noResample = dlg.mainTab.file.resampleGp.add('radiobutton',undefined,'Do not resample: add borders at existing resolution'); noResample.name = 'noResample';

        resampleSmall = dlg.mainTab.file.resampleGp.add('checkbox',undefined,'Only resample cropped images (less than 11" at 300 dpi)'); resampleSmall.name = 'resampleSmall';

        resample.value = true;

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 ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

That makes a lot of sense. Thanks Chuck!

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 ,
Dec 02, 2016 Dec 02, 2016

Copy link to clipboard

Copied

One other advantage of using a straight variable name for your controls: in my above code, you can notice that I gave each control a name that is the same as the variable name. That allows me to do a recursive function to grab all the control values and put them into an XML file to use as presets. By doing it this way, I can add any control, and the recursive function will get the value, and I don't have to change anything code wise - very handy when you have a large UI that needs some updating. Here's just one tab of a UI for a script that I used to use for work. I could record all the values and then save them as a preset

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 ,
Dec 05, 2016 Dec 05, 2016

Copy link to clipboard

Copied

Ah, clever. So do you just iterate through all the children in that tab and then save the values for any controls/variables that have that name property?

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 ,
Dec 05, 2016 Dec 05, 2016

Copy link to clipboard

Copied

Yes, by using a recursive function, you can get the values of all the children controls is all the groups, panels, and tabs. You can see an example of this the the file: _Deco Menu.jsx, which is in the Deco folder in the the Presets folder of PS. Do a search for Uebele, and it should start around the second mention of my 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
Participant ,
Dec 06, 2016 Dec 06, 2016

Copy link to clipboard

Copied

Thanks, I will check it out!

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 ,
Feb 01, 2017 Feb 01, 2017

Copy link to clipboard

Copied

Hey Chuck,

Sorry for bringing this old topic up again, but I'm currently in the middle of switching over one of my largest scripts to use this iterative method of saving control settings (such a great way of doing it, I love it). I decided to stick to keeping my controls as object properties (i.e. tabs.search.top.left.listsGroup.fileList) rather than giving them global names. I then just gave each of the controls the .name property and then passed their parent window as a parameter into my iterative function, which works great.

That got me thinking, out of curiosity, as to why you chose to make your controls global?

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 ,
Feb 01, 2017 Feb 01, 2017

Copy link to clipboard

Copied

I don't rename the values from the UI to use in my script. Often I will put the whole UI in a function, and I need those values available for use in other functions, so they need to be global. Does using object name work okay with recording the XML? I haven't played with that much, but I think I ran into issues as the XML file uses the periods as hierarchy levels, so a straight simple name worked best for me.

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 ,
Feb 05, 2017 Feb 05, 2017

Copy link to clipboard

Copied

LATEST

Apologies for the late reply. I was using it for storing some settings in a json file. It was for a dialog that remembers its control values on close and redisplays them on show. I wanted to switch to using an iterative function to make it all a little more manageable, but unfortunately because the settings have to be saved on things like file list selection changes, there was a noticeable impact on performance as I started testing it on a few, so I didn't go ahead with it.

'I don't rename the values from the UI to use in my script'

I'm not sure what you mean by this?

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