Activity › Forums › Adobe After Effects Expressions › Check if comp exists before running rest of script
-
Check if comp exists before running rest of script
Posted by Danyl Bernard on October 19, 2020 at 4:42 pmHi all,
I’m trying to create loop that keeps checking if a comp exists. I want the loop to keep running until the comp exists, and once the comp is found run the rest of the script.
So far I have a basic for loop, but it doesn’t seem to be doing anything
for (var i=0 ; i<app.project.numItems.length; i ++)
if (app.project.item(i). name != “FINAL 2”)
{
break;
}
Thank you
Danyl Bernard replied 5 years, 6 months ago 3 Members · 7 Replies -
7 Replies
-
Brendon Murphy
October 19, 2020 at 8:52 pmFew things here:
-I believe your “for” loop should start with i=1 in this case, not 0.
-You are missing the curly braces that should follow your for conditions: for (var i=1 ; i<app.project.numItems.length; i ++)
– “!=” means “does not equal”. So you are currently looking for a comp that is NOT named “Final 2”. You will probably want to use ==
-“break” tells AE to leave the loop and do nothing else. You would not want this in front of any code that should be run when the condition is met.
-While you could theoretically place all of this code inside another loop that runs forever, that sounds like a recipe for disaster! After effects essentially freezes while a script is running – you could never do any action outside of the running script to create a comp. An endless loop might even cause your computer to freeze up and crash.
-
Danyl Bernard
October 19, 2020 at 9:10 pmThank you. I’m still fairly new to scripting and programming in general, so I’m working off other bits of after effects scripts and JavaScript that I find.
I figured an endless loop would cause issues. Is there a way to run a loop every 3 seconds so as to prevent that freeze from happening?
My scenario is like this. I have a third party script running in AE creating comps. Then I have a second script that I need to run after the first one is finished.
The first script is one I bought, and the second is one I made, so I can’t combine the two.
My solution was to run a loop at the beginning of my script, checking if the last comp which I know will be named FINAL 2 exists, and only then running the rest of the script
I should add that the third party script has a UI, and my script is running through a command prompt with AHK. The issue is, that AHK runs my script before the first one has a chance to finish, so I need some sort of indicator that the first script is done
-
Brendon Murphy
October 19, 2020 at 9:39 pmAs far as I know, the only way to run a script periodically would be to queue it from outside AE, like running it from a batch file using Task Scheduler (in Windows). It may make more sense to just run the script with your hotkey when the other one finishes – does it take longer than a second for it to execute?
Is your third party script a jsxbin? if its a jsx you can chain your scripts together.
-
Danyl Bernard
October 19, 2020 at 9:47 pmIt’s a jsxbin.
I’m using AHK to handle the first script because it uses a GUI, and doesn’t have a headless mode. The script takes anywhere from 5 to 120 seconds to run, and I don’t want to set a delay of 2 minutes before each time AHK runs my script (it’s a loop of about 60 projects which will delay it significantly).
I do know that the comp FINAL 2 will be the last one the jsxbin script creates
-
Stephen Dixon
October 19, 2020 at 11:02 pmIf you use CEP panels you can use the setInterval() method to run a loop, but I feel like learning how to use CEP panels may be more overhead than you need.
-
Brendon Murphy
October 20, 2020 at 1:03 amAh, ok. I don’t know much about AHK, but it seems like running the third party script is the hard part. If you’ve already got that going, maybe you can use a “sleep” delay to attempt running the checker Script from AHK every 15 sec. Write a function into the checker script so that every time it finishes, it writes a text file out to your hard drive with the word “done”. AHK can look for this log before trying the checker each time. If it finds the word “done”, it deletes the word and moves on to the next step.
-
Danyl Bernard
October 20, 2020 at 1:19 amI’m trying something similar with having the checking script creating an alert box that AHK waits for, but it’s still pretty buggy.
Thanks for the help
Reply to this Discussion! Login or Sign Up