Skip to main content
Inspiring
October 8, 2013
Answered

Linking a JS object to ScriptUI listbox item

  • October 8, 2013
  • 3 replies
  • 7919 views

I am writing a script that that takes elements from a document and for each element makes a custom object with its own properties and methods. This list of elements is then spat out into a ScriptsUI listbox.

How do I link each listbox item to its associated obejct? i.e. So if I double click on a listbox item it will run a particular method within that object.

P.S. I dont want to have to store array ids in a column or something hacky like that.

This topic has been closed for replies.
Correct answer McShaman

Got it!

From what I can tell the issue with extending native Adobe objects is not a problem with ScriptUI objects.

So the following works:

ListItem.prototype.targetObject = undefined;

var myDialog = new Window( 'dialog' );

var list = myDialog.add( 'listbox' );

var paraStyles = app.activeDocument.allParagraphStyles;

var listItems = [];

for( var i = 0; i < paraStyles.length; i++ ) {

     var listItem = list.add( 'item', paraStyles.name );

     listItem.targetObject = paraStyles;

     listItems.push( listItem );

}

myDialog.show();

3 replies

McShamanAuthor
Inspiring
October 15, 2013

Can you put id of an element in a column then hide the column from the user?

TᴀW
Brainiac
October 15, 2013

Thinking about it, theoretically you could add a custom property to each

listItem:

w = new Window("dialog");

l = w.add("listbox");

with (l){

i = add("item", "Dog");

i.key = "key1";

j = add("item", "Cat");

j.key = "key2";

k = add("item", "Cow");

k.key = "key3";

}

l.onDoubleClick = function(){alert(l.selection.key)}

w.show();

Double click on an item to show it's key.

So now, it's really simple: just store your homemade objects in an array

based on a list of string keys (or, as I mentioned, the objects' ID

converted to a string). Create your listbox, and for each Item, add a

custom property (call it .key, say) equal to the unique ID of the object

that the listbox item represents. Then, to get hold of the object you're

after, just use:

myObject = arrayOfHomemadeObjects[listBox.selection.key];

I think that's as direct as it can be.

Ariel

McShamanAuthor
Inspiring
October 15, 2013

It's worth googling MVVM (model, view, view-model). It's a standard

programming pattern, the idea behind which is to separate the UI from

the programming logic.

Possibly overkill for a little InDesign script though...

Ariel


I'm always keen to follow best practices.

Interesting read... Thanks for that Ariel.

Quick question...

The philosophy of MVVM seems to be content (model) - translation (view-model) - display (view). The philosophy of OOP is to keep your code modular.

The content component is the easy part, I can build an object with properties and methods to set and get this data. But the translation and display components are reliant on the existence of content component to work.

Normally I would bundle these concepts up into a single object... But from what I have read it seems the MVVM suggests separating these concepts. Is that right?

e.g.

var DataClass = function() {

     // Properties and set/get methods

}

var UIClass = function() {

     // Properties and methods to display user interface

}

var TranslatorClass = function() {

     // Properties and methods to communicate between DataClass and UIClass

}

McShamanAuthor
Inspiring
October 14, 2013

Anybody?

Participating Frequently
October 8, 2013

var names = ["1", "2"];

var w = new Window ("dialog", undefined,undefined, {closeButton: false});

w.alignChildren = "right";

var main = w.add ("group");

var list = main.add ("dropdownlist", [0,0,240,20], names);

list.minimumSize.width = 150;

var buttons = w.add ("group")

buttons.add ("button", undefined, "OK", {name: "ok"});

buttons.add ("button", undefined, "Cancel", {name: "cancel"});

if (w.show ()==1){

    if  (list.selection==0){

        alert ("1")

        }

    else if (list.selection==1){

         alert ("2")

        }

    else{

        alert ("Select Drop list")

        }

    }

McShamanAuthor
Inspiring
October 8, 2013

This solution is no good for me as the list I will be generating is dynamic (based on elements in a docment). There is no way for me to know how many elements there will be in a users document or what they are so I cant possibly do an if statement for each itme.