ExtendScript Working with Files

<- Previous article: Extendscript Hello World !!!





Hello.





In a previous article, I described how to write a script for Adobe After Effects. Let me remind you that our script creates a window in the program interface. In this window, you can enter and execute Extendscript, a scripting language for Adobe programs. Now I want to offer you some expansion of the functionality of this plugin. Let's make it so that it can load and save the scripts we have written.





And so, let's go.





Let's start by loading the script. To do this, add the Open button and a handler for pressing it.





var btnOpenFile = win.add('button', undefined, 'open');
btnOpenFile.onClick = function() {
   var file = File.openDialog('   Adobe AE');

   if (!file) return;

   if (/\.(jsx|js)$/.test(file.name)) {
       file.open("r");                 
       editText.text = file.read();
       file.close();
   } else {
       alert(" " + file.name + "    .js  .jsx")
   }
};
      
      



First of all, in the handler, we call the dialog box to open the file. The first argument to the static method File.openDialog is a prompt for the user. If the system supports such a prompt, it will be displayed. Otherwise, this argument will be ignored.





This method also accepts a second argument - a method or a string (depending on the OS) for filtering the extension of opened files. But for some reason, in recent versions of AE, this argument does not work, so I did not use it in this script. Instead, we'll make the appropriate check below.





, File.openDialog File. , , null.





if (!file) return;
      
      



. , .





/\.(jsx|js)$/.test(file.name)
      
      



.js .jsx. AE . , , open “r” - read, , .





file.open("r");                 
editText.text = file.read();
file.close();
      
      



, , .





alert(" " + file.name + "    .js  .jsx")
      
      



. , .





. Save.





var btnSaveFile = win.add('button', undefined, 'save');
btnSaveFile.onClick = function() {
   var file = File.saveDialog('   Adobe AE');

   if (!file) return;

   if (!/\.(jsx|js)$/.test(file.name)) {
       file.rename(file.name + '.jsx')
   }

   file.open('w');
   file.write(editText.text);
   file.close();
};
      
      



, . .





var file = File.saveDialog('   Adobe AE');
      
      



File.saveDialog   null    





if (!file) return;
      
      



, .js .jsx, ,





if (!/\.(jsx|js)$/.test(file.name)) {
   file.rename(file.name + '.jsx')
}
      
      



, rename.





. , . , . , After Effects . , Preferences > Scripting & Expressions, Allow Scripts to Write Files and Access Network. , , .





Well, that's all. There is only one caveat left, our buttons are located under each other, which is not very aesthetically pleasing and convenient. To fix this, we add a group element before adding the buttons.





var btnsGroup = win.add('group');
      
      



And the buttons themselves will no longer be added to the window, but to this group





var btnRun = btnsGroup.add('button', undefined, 'run');
var btnOpenFile = btnsGroup.add('button', undefined, 'open');
var btnSaveFile = btnsGroup.add('button', undefined, 'save');
      
      



Now the buttons are rendered inline, which, you see, looks much better.





That's all for now. I hope this example was helpful to you. You can find the complete script with detailed comments here . See the documentation on working with files in AE Extendscript here .





Thank you for attention.





<- Previous article: Extendscript Hello World !!!








All Articles