Wait for saveFrameToPng() to create the file.

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
Andrei Popa
Posts: 4
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);
      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";
    var content = file.read();
    return content;
Posts: 1
Joined: March 26th, 2021, 5:02 pm

This is a fairly old post but I've come up with a bit of a solution and figured I'd share:

After you call saveFrameToPng you can add this:

Code: Select all

while (!myTempFile.exists) {

Maybe a little hackish, but it basically forces extendscript to wait until that file exists on your drive, and only then continue

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

Thanks for this solution.
It works great, I just had to lower a bit the waiting time because I have a high amount of pics to process.

Posts: 1
Joined: May 17th, 2021, 12:46 am

Thanks for helping me this also worked for me GIMP download Mac

Post Reply