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

ID Scripting: unique id of something. Can I find out what it belongs to?

Participant ,
Jun 05, 2021 Jun 05, 2021

Copy link to clipboard

Copied

Hello everyone

When processing a list of text variables that are based on the text styles of different classes (ParagraphStyle, CharacterStyle), I want to get a combined array of such different class styles, for example, by their unique ids, so that later I can get a reference to the style object by its id.

But there is no superclass for all text styles, and, accordingly, there is no collection that brings together different style classes. And there is no way to use the itemByID method...


Is there any method (outside of collections) that would return a reference to an object by its unique id?
(Maybe the solution has something to do with the use of Specifiers)

TOPICS
Scripting

Views

766

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 , Jun 07, 2021 Jun 07, 2021

Hi vnh68,

are you looking for textVariableInstances ?

 

There is a unique id on every text variable instance:

http://jongware.mit.edu/idcs6js/pc_TextVariableInstance.html#id

 

And you can reach that textVariableInstance with:

app.documents[0].pageItems.itemByID( idNumber );

 

But to return textVariableInstances with that method you have to resolve them like that:

app.documents[0].pageItems.itemByID( idNumber ).getElements()[0];

 

Here comes the issue: Such a textVariableInstance is not directly

...

Votes

Translate

Translate
Community Expert ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

Not sure exactly what you're going for, but could you not just stash them in a nested array or object as you are processing the list, ie. 

 

var txtVarFinds = {

       charStyleFinds: [],

       parStyleFinds: [],

}

for each text variable:

if style instanceof Character Style: 

txtVarFinds.charStyleFinds.push(aFind);

 

Then operate on each object property after?

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 ,
Jun 06, 2021 Jun 06, 2021

Copy link to clipboard

Copied

brianp311, thank you for your answer.

I have a very similar code now. 
I want to process everything monotonous without unnecessary sorting, like

 

var textVariablesStyles = { 
    appliedParagraphStyle: [], 
    appliedCharacterStyle: [] 
};
app.activeDocument.textVariables.everyItem().getElements()
.filter(function(tv){ 
    return (tv.variableType == VariableTypes.MATCH_PARAGRAPH_STYLE_TYPE) ||
           (tv.variableType == VariableTypes.MATCH_CHARACTER_STYLE_TYPE);
})
.forEach(function(tv){
   switch (tv.variableType) {
      case VariableTypes.MATCH_PARAGRAPH_STYLE_TYPE:
          textVariablesStyles.appliedParagraphStyle
          .push(tv.variableOptions.appliedParagraphStyle);
          break; 
      case VariableTypes.MATCH_CHARACTER_STYLE_TYPE:
          .push(tv.variableOptions.appliedCharacterStyle);
          break;
   }
});

 

If there was a way in scripting to find an instance of a class by id, then one would not have to use loops too often to filter the desired objects.

I may be wrong, but it seems to me that InDesign gives unique ids to persistent objects (also styles) from the same source, and they never repeat. I had such an idea, I just decided to check.

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 ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Hi vnh68,

are you looking for textVariableInstances ?

 

There is a unique id on every text variable instance:

http://jongware.mit.edu/idcs6js/pc_TextVariableInstance.html#id

 

And you can reach that textVariableInstance with:

app.documents[0].pageItems.itemByID( idNumber );

 

But to return textVariableInstances with that method you have to resolve them like that:

app.documents[0].pageItems.itemByID( idNumber ).getElements()[0];

 

Here comes the issue: Such a textVariableInstance is not directly part of the pageItem collection:

http://jongware.mit.edu/idcs6js/pc_PageItem.html

 

Nevertheless you can loop through that collection using ID numbers to filter them.

Something like that:

var tempRectangle = app.documents[0].rectangles.add();
var maxIDNumber = tempRectangle.id;
tempRectangle.remove();

var allTextVarInstances = [];

for( var n=0; n<maxIDNumber; n++ )
{
	try
	{
		var theObjectResolved = app.documents[0].pageItems.itemByID( n ).getElements()[0];
		if( theObjectResolved.constructor.name == "TextVariableInstance" )
		{
			allTextVarInstances[ allTextVarInstances.length++ ] = theObjectResolved ;
		};
	}catch(e){};
	
};

 

As you can see from the first three lines in the code I used a little trick to determine where the loop should end.

 

Regards,
Uwe Laubender

( ACP )

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 ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

Hi Uwe,

Thanks for the tip! I once read about getting the maximum value of an id, but then I didn't need it. In addition, there were doubts that even TextVariableInstance could be found in the PageItems collection.

As for the TextVariableInstances, I group them like this:

app.activeDocument.stories.everyItem().textVariableInstances.everyItem().getElements();

But I'm really interested in getting a reference to an instance of a persistent class by its id. This hint looks like it should work as it should!

 

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 ,
Jun 07, 2021 Jun 07, 2021

Copy link to clipboard

Copied

LATEST

Hi vnh68,

that walk through all available ids of a document should get you some interesting insides what kind of objects are addressable by their id. Table cells are not included, even if there is an id for table cells.

This method is not very handy if it comes to variableTextInstances, your code does a better job, but the method will find deeply nested MSOs for example in states that are currently not active where even the allPageItems array of a document will not list them.

 

app.documents[0].pageItems.itemByID( idNumber )

is a powerful method to address deeply nested objects directly.

 

Regards,
Uwe Laubender

( ACP )

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