8

This seems like it would be a very easy problem to solve, but I've been banging my head against it for almost an hour. All I need is a snippet of javascript/extendscript code so that my InDesign CS6 script can create a folder. I know the existing folder in which the new one should be created, and I know the name that this new folder should be called. But how do I get javascript to do it?

By the way, all searches online for the folderObj.create() method, which is in the JavaScript Tools Guide, prove useless. I've tried several variations on that method, but nothing seems to actually create the folder. What am I missing?

Sturm
  • 689
  • 2
  • 23
  • 52

3 Answers3

19
    var f = new Folder('/c/myfolder/');
    if (!f.exists)
        f.create();
Anna Forrest
  • 1,711
  • 14
  • 21
7

Okay, found a work-around: I have to specify the folder absolutely, rather than use the ~ home shortcut. In addition, I have use /Volumes at the very beginning. Thus, the code becomes:

var f = new Folder("/Volumes/apache HD/Users/apache/Desktop/my_new_fodler");  
f.create();

And that seems to work, finally. Thanks for your help, @Anna Forrest and @fabiantheblind! (You seem to be the resident ExtendScript expert around here.)

Sturm
  • 689
  • 2
  • 23
  • 52
  • SO how about addFolder(); method?What does it do?? – Amal lal T L Mar 23 '18 at 09:44
  • From a cursory search, it seems `addFolder()` is a method that's only defined for After Effects, not any other Adobe program. It appears to create folders in an After Effects project bin for holding project assets, which appears to be a separate entity from actually creating a _directory_ on the OS' file system. I could be wrong, as I don't really use AE much, let alone script for it. I primarily use Illustrator and InDesign in my daily work. For them, an actual ExtendScript `Folder` object must be created first, then written to the file system as a directory with `.create()`. – Sturm Apr 16 '18 at 12:44
  • I forgot to wrap my folder creation code in a function. Make sure it is wrapped in a function createFolder() { ... Put here ...}; createFolder(); and executed to successfully run. – Archie Butler May 12 '22 at 14:33
4

try this:

var f = new Folder("~/Desktop/my_new_fodler");  
f.create();
fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
  • No go. Just tried copy/pasting that into ExtendScript Toolkit, targeted InDesign, and ran it; nothing happens. Well, the script seems to run without errors, but no folder is created on the desktop. – Sturm Aug 20 '13 at 16:44
  • Talk about confusing and making no sense… After logging in this morning and giving it a try, your code works. _However_, I then connected to our two servers, then ran the code again, and nothing happened. What gives? – Sturm Aug 21 '13 at 12:41
  • I have run into odd behaviour if the parent folder doesn't exist or can't be found anyway. So in this case the use of the ~ may not be mapping to quite the location you are expecting and hence ~/Desktop is not found? I find it is always safer to use the full path – Anna Forrest Sep 30 '13 at 13:02
  • Yeah, I've pretty much figured out now that path String matters greatly. I haven't actually nailed it down completely, as I'm still using trial and error, but I've determined that the more "absolute" the path, the more likely that the folder will be created. I hope that makes some sense. – Sturm Feb 24 '14 at 18:47