Make a function/script run onClick, not directly on load?

Moderators: Disciple, zlovatt

Post Reply
klinus
Posts: 1
Joined: September 3rd, 2015, 4:11 am

Hi!

I'm fairly new to scripting, but have managed to put together a script trough copy/paste and googling from other scripts. The script work more or less exactly as I want to if I select it from "file > scripts". I did a button, and its there, but the function just starts as I load the script, and the button is not working.

If I replace the function with a simpler one, the button work, so I know it is in my function, but I cant seem to understand what.

Credits: the top part is credited to a script called "3D Layer Offset" by Brian Charles, and the base of the function, where it searches through the layer and connects all paths to their duplicate, is borrowed from the script "duplicateWithConnections" by Michael Gochoco.

Code:

Code: Select all

// offset strokes

var win = new Window('palette', 'Offset stroke');
	this.windowRef = win;
	// Create a container panel for the components
	win.pnl = win.add("panel", [5,5,170,80], '');
	// Use the panel's add() method to create components

	// Add OK/Cancel buttons
	win.pnl.okBtn = win.pnl.add("button", [40,10,120,60], 'Stroke!');
	// win.pnl.cnlBtn = win.pnl.add("button", [90, 40, 140, 60], 'Cancel');
	// Define the behavior of the buttons
	
    win.pnl.okBtn.onClick = function()
	{
	//alert("button pressed"); //debug
        traverseGroup();
         main();
         }

	//win.pnl.cnlBtn.onClick = function()
	//{
		//$.writeln("Cancel Button Pressed");
		//win.close();
	//}
	// Display the window  
   win.show();
    
    
	var proj = app.project;
    var useIndex = false;
    var useIndexLayers = false;

function traverseGroup(inGroup, inPath)
	{
		var leng = inGroup.numProperties;
		for(var i = 1; i <= leng; i++)
		{
			var path = inPath;
			if(inGroup.property(i).enabled && inGroup.property(i).active)
			{
				if(inGroup.property(i).propertyType == PropertyType.PROPERTY)
				{
                    
					if(inGroup.property(i).canSetExpression)
					{
						try
						{
                                if(useIndex)
                                    path += "(" + inGroup.property(i).propertyIndex + ")";
                                else
                                    path += "(\"" + inGroup.property(i).name + "\")";
                            
                                // if we can set an expression
                                if(inGroup.property(i).canSetExpression)
                                {
                                    try
                                    {
                                        inGroup.property(i).expressionEnabled = true;
                                        inGroup.property(i).expression = path;
                                        
                                    }
                                    catch(err) // skip if property cannot be set
                                    {}
                                     
                                }
						}
						catch(err) // skip if property cannot be set
						{}
					}
				}
            
				else
				{
                        if(useIndex)
                            path += "(" + inGroup.property(i).propertyIndex + ")";
                        else
                            path += "(\"" + inGroup.property(i).name + "\")";
                            
					traverseGroup(inGroup.property(i), path);
                            var myFill = path; 
                                        
				}
			}
		}
	}
function main()
	{
		app.beginUndoGroup("duplicateWithConnections");

		if(proj)
		{
			if(proj.numItems > 0)
			{
				var myComp = proj.activeItem;
				
				if(myComp && myComp instanceof CompItem)
				{
					var target = myComp.selectedLayers;

                    if(target.length > 0)
                    {
						var compPath = "thisComp.";

						//if(confirm("Use absolute path?",true,"Confirm path"))
							compPath = "comp(\"" + myComp.name + "\").";

						//if(confirm("Use Index instead of Name for Properties?",true,"Confirm path"))
							useIndex = true;
                            
						//if(confirm("Use Index instead of Name for Layers?",true,"Confirm path"))
							useIndexLayers = true;
                            
						for( var j = 0 ; j < target.length ; j++)
						{
							target[j].selected = false;
							var path = compPath;
                                
                                if(useIndexLayers)
                                    path += "layer(" + (target[j].index+1) + ")";
                                else
                                    path += "layer(\"" + target[j].name + "\")";
         
                                var dupeComp = target[j].duplicate();
							dupeComp.name = target[j].name + " stroke";
                            	target[j].name = target[j].name + " fill";
							traverseGroup(dupeComp, path);
							dupeComp.selected = true; 
                             // remove fill 
                             var layer = proj.activeItem.selectedLayers[0];
                             var contents = layer.property("ADBE Root Vectors Group");
                             for(i = 1; i <= contents.numProperties; i++){
                            var shape = contents.property(i).property("Contents");
        if(shape.property("ADBE Vector Graphic - Fill")){
            shape.property("ADBE Vector Graphic - Fill").remove();
            }
      }
                            
                                    //add stroke
        var myStroke = dupeComp.property("Contents").addProperty("ADBE Vector Graphic - Stroke"); 
        var colorExpression = 'effect(3)(1)';   
        var widthExpression = 'effect(4)(1)';
        myStroke.property("Color").expression = colorExpression;
        myStroke.property("Opacity").setValue([100]);
        myStroke.property("Stroke Width").expression = widthExpression;
        myStroke.lineCap.setValue(2);
        //add sliders
        var xslider = dupeComp.Effects.addProperty("ADBE Slider Control"); 
        xslider.name = "X offset";
        xslider.property(1).setValue(-10);
        var yslider = dupeComp.Effects.addProperty("ADBE Slider Control"); 
        yslider.name = "Y offset";
        yslider.property(1).setValue(-10);
                var colorslider = dupeComp.Effects.addProperty("ADBE Color Control"); 
                colorslider.name = "Color";
        colorslider.property(1).setValue([1,1,1]/255);
        var widthslider = dupeComp.Effects.addProperty("ADBE Slider Control"); 
        widthslider.name = "Stroke width";
        widthslider.property(1).setValue(4);
        //var roundcheck = dupeComp.Effects.addProperty("ADBE Checkbox Control"); 
        //roundcheck.name = "Round caps";
        //widthslider.property(1).setValue(4);

                           //x and y position;
                          var posExpression = 'x=thisComp.layer("' + target[j].name + '").transform.position[0]+effect(1)(1);\ny=thisComp.layer("' + target[j].name + '").transform.position[1]+effect(2)(1);\n[x,y]';
							dupeComp.position.expression = posExpression;
        

						}
					}
                
					else
						alert("Please select layer(s) within an active comp!1");
				}
                  else
                    alert("Please select layer(s) within an active comp!2");
			}
              else
                alert("Please select layer(s) within an active comp!");
		}
        else
            alert("Please select layer(s) within an active comp!");



	}

	main();



		app.endUndoGroup();
        
        
stib
Posts: 21
Joined: December 10th, 2006, 10:23 pm
Contact:

you call the main() function right at the bottom, so it gets called when the script loads. I think that might be your problem. Comment out the last line and see how you go.
Post Reply