Calling Functions with ScriptUI

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

I have written many scripts but cannot seem to overcome the obstacle of ScriptUI. My problems arise when trying to call certain functions with the buttons. Some functions seem to run and some don't. My script was working somewhat earlier today but I cannot figure out what is failing at the moment. Here's my code for what it's worth. It's a script that renders your queued items to a temporary location and then moves them back when finished.

Code: Select all

{
  //  function MAR_script(thisObj) {
        
    var myPalette = buildUI(this);

    if (myPalette != null && myPalette instanceof Window) {
        myPalette.show()
        }

    function buildUI (thisObject) {

    if (thisObject instanceof Panel) {
        var myWindow = thisObject;
        } else { 
        var myWindow = new Window ("palette", "My Window");
        }

        //alert( myWindow) ;
        
            myWindow.myPanel = myWindow.add("group");
            myWindow.myPanel.orientation = "column";
             myWindow.myPanel.titleText = myWindow.myPanel.add("staticText");
             myWindow.myPanel.titleText.text =  "Move After Render v1.0";      
             myWindow.myPanel.okButton = myWindow.myPanel.add("button");
            myWindow.myPanel.okButton.text = "Render";
             myWindow.myPanel.stopButton = myWindow.myPanel.add("button");
            myWindow.myPanel.stopButton.text = "Stop Render";
            myWindow.myPanel.resetButton = myWindow.myPanel.add("button");
            myWindow.myPanel.resetButton.text = "Reset";

            myWindow.layout.layout(true);
            myWindow.layout.resize();

            myWindow.myPanel.okButton.onClick = executeRender;			

            myWindow.myPanel.resetButton.onClick = function () {
                //alert("Cancel");
                prefLoad("Temp_Render_Location", 1, true);
                //myWindow.close();
                }
             myWindow.myPanel.stopButton.onClick = function () {
                alert("hold your horses there partner, this feature is not yet enabled!");
             
             }
    return myWindow;
    } //function buildUI () 


    function prefLoad(prefLoc,pType, reset){
        var curPrefData = ""
        if(reset){
            curPrefData = "";
                
            }else if(app.settings.haveSetting("BN_Move_File", prefLoc)) {
            curPrefData = app.settings.getSetting("BN_Move_File", prefLoc);
            }        
                    
        if(curPrefData){
               outData = curPrefData;
               //alert("loading " +prefLoc+ " from prefs ="+outData);
        }else{
            if(pType == 2){//2= file, 1=folder
                var findIt = File.openDialog("Please find the file for "+prefLoc, "");
                var outData = findIt;
            }else{	
                var findIt = Folder.selectDialog("Please find the location of the folder for "+prefLoc);
                var outData = findIt;
            }
        }
        if (app.settings.haveSetting("BN_Move_File", prefLoc)) {
          //alert("In savesetting");
         app.settings.saveSetting("BN_Move_File", prefLoc, outData.toString());
        }else{
          app.settings.saveSetting("BN_Move_File", prefLoc, outData.toString());
         //alert("In savePrefsAsLong, and string is " + outData.toString());
        }
    return outData;
    }//prefload


    function ChangeRenderLocations(){
            var tempArray = new Array();
            alert(MAR.renderFiles.length +" :start");
            //MAR.newLocation = Folder.selectDialog("Select a temporary render output folder...");
            MAR.newLocation = prefLoad("Temp_Render_Location", 1, false);
            
            if (MAR.newLocation != null) {
                app.beginUndoGroup(MAR.scriptName);
                
                // Process all render queue items whose status is set to Queued.
                for (i = 1; i <= app.project.renderQueue.numItems; ++i) {
                    MAR.curItem = app.project.renderQueue.item(i);
                    
                    if (MAR.curItem.status == RQItemStatus.QUEUED) {
                        alert(MAR.curItem.render);
                        // Change all output modules for the current render queue item.
                        for (j = 1; j <= MAR.curItem.numOutputModules; ++j) {
                            MAR.curOM = MAR.curItem.outputModule(j);
                            
                            MAR.oldLocation = MAR.curOM.file;
                           //alert(MAR.oldLocation.path);
                            MAR.curOM.file = new File(MAR.newLocation.toString() + "/" + MAR.oldLocation.name);
                            tempArray[MAR.fileIndex] = MAR.curOM.file;
                               MAR.fileIndex++;
                            //alert("New output path:\n"+MAR.curOM.file.fsName, MAR.scriptName);
                        }
                    }
                }
                
                app.endUndoGroup();
            }
        alert(tempArray.length);
        return tempArray;
        }
        
        
        function moveFiles(inputFiles){
            
            for(var i = 0; i < inputFiles.length; i++){
                        MAR.curFile = inputFiles[i];
                        MAR.curFile.copy(MAR.oldLocation.path +"/" + MAR.curFile.name);
                        MAR.curFile.remove();
                        alert("Moved the files from:\n"+ MAR.newLocation.toString() + "\nto\n" + MAR.oldLocation.path);
        
                
                }
        
        }	

    function Pow(){ 
        alert("Pow!")
        }

    function executeRender(){
        writeLn("rendering now...");
        var MAR = new Object; 

        MAR.scriptName = "Move File After Render";
        //MAR.the_file = File.openDialog("where is the file!?");

        writeLn(MAR.scriptName);

        MAR.proj = app.project;

        writeLn(MAR.proj);

        MAR.renderFiles = new Array();

        writeLn(MAR.renderFiles.length);

        //alert(MAR.renderFiles.length + MAR.fileIndex);

        MAR.fileIndex = 0;

        writeLn(MAR.renderFiles);

    MAR.renderFiles  = ChangeRenderLocations();
       // MAR.renderFiles = ChangeRenderLocations();
        Pow();
        
        MAR.proj.renderQueue.render();

        moveFiles(MAR.renderFiles);
        //moveFiles(MAR.renderFiles);
        //MAR.renderFiles = [];
        }

    //if(MAR.the_file.copy(MAR.the_folder +"/" + MAR.the_file.name) ){alert("success")};
   // }
//MAR_script(this);
}
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

The most obvious problem I see is that you're defining an object (MAR) in one of the functions and yet other functions are trying to access it. Something that is defined inside a function only has scope in that function. You'll need to define it outside any function (or inside a function that also contains all the functions that need access to it).

This post from the other day also deals with variable scope and script structure, so it might be useful:
http://www.aenhancers.com/viewtopic.php?f=8&t=2058

I also get an error if I cancel the open dialog after clicking the reset button because you're not checking if findIt == null.
byronnash
Posts: 321
Joined: July 7th, 2004, 2:30 pm
Location: Charlotte, NC
Contact:

Thanks Paul. I worked on it some more today and moved some of the variables our of that function into global space. For some reason now, when I launch it from the Window menu it opens a panel then throws a floating window like I launched the script from the File menu.

I looked at the post from the other day and added that stuff in. It didn't really help my situation here but it's good to be using best practices.
Paul Tuersley
Posts: 704
Joined: June 5th, 2004, 7:59 am
Location: London, UK

Regarding it opening a panel and a window, I'd suggest looking more carefully at the example on that other post.
When run from the Windows menu, "this" is the panel that AE creates. The last line of the script "myScript(this)" sends the panel (if it exists) to the main script function. Then there is this line in the script that checks if "thisObj" is a panel and uses it if there is one, or creates a window if there isn't:
var pal = (thisObj instanceof Panel) ? thisObj : new Window("palette", myScript_Data.scriptName, undefined);
Post Reply