Wait for saveFrameToPng() to create the file.

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Andrei Popa
Posts: 3
Joined: October 3rd, 2017, 5:02 am

Hello everyone. I am trying to make a direct composition to png string. I thought about saving the comp to a png, then open png, turn it into ScriptUI usable code(with .toSource()) and then delete the png. The problem is that the script tries to do all these things before the png is created. Can I wait for saveFrameToPng() to create the file? I saw it has a .onCOmplete() part, but I am not sure how to use that. Here is my code.

Code: Select all

/**
* Returns the string that can be used to draw the picture in the ScriptUI
* 
* @param {comp:compItem,time:number} options 
*/
function createImageString(options) {

  //Setting defaults
  options = options || {};
  options.comp = options.comp || app.project.activeItem;
  options.time = options.time || options.comp.time;

  var jsxStringForPng = pngToString(options.comp, options.time);
  return jsxStringForPng;

  function pngToString(comp, time) {
    if (app.project.file) {
      var myTempFile = new File(app.project.file.path + "/" + comp.name + ".png");
      var a = comp.saveFrameToPng(time, myTempFile);
     // wait()
      var myString = pngToJsx(myTempFile);
      myTempFile.remove();
      return myString;
    } else {
      alert("Please save the project before using this function");
      return null;
    }

  }

  function pngToJsx(myFile) {
    if (!myFile) return;
    var rawData = readBinaryFile(myFile);
    return rawData.toSource().slice(13, -3);
  }


  function readBinaryFile(file) {
    file.encoding = "BINARY";
    file.open("r");
    var content = file.read();
    file.close();
    return content;
  }
}
Post Reply