Best Way to Make a Specific CompItem, the activeItem

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
pfmurphy
Posts: 6
Joined: February 18th, 2007, 3:30 am
Location: Los Angeles, CA

I have been banging my head against this issue because half the time things seem to work, and the other half is spent testing my chair's durability against a katana sword.

My understanding is that in order to 'do stuff' with an object's attribute (a JavaScript object property, as defined in Scripting Manual), the object must be called into focus/made to be the activeItem upon which the script references for code execution.

There are many instances in which a script may call upon objects in a project, that need to be referenced through difering criteria. For example:

1. Check to see if there is an activeItem in the project panel and test to make sure that item is a CompItem (has been successfully implemented)

2. Unselect the activeItem(s) in a project so that the focus can change to a new object requiring different parameters to become the activeItem, (has been successfully implemented)

3. Make active any type of item in the project panel, that was not already selected (50% successful , 50% chair left).
- this can be accomplished by referencing the item[index]. But what if you do not know / or cant force what that index # is?
- this can presumably be accomplished referencing the app.project.item.name? It makes sense that I should be able to do this
but it almost never works. And when it does, the next project I open, it seems to stop working. Here is a snippet:

Code: Select all

var selectedComp = new Array();
for (var i = 1; i <= app.project.items.length; i++)
   {
    if ((app.project.items[i] instanceof CompItem)&&(app.project.items[i].name == "RENDER")) 
	   selectedComp[selectedComp.length] = app.project.items[i];
	   selectedComp.selected = true; 
    }
I know I am doing many things wrong. Most importantly, I am concerned that I am misunderstanding the principals behind scripting. Guidance, instruction, thoughts etc., would be appreciated.
User avatar
redefinery
Posts: 112
Joined: April 1st, 2005, 8:16 pm
Location: CA
Contact:

Hi pfmurphy,

Just to clear up some stuff, only one project item can be active at a time (i.e., activeItem will be null if 0 or more than 1 item is selected), but there can be multiple selected items.

Your check for detecting comps named "RENDER" is fine. However, you are trying to select the array (selectedComp.selected = true), not the actual item. You can do that as:
app.project.items.selected = true;
or:
selectedComp[selectedComp.length-1].selected = true;
Notice in this second example the use of -1. Because the previous line added an entry to the array, and because array indices start at 0, you need to reference it as the array's length - 1.

If you're wanting to select only the comps that are named "RENDER" and deselect everything else, one thing you can do is place the following before your if statement:
app.project.items.selected = false;
That way you're always forcing the deselection of the current item in the loop, and selecting it only if the if statement is true.

Hope that helps.

Jeff
pfmurphy
Posts: 6
Joined: February 18th, 2007, 3:30 am
Location: Los Angeles, CA

Wow! Thank you so much! You have truly clarified an issue and a mis-understanding that I have had with scripting. Expecially the idea that there is a distinction between active items and selected items. To truly grasp this, I will do my homework and seek out further explanation.

Once again, thank you!

By the way - many thanks to you for allowing "us" access to the scripts on your site. They continually prove themselves as valuable production tools, excellent examples to those of us who are learning, and timesavers when embarking on the often tedious task of writing my own scripts.
Post Reply