Skip to main content
Participant
March 12, 2018
Question

ScriptUI palette returns .pathItems as String, not actual Array of items.

  • March 12, 2018
  • 2 replies
  • 530 views

In basic terms I have a artboard covered in thousands rectangles and the script goes through each path/rectangle in the document, and add additional points to it (making it a hexagon), and then positions it on the artboard relative to the others. I had the script working fine as a dialog, but in switching it to a palette it has broken the code due to the complications with accessing PathItems.

I've been all over the internet trying to track this down, and currently this is what I think is happening:

  • In order to access application information from a "palette" in ScriptUI (not a 'window' or 'dialog'), you need to use BridgeTalk
  • Through bridgeTalk, results are being returned as a string.
  • This results in the 'app.activeDocument.pathItems' called through BridgeTalk is being returned as "[PathItems]" (a string) and not an array of the actual paths.

My script is gigantic, but the relevant portion is below. Is there any way around this? Thanks!

//Outside of the palette code

function getPaths(){

    return app.activeDocument.pathItems;

}

//Inside the palette code

//Get the total pathItems from the file

var bt2 = new BridgeTalk();

bt2.target = "illustrator";

bt2.body = getPaths+'getPaths();';

       

bt2.onResult = function(result){

     for (i=0; i < totalPills; i++ ) {

          myPaths = result.body;

     }

}

bt2.send();

This topic has been closed for replies.

2 replies

Silly-V
Legend
March 12, 2018

Using BridgeTalk, messages passed back and forth have to be strings. Using a pasted JSON object in your ExtendScript code, data objects can be passed back and forth and parsed into a real variable on the other end. (Using eval() and .toSource() (to my knowledge) does the same thing if you're not worried about some issues)

Now, the issue is that there's absolutely no way to actually transfer direct pathItems data from one end to the other and you're really limited to passing data back about the pathItems. Add another issue that as opposed to InDesign, there's no script-accessible art element unique IDs which could be used to referenced the specific items!

However, there could still be sneaky ways to accomplish your goals. First, you can make some recursively-active node-gathering function to get you a document art tree which your receiving script can use to 'reconstruct' the data object from your document. This way you would go through just the items you need to get just the items you want, without extra actions. Since you're likely getting your reference to the pathItems, you may be able to create a node-tree for each one by traveling up the items' parent containers all the way up to the document level. Indexing all the parents via index is really the only way to do this because in Illustrator things can be named the same name, and groups can be nested in groups and layers can be nested in layers.

Another simplistic way could be to utilize UI document artifacts, such as your document selection. After something is selected and a bridgetalk message arrives on the other end, those items will still be selected and instead of the whole document, perhaps the selection could be used to create and parse a more brief node tree. In the case of your items all being selected in one top-level layer, this node tree would not even have to exist.

Yet another way to get at the items is to alter their alterable properties and to add your own custom markers. For example if the situation allows, you can simply re-name all the concerned pathItems to your own custom unique name identifiers. The receiving script could get a list of your pathItems' original names and their new special names. After processing the string information array into pathItems data, the script can simply re-re-name the items back to their original names, even if they were empty strings.

Similarly, the .note of pageItems could be used, or even the cryptic Illustrator scripting tag property, which resides in the pageItem.tags array-like object.

Although I have never tried the renaming technique, I think it could be the fastest method now that I think about it. To add a unique identifier name to the pathItems, you can use new Date().getTime() to get a unique time-id for the script run and then add a dash (-1, -2) with an increment to signify the unique name for this script run and this pathItem.

chaconautAuthor
Participant
March 13, 2018

Thanks for the explanation. I did some tests with similar techniques regarding re-naming and re-parsing everything, however, the code runs from a for loop and due to the immense number of paths to rename (thousands), while it works the script hangs for so long it's just not worth the lag.

I think I'll revert back to 'dialogs' to avoid the BridgeTalk issue.

Silly-V
Legend
March 13, 2018

Yea, thousands of paths is a lot.

pixxxelschubser
Community Expert
Community Expert
March 12, 2018

Hi chaconaut​,

how does the string looks like? What for a delimiter it has?

At first: I cannot help you with your problem directly. But you can try the following workaroud:

Create a new Array from your string.

var a = "abd, cfg, htg";

alert(a);

var arr = a.split(",");

alert(arr);

alert(arr[0]);

Maybe someone else can help you to find a better way.

Have fun