Copy link to clipboard
Copied
Hi,
i recently started scripting and wrote a script in VS Studio for After Effects, which works fine when i run it from Visual Studio. But when i save it and run the script from After Effect it gives me an error message: "unable to execute at line 20..."
which is a function in my script. everything before gets executed.
I have Variables (a,b) in my function because i am looking for several Objects meaning CompItem, FootageItem, Folderitem where I'm looking for specific names as a string.
The Function looks like that:
function graballItems(a, b) {
var proj, curItem, itemTotal, ItemAry;
proj = app.project;
itemTotal = proj.numItems;
ItemAry = new Array();
for (var i = 1; i <= itemTotal; i++) {
curItem = proj.item(i);
if (curItem instanceof b && curItem.name.indexOf(a) !== -1) {
ItemAry = curItem;
}
}
return ItemAry;
}
// And i use it like that:
var myCompBeauty = graballItems("CAN_ENDTAG_PRE", CompItem);
But strange though, that it is working from VS Studio.
Does anybody know why the error message appears?
Cheers
Martin
Copy link to clipboard
Copied
Just tested and it works for me after commenting out that line. Try putting it in a try catch and see if you can get any more info on the error.
Copy link to clipboard
Copied
The line "ItemAry = new Array()" suggests that you want to return an array of the found items. But in the line
ItemAry = curItem;
you replace your array with a single item.
If you want to grab all items, you most likely want to do
ItemAry.push(curItem)
instead.
Copy link to clipboard
Copied
First of all, thank you both for your quick reply.
I tried to implement the try and catch statement but without any sucess.
I implemented the push item command but recognized that i only need one item not an array so i changed it to just get the curitem. But still the same error.
Last thing i wanted to try to set an alert line by line to check where the code breaks exactly.
Strange enough the script works like that:
Copy link to clipboard
Copied
what happens, if you set a breakpoint in the debugger instead of the alert and go through the code execution step by step?
And just to be sure: Did you check if your script behaves as you want when there is no item found (and hence an empty array is returned)?
Copy link to clipboard
Copied
also, if you just need a single item, you can change the code as follows:
function graballItems(a, b) {
alert("OK");
var proj, curItem, itemTotal;
proj = app.project;
itemTotal = proj.numItems;
for (var i = 1; i <= itemTotal; i++) {
curItem = proj.item(i);
if (curItem instanceof b && curItem.name.indexOf(a) !== -1) {
return curItem;
}
}
throw new Error("could not find the item!");
}
This has two important changes:
- When the first item is found, the search is stopped (which makes the script faster)
- when nothing is found, an error is thrown
If you don't want to throw an error if it was not found, you can also return null or undefined, or whatever your script needs in that case, but then you have to make sure that the code using the function deals with that null properly.
Copy link to clipboard
Copied
also, grabFirstItem might then be a better name than grabAllItems
Copy link to clipboard
Copied
thanks for your help, Mathias!
Sorry for the noob question how do i set breakpoints in my script in VS Studio?
Did all the implementations work all fine but still, only when i have this alert line activ.
Without the alert it gives me the Error.
But as said only when i save it. When i launch it from VS Studio directly everything works fine as well.
Ah and the script will always find the specific elements (comps, footage, and folder).
It's a script for a specific Project , but good to have the throw error statement, though.
Copy link to clipboard
Copied
Isn't it strange that the script works as it should when the alert is active, and when it is not it throws me an error?
Copy link to clipboard
Copied
For setting break points, see the section "Debugging" here
https://marketplace.visualstudio.com/items?itemName=Adobe.extendscript-debug
The break points are the red dots left of the line numbers in the editor.
At a break point, the code execution will pause and then you can go thought the code line by line and see where the error shows up.
And yes, it is very strange if it works inside VS Code but but inside Ae.
Copy link to clipboard
Copied
Hi,
i did the debugging but didn't find an error and the same behaviour occurs.
Launching it via the debugger works but launching the script via afx gives me an error, when i comment out the alert.
With the alert active it works.
Copy link to clipboard
Copied
If the alert fixes the issue, I suspect that it is some weird timing issue. What happens if you move the alert to a later point in time? Try moving it down one line step by step and see at which point the alert does not help anymore.
Then you know which line causes the trouble.
Also, how do you exactly execute the script in Ae? File->Scripts->Run Script File, or is it a panel, or do you execute it from the command line?
Copy link to clipboard
Copied
I was currently exactly trying what you mentioned and came to the conclusion that the error must appear in the usage of the function later on.
When i move the alert after the last line it throws an error.
Copy link to clipboard
Copied
By the way, does it make a difference if you replace
curItem instanceof b
by
curItem instanceof CompItem
?
Not sure if the old Extend Script handles instanceof with variables properly. It should be possible, but it is the only part of your code that seems a bit unusual to me.
Copy link to clipboard
Copied
Yes it seems like there is a problem with the variables. At least wit the second time i use the Compitem variable.
But i use this function to find folders and footage as well. That's why i used the variable.
Otherwise i have to write the for loop 3 times
Copy link to clipboard
Copied
I tried now to replace the funtion and the lines where i call the function with the for loops respectivly.
So i coded four foor loops without variables and and it gives me the same error message, though.
Copy link to clipboard
Copied
Can you post your updated code and the line that you're recieving the error on?
Copy link to clipboard
Copied
Yes, here is the updated Version:
Copy link to clipboard
Copied
And it is the same error message. The line number differs but it is always between:
Copy link to clipboard
Copied
And here is the complete Code with function and function calls. And the alert between the first and second function call. That's how it would work from AFX.