Activity › Forums › Adobe After Effects Expressions › Load expression into script from a file
-
Load expression into script from a file
Posted by Nick Leigh on December 1, 2010 at 2:22 pmIs there a way I can load an expression into a script from a file?
The reason I ask is because I have an expression that is about 150 lines of code and if I ever needed to make changes to it would be a pain when it is all on one line.
The expression file will be in a folder called expressions that is in the same folder as the script.
Here is what I have now:
// home score
var homeScore = myComp.layers.addText("0");
homeScore.position.setValue([818,124]);
homeScore.text.sourceText.expression = "Expression code all on one line"
Thanks for the help.
Nick
Nick Leigh replied 15 years, 5 months ago 3 Members · 9 Replies -
9 Replies
-
Dan Ebberts
December 1, 2010 at 2:33 pmSure. Check out Adobe’s JavaScript Tools Guide for all the info on how to open, read, and close a file.
Dan
-
Nick Leigh
December 1, 2010 at 4:41 pmHey Dan,
So I have done some reading and looking around at other posts and still can’t get my script to read in the file contents. I tried using read() but I get an error. This is what I have so far:
var hsFile = new File("/C/Program Files/Adobe/Adobe After Effects CS4/Support Files/Scripts/scoringexpression.js");
if (hsFile != null)
{
var hsFileText = hsFile.open('r');
if (hsFileText)
{
var hsExpression = eval(hsFileText);
homeScore.text.sourceText.expression = hsExpression;
}
else{
homeScore.text.sourceText.expression = '6';
}
}
Just have the 6 there so I know if it succeeded or failed.I also posted the expression code I am trying to put in the sourceText for the text layer below.
Thanks again for any help.
if (time > 0)
{
count = 0;
c = thisComp.layer("Game Markers");
n = c.marker.nearestKey(time).index;
if (c.marker.key(n).time > time) {n--;}// loop thru
for (i = n; i > 0; i--)
{
if (c.marker.key(i).comment == 3)
{
count++;
}
}
}
else
{
count = 0;
}away = count;
CS5 Production Suite
-
Chris Wright
December 1, 2010 at 5:36 pmhere’s an advanced text input script with line spacing, or creating just one text layer containing all the text.
just smash the line text code into eval
crg_Text_from_File.jsx
https://www.crgreen.com/aescripts/ie/https://technicolorsoftware.hostzi.com/
-
Dan Ebberts
December 1, 2010 at 6:20 pmThis works for me. From here you should just be able to stuff hsFileText into the source text expression (no need to use eval()).
var hsFile = new File("/C/Program Files/Adobe/Adobe After Effects CS4/Support Files/Scripts/scoringexpression.js");
if (hsFile != null)
{
hsFile.open('r');
var hsFileText = hsFile.read();
}
alert (hsFileText);
Dan
-
Nick Leigh
December 1, 2010 at 8:12 pmDan,
Yes that worked for me know as it is now putting the file contents in the sourceText but now my script ends as soon as that happens. It’s almost like it has a return or exit. It works perfect up to this point and then exits the script for some reason.
var homeScore = myComp.layers.addText("0");
homeScore.position.setValue([818,124]);
myHomeS = homeScore.property("ADBE Text Properties").property("ADBE Text Document");
var myhsDoc = myHomeS.value;
myhsDoc.font = "Square721BT-BoldExtended";
myhsDoc.fontSize = 38;
myhsDoc.fillColor = [1,1,1];
myhsDoc.justification = ParagraphJustification.CENTER_JUSTIFY;
myHomeS.setValue(myhsDoc);
homeScore.name = "Home Score";
// get score expression file
var hsFile = new File("/C/Program Files/Adobe/Adobe After Effects CS4/Support Files/Scripts/Expressions/HomeScoreExp.js");
if (hsFile != null)
{
hsFile.open('r');
var hsFileText = hsFile.read();
homeScore.text.sourceText.expression = hsFileText;
}And if I comment out the expression part the script runs out properly. Anyone know why that might be? Thanks
Nick
CS5 Production Suite
-
Dan Ebberts
December 1, 2010 at 9:14 pmI’m confused. What else do you want it to do? I tried it and it applies the expression and exits. Isn’t that what you expected?
BTW – you should probably add a hsFile.close() after the read().
Dan
-
Nick Leigh
December 2, 2010 at 12:55 pmDan,
That was only a piece of the script. I need to do the same thing to 3 different layers. So the middle of my script looks like this:
// add the text layers needed for the scoreboard
// clock
var theTime = myComp.layers.addText("10:00");
theTime.position.setValue([1554,124]);
myTime = theTime.property("ADBE Text Properties").property("ADBE Text Document");
var myTimeDoc = myTime.value;
myTimeDoc.font = "Square721BT-BoldExtended";
myTimeDoc.fontSize = 38;
myTimeDoc.fillColor = [1,1,1];
myTimeDoc.justification = ParagraphJustification.CENTER_JUSTIFY;
myTime.setValue(myTimeDoc);
theTime.name = "Score Clock";
// get clock expression file
var ceFile = new File("/C/Program Files/Adobe/Adobe After Effects CS4/Support Files/Scripts/Expressions/clockExp.js");
if (ceFile != null)
{
ceFile.open('r');
var ceFileText = ceFile.read();
theTime.text.sourceText.expression = ceFileText;
}// home score
var homeScore = myComp.layers.addText("0");
homeScore.position.setValue([818,124]);
myHomeS = homeScore.property("ADBE Text Properties").property("ADBE Text Document");
var myhsDoc = myHomeS.value;
myhsDoc.font = "Square721BT-BoldExtended";
myhsDoc.fontSize = 38;
myhsDoc.fillColor = [1,1,1];
myhsDoc.justification = ParagraphJustification.CENTER_JUSTIFY;
myHomeS.setValue(myhsDoc);
homeScore.name = "Home Score";
// get score expression file
var hsFile = new File("/C/Program Files/Adobe/Adobe After Effects CS4/Support Files/Scripts/Expressions/HomeScoreExp.js");
if (hsFile != null)
{
hsFile.open('r');
var hsFileText = hsFile.read();
homeScore.text.sourceText.expression = hsFileText;
}// away score
var awayScore = myComp.layers.addText("0");
awayScore.position.setValue([1230,124]);
myAwayS = awayScore.property("ADBE Text Properties").property("ADBE Text Document");
var myasDoc = myAwayS.value;
myasDoc.font = "Square721BT-BoldExtended";
myasDoc.fontSize = 38;
myasDoc.fillColor = [1,1,1];
myasDoc.justification = ParagraphJustification.CENTER_JUSTIFY;
myasDoc.text = "0";
myAwayS.setValue(myasDoc);
awayScore.name = "Away Score";
// get score expression file
var asFile = new File("/C/Program Files/Adobe/Adobe After Effects CS4/Support Files/Scripts/Expressions/AwayScoreExp.js");
if (asFile != null)
{
asFile.open('r');
var asFileText = asFile.read();
awayScore.text.sourceText.expression = asFileText;
}
And the script exits right away loading the first file. Am I doing something wrong?
CS5 Production Suite
-
Dan Ebberts
December 2, 2010 at 5:17 pmIt looks like it should work unless there’s something wrong with the first expression or the path to it. Have you tried setting a breakpoint just beyond the first section to see how far it gets and making sure that it can get that first expression installed and the expression doesn’t generate an error?
Dan
-
Nick Leigh
December 2, 2010 at 7:44 pmHey Dan,
So it was an order issue with how I was putting the layers in because the expression was referencing a layer that hadn’t been created yet. Thanks for all your help.
Nick
CS5 Production Suite
Reply to this Discussion! Login or Sign Up