1 Reply Latest reply on Oct 27, 2009 12:13 AM by Mylenium

    Help with first Adobe Script for AE?

    jasondrey13

      Hey,

       

      I'm trying to create a script that will allow me to set a certain number of layer markers at an even interval to mark out a song. Could someone help me troubleshoot this script? I've been working on it for ages now, and I'm about to snap. I've basically gone from HTML and CSS, to javascript and Adobe scripting in the past few hours, and I cannot figure this out for the life of me.

       

      I want to create a dialog with two fields, the number of markers to place, and the tempo of the song. Tempo is pretty simple, its just the number of beats per minute. The script is meant to start at a marker that I have already placed, and set a new marker at incrementing times. Its mainly to help me see what I'm trying to animate too, even if the beat is a little hard to pick up every once in a while.

       

      Also, is there a better way to do this? This script will help me in the long run because I will need to do this pretty often, but I'm wondering if there was something that I could not find online that would have saved me these hours of brain-jumbling?

       

       

      Thank you very much for any help you can offer.




          {
          // Neo_Add_MultiMarkers.jsx
          //jasondrey13
          //2009-10-26
          //Adds multiple markers to the selected layer.
          // This script prompts for a certain number of layer markers to add to the selected audio layer,
          //and an optional "frames between" to set the number of frames that should be skipped before placing the next marker
          //
          // It presents a UI with two text entry areas: a Markers box for the
          // number of markers to add and a Frames Between box for the number of frames to space between added markers.
          //
          // When the user clicks the Add Markers button,
         
         
          //
          // A button labeled "?" provides a brief explanation.

       


          function Neo_Add_MultiMarkers(thisObj)
          {
              // set vars
             
              var scriptName = "Neoarx: Add Multiple Markers";
              var numberOfMarkers = "0";
              var tempo = "0";

       

       

       

       

       

              // This function is called when the Find All button is clicked.
              // It changes which layers are selected by deselecting layers that are not text layers
              // or do not contain the Find Text string. Only text layers containing the Find Text string
              // will remain selected.
              function onAddMarkers()
              {
                 
                 

       

                  // Start an undo group.  By using this with an endUndoGroup(), you
                  // allow users to undo the whole script with one undo operation.
                  app.beginUndoGroup("Add Multiple Markers");

       

                  // Get the active composition.
                  var activeItem = app.project.activeItem;
                  if (activeItem != null && (activeItem instanceof CompItem)){
                     
                      // Check each selected layer in the active composition.
                      var activeComp = activeItem;
                      var layers = activeComp.selectedLayers;
                      var markers = layers.property("marker");
                     
                      //parse ints
                      numberOfMarkers = parseInt (numberOfMarkers);
                      tempo = parseInt (tempo);
                      // Show a message and return if there is no value specified in the Markers box.
                      if (numberOfMarkers < 1 || tempo < 1) {
                      alert("Each box can take only positive values over 1. The selection was not changed.", scriptName);
                      return;
                      }
                      if (markers.numKeys < 1)
                      {
                      alert('Please set a marker where you would like me to begin.');
                      return;
                      }
                     
                     
                      var beginTime = markers.keyTime( 1 );
                     
                      var count = 1;
                      var currentTime = beginTime;
                      var addPer = tempo/60;
                     
                      while(numberOfMarkers < count)
                      {
                      markers.setValueAtTime(currentTime, MarkerValue(count));
                      currentTime = currentTime + addPer;
                      if (count==numberOfMarkers) {
                          alert('finished!');
                          return;
                          }
                      else{
                          count++;
                          }
                      }
                     
                     
                     
                     
                  }
                  app.endUndoGroup();
              }

       

             

       

         
         
         
          // Called when the Markers Text string is edited
              function onMarkersStringChanged()
              {
                  numberOfMarkers = this.text;
              }

       

              // Called when the Frames Text string is edited
              function onFramesStringChanged()
              {
                  tempo = this.text;
              }

       

              // Called when the "?" button is clicked
              function onShowHelp()
              {
                  alert(scriptName + ":\n" +
                  "This script displays a palette with controls for adding a given number of markers starting at a pre-placed marker, each separated by an amount of time determined from the inputted Beats Per Minute (Tempo).\n" +
                  "It is designed to mark out the even beat of a song for easier editing.\n" +
                  "\n" +
                  "Type the number of Markers you would like to add to the currently selected layer. Type the tempo of your song (beats per minute).\n" +           
                  "\n" +
                  "Note: This version of the script requires After Effects CS3 or later. It can be used as a dockable panel by placing the script in a ScriptUI Panels subfolder of the Scripts folder, and then choosing this script from the Window menu.\n", scriptName);
              }
             
             
              // main:
              //
             
              if (parseFloat(app.version) < 8)
              {
                  alert("This script requires After Effects CS3 or later.", scriptName);
                  return;
              }
              else
              {
                  // Create and show a floating palette
                  var my_palette = (thisObj instanceof Panel) ? thisObj : new Window("palette", scriptName, undefined, {resizeable:true});
                  if (my_palette != null)
                  {
                      var res =
                      "group { \
                          orientation:'column', alignment:['fill','fill'], alignChildren:['left','top'], spacing:5, margins:[0,0,0,0], \
                          markersRow: Group { \
                              alignment:['fill','top'], \
                              markersStr: StaticText { text:'Markers:', alignment:['left','center'] }, \
                              markersEditText: EditText { text:'0', characters:10, alignment:['fill','center'] }, \
                          }, \
                          FramesRow: Group { \
                              alignment:['fill','top'], \
                              FramesStr: StaticText { text:'Tempo:', alignment:['left','center'] }, \
                              FramesEditText: EditText { text:'140', characters:10, alignment:['fill','center'] }, \
                          }, \
                          cmds: Group { \
                              alignment:['fill','top'], \
                              addMarkersButton: Button { text:'Add Markers', alignment:['fill','center'] }, \
                              helpButton: Button { text:'?', alignment:['right','center'], preferredSize:[25,20] }, \
                          }, \
                      }";
                     
                      my_palette.margins = [10,10,10,10];
                      my_palette.grp = my_palette.add(res);
                     
                      // Workaround to ensure the editext text color is black, even at darker UI brightness levels
                      var winGfx = my_palette.graphics;
                      var darkColorBrush = winGfx.newPen(winGfx.BrushType.SOLID_COLOR, [0,0,0], 1);
                      my_palette.grp.markersRow.markersEditText.graphics.foregroundColor = darkColorBrush;
                      my_palette.grp.FramesRow.FramesEditText.graphics.foregroundColor = darkColorBrush;
                     
                      my_palette.grp.markersRow.markersStr.preferredSize.width = my_palette.grp.FramesRow.FramesStr.preferredSize.width;
                     
                      my_palette.grp.markersRow.markersEditText.onChange = my_palette.grp.markersRow.markersEditText.onChanging = onMarkersStringChanged;
                      my_palette.grp.FramesRow.FramesEditText.onChange = my_palette.grp.FramesRow.FramesEditText.onChanging = onFramesStringChanged;
                     
                      my_palette.grp.cmds.addMarkersButton.onClick    = onAddMarkers;
                      my_palette.grp.cmds.helpButton.onClick    = onShowHelp;
                     
                      my_palette.layout.layout(true);
                      my_palette.layout.resize();
                      my_palette.onResizing = my_palette.onResize = function () {this.layout.resize();}
                 
                      if (my_palette instanceof Window) {
                          my_palette.center();
                          my_palette.show();
                      } else {
                          my_palette.layout.layout(true);
                      }
                  }
                  else {
                      alert("Could not open the user interface.", scriptName);
                  }
              }
          }
         
         
          Neo_Add_MultiMarkers(this);
      }