Retrieving user entered value?

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
User avatar
vfxman
Posts: 49
Joined: February 23rd, 2007, 7:00 pm
Location: California
Contact:

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.
Last edited by vfxman on February 15th, 2009, 4:49 pm, edited 1 time in total.
Yenaphe
Posts: 84
Joined: February 3rd, 2009, 6:30 pm
Location: Paris - France
Contact:

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
User avatar
vfxman
Posts: 49
Joined: February 23rd, 2007, 7:00 pm
Location: California
Contact:

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.
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

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);
}
Yenaphe
Posts: 84
Joined: February 3rd, 2009, 6:30 pm
Location: Paris - France
Contact:

Paul, what is the main difference between eval(); and ParseInt(); ?
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

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.
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

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.
User avatar
vfxman
Posts: 49
Joined: February 23rd, 2007, 7:00 pm
Location: California
Contact:

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.
Post Reply