Page 1 of 1

Retrieving user entered value?

Posted: February 14th, 2009, 6:30 pm
by vfxman
I am running some experiments and teaching myself AE Dockable ScriptUI building (yes, I know my first script was a dockable panel, but only with Paul's help. :D ) and I can't seem to find the answer/syntax for retrieving a user entered value from an edittext box within the AE script language. I created my text box like this...

Code: Select all

myPanel.butTwo = myPanel.add("editText", [45, 10, 100, 30], "5");
I set 5 as a default, but the user can change it to say 100 or whatever.

The full script is here: (I used Lloyd Alvarez's duplicate layer script from the AENY video as a test and built the UI via Ian Haigh's Ease & Whiz script style cause it made more sense to me)

Code: Select all

function Dup_buildUI(thisObj) {
var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Duplicator", [100, 100, 300, 300]);

	//Jeff Almasol's solution to fix text color
	var winGfx = myPanel.graphics;
	var darkColorBrush = winGfx.newPen(winGfx.BrushType.SOLID_COLOR, [0,0,0], 1);

//notes------------------------------------[10, 10, 40, 30] = [LeftEdge, TopEdge, ButtonEdge, ButonEdge]
//"Qty:" text
myPanel.butOne = myPanel.add("staticText", [10, 10, 40, 30], "Qty:");

//Input box
myPanel.butTwo = myPanel.add("editText", [45, 10, 100, 30], "5");
myPanel.butTwo.graphics.foregroundColor = darkColorBrush;	//Keeps text black at all times

//"Do It!" button
myPanel.butThree = myPanel.add("button", [10, 40, 70, 60], "Dup It!");
myPanel.butThree.onClick = dupIt;	//launch "dupIt" function when button is clicked

return myPanel;
}

	function dupIt()
	{

	app.beginUndoGroup("Dup It!")
	
	var myComp = app.project.activeItem;
	if(myComp instanceof CompItem) {
		
    //Selected layers loop
    var myLayers = myComp.selectedLayers;
	for (var i = 0; i < myLayers.length; i++) {
	var myLayer = myLayers[i]
			
	var selLayers = myLayer;

	var numCopies = 10;

				var counter = 0;
			
			app.beginUndoGroup("Dup It!")

				while(counter < numCopies){
					selLayers.duplicate();
					counter++
			app.endUndoGroup()
			}

		}
	}
}


Dup_buildUI(this);
You can also see that right now there is one line (line 42 if you want to count) set like this:

Code: Select all

var numCopies = 10;
The script works and duplicates the layer 10 times with this set value, but I want to plug in the user entered data.

Re: Retrieving user entered value?

Posted: February 14th, 2009, 7:30 pm
by Yenaphe
I'm not a killer with UI objects, but does something like this works:

Code: Select all

var numCopies = eval(myPanel.butTwo.value); //you have to use eval to convert a text string to a usable number

Re: Retrieving user entered value?

Posted: February 14th, 2009, 7:38 pm
by vfxman
Yenaphe wrote:

Code: Select all

var numCopies = eval(myPanel.butTwo.value); //you have to use eval to convert a text string to a usable number
Thanks Yenaphe, but it didn't work.

Re: Retrieving user entered value?

Posted: February 15th, 2009, 4:21 am
by Paul Tuersley
And if you put in something like alert(myPanel.butTwo.value) you get an error saying myPanel is not defined. Which isn't surprising because you defined the myPanel variable in the Dup_buildUI function, and variables can only be accessed within the scope of their defining function.

I only know of one way to get a script working properly with a dockable panel, based on Jeff's scripts at http://www.redefinery.com. If I apply that structure to your script I can get it to work, but don't ask me how.

Code: Select all

{
	function Dup(thisObj) {
	
		function Dup_buildUI(thisObj) {
			var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Duplicator", [100, 100, 300, 300]);

		   //Jeff Almasol's solution to fix text color
		   var winGfx = myPanel.graphics;
		   var darkColorBrush = winGfx.newPen(winGfx.BrushType.SOLID_COLOR, [0,0,0], 1);

			//notes------------------------------------[10, 10, 40, 30] = [LeftEdge, TopEdge, ButtonEdge, ButonEdge]
			//"Qty:" text
			myPanel.butOne = myPanel.add("staticText", [10, 10, 40, 30], "Qty:");

			//Input box
			myPanel.butTwo = myPanel.add("editText", [45, 10, 100, 30], "5");
			myPanel.butTwo.graphics.foregroundColor = darkColorBrush;   //Keeps text black at all times

			//"Do It!" button
			myPanel.butThree = myPanel.add("button", [10, 40, 70, 60], "Dup It!");
			myPanel.butThree.onClick = dupIt;   //launch "dupIt" function when button is clicked

			return myPanel;
		}

		function dupIt() {

			var myComp = app.project.activeItem;
			
			if(myComp instanceof CompItem) { 
				
				app.beginUndoGroup("Dup It!");

				var counter = 0;	
				var numCopies = parseInt(dupItPal.butTwo.text);
				
				//Selected layers loop
				var myLayers = myComp.selectedLayers;
				for (var i = 0; i < myLayers.length; i++) {
					
					var myLayer = myLayers[i];	 
					var selLayers = myLayer;	 

					while(counter < numCopies){
						selLayers.duplicate();
						counter++;
					}
				}
				app.endUndoGroup();
			}
		}

		var dupItPal = Dup_buildUI(thisObj);
		
		if ((dupItPal != null) && (dupItPal instanceof Window)) {
			dupItPal.center();
			dupItPal.show();
		}         
	}

	Dup(this);
}

Re: Retrieving user entered value?

Posted: February 15th, 2009, 6:24 am
by Yenaphe
Paul, what is the main difference between eval(); and ParseInt(); ?

Re: Retrieving user entered value?

Posted: February 15th, 2009, 6:45 am
by Paul Tuersley
I don't think I've ever used eval() in a script. I googled it and found "The eval() function evaluates a string and executes it as if it was script code."

So for example to get a script to run another script you might read the entire contents of the text file as a string and eval() it. parseInt() is used to convert a string into an integer, while parseFloat() will interpret a string as a float/decimal value.

Re: Retrieving user entered value?

Posted: February 15th, 2009, 6:58 am
by Paul Tuersley
Thinking about this a bit more I realised you could just make sure you initially defined myPanel within the main scope of the script.

Code: Select all

{
	var myPanel;

	function Dup_buildUI(thisObj) {
		myPanel = (thisObj instanceof Panel) ? thisObj : new Window("palette", "Duplicator", [100, 100, 300, 300]);

	   //Jeff Almasol's solution to fix text color
	   var winGfx = myPanel.graphics;
	   var darkColorBrush = winGfx.newPen(winGfx.BrushType.SOLID_COLOR, [0,0,0], 1);

		//notes------------------------------------[10, 10, 40, 30] = [LeftEdge, TopEdge, ButtonEdge, ButonEdge]
		//"Qty:" text
		myPanel.butOne = myPanel.add("staticText", [10, 10, 40, 30], "Qty:");

		//Input box
		myPanel.butTwo = myPanel.add("editText", [45, 10, 100, 30], "5");
		myPanel.butTwo.graphics.foregroundColor = darkColorBrush;   //Keeps text black at all times

		//"Do It!" button
		myPanel.butThree = myPanel.add("button", [10, 40, 70, 60], "Dup It!");
		myPanel.butThree.onClick = dupIt;   //launch "dupIt" function when button is clicked

		return myPanel;
	}

	function dupIt() {

		var myComp = app.project.activeItem;
		
		if(myComp instanceof CompItem) { 
			
			app.beginUndoGroup("Dup It!");

			var counter = 0;	
			var numCopies = parseInt(myPanel.butTwo.text);
			//Selected layers loop
			var myLayers = myComp.selectedLayers;
			for (var i = 0; i < myLayers.length; i++) {
				
				var myLayer = myLayers[i];	 
				var selLayers = myLayer;	 

				while(counter < numCopies){
					selLayers.duplicate();
					counter++;
				}
			}
			app.endUndoGroup();
		}
	}

	Dup_buildUI(this);
}
Although one benefit of doing it the other more complicated way is that you can run the script as either an old style palette (using Run Script) or a dockable panel (by putting it in ScriptUI Panels and running from the Window menu), while this version will only work as a dockable panel.

Re: Retrieving user entered value?

Posted: February 15th, 2009, 5:42 pm
by vfxman
Hey Paul, thanks for the solution and explanation. I understand now why it wasn't finding 'myPanel.butTwo' in the numLayers variable. I feel a little more confident now trying to possibly tackle an update to one of my previous scripts all on my own. We'll see though, lol. Thanks again.