Expressions/Scripts/Presets

Moderator: Paul Tuersley

 
StanTheMan
Topic Author
Posts: 2
Joined: Wed Jul 05, 2017 3:56 am

Need help about a script! (New to scripting)

Wed Jul 05, 2017 4:03 am

Hello everyone.

I'm brand new to scripting (a few hours), 
and I have a script I need help with. 

Basically I want to make new comps from selected layers and have the start timecode to begin on 0, 
I think I've made that one correct. 

However... I'm getting this line "Undefined is not an object" when running the script.
The script does what I want it to do, but this error is bugging me and I want to learn how
to correct that error. 

Hopefully some gently souls will help me out!

Thanks in advance, 
StanTheMan


Script:

var selectedItems = app.project.selection;


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

var myComp = app.project.items.addComp(selectedItems[i].name, selectedItems[i].width, selectedItems[i].height, selectedItems[i].pixelAspect, selectedItems[i].duration, selectedItems[i].frameRate);

myComp.layers.add(selectedItems[i]);

myComp.displayStartTime = 0;

}

 
User avatar
zlovatt
Posts: 12
Joined: Mon Oct 31, 2016 5:00 pm
Location: Los Angeles
Contact:

Re: Need help about a script! (New to scripting)

Wed Jul 05, 2017 12:19 pm

Luckily this is a simple fix! Change from this--
for (var i=0; i <= selectedItems.length; i++) {

to--

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


The issue is that selectedItems is an array counting from 0; if there were 3 elements, you would access them by array[0], array[1], array[2]. So, you want the 'for' loop to count one less than the array length.
What's happening now is that it's doing array[0], array[1], array[2], and then it's trying to do array[3] but there isn't any! Because of the <=.
Check out this link for more info on arrays in javascript.
 
StanTheMan
Topic Author
Posts: 2
Joined: Wed Jul 05, 2017 3:56 am

Re: Need help about a script! (New to scripting)

Thu Jul 06, 2017 2:52 am

Thank you!

Much appreciated!

The start to learn scripting begins. 


/Stan

Who is online

Users browsing this forum: No registered users and 3 guests