Skip to main content
Participant
October 30, 2024
Answered

Script to select all files in a given folder and save with each file name separately

  • October 30, 2024
  • 1 reply
  • 202 views

Hello,

 

Since the past four days I was trying to build some script that can help me automate 700+ files that I want to create mockups for. The mockup file I have requires to open the smart object, therefore simple "replace smart object" macros failed. Using multiple resources I came up with below macro - it works flawlessly, but for one file only. I would need to specify a folder to pick up the file to be placed embeded, and then save it under the filename of each file used for placing. I tried adding this script modified to my script, but I kept gettings errors: https://community.adobe.com/t5/framemaker-discussions/loop-through-all-mif-files-in-a-given-folder/m-p/10581749#M62305

 

Here is my script:

var myDocument = app.activeDocument;
    var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
    var thePath = myDocument.path;
    var theLayer = myDocument.activeLayer;

// JPG Options;
    jpgSaveOptions = new JPEGSaveOptions();  
    jpgSaveOptions.embedColorProfile = true;  
    jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
    jpgSaveOptions.matte = MatteType.NONE;  
    jpgSaveOptions.quality = 8;   

//path to the file
var filePath = new File('D:/Creative/Circuits/To print/50x70/Border/Light/50x70_Barcelona_FullColor_Light_Light (Chicane)_border.png');

//name of the saved file
var theNewName = filePath.name.match(/(.*)\.[^\.]+$/)[1];

function placeEmbeded(filePath) {


    var actDesc = new ActionDescriptor();


    actDesc.putPath(charIDToTypeID('null'), filePath);


    actDesc.putEnumerated(charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa'));


    executeAction(charIDToTypeID('Plc '), actDesc, DialogModes.NO);
};

////// open smart object //////
openSmartObject (app.activeDocument.activeLayer);


function openSmartObject (theLayer) {
    current = app.activeDocument;
    if (theLayer.kind == "LayerKind.SMARTOBJECT") runMenuItem(stringIDToTypeID('placedLayerEditContents'));
//place the file
    placeEmbeded(filePath);

    if ( current == app.activeDocument) {
        //alert("Did mot open all the way");
        try {         
            var r = new ActionReference();     
            r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObject"));  
            r.putIdentifier(stringIDToTypeID("layer"), theLayer.id);  
            var name = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObject")).getString(stringIDToTypeID("fileReference"));         
            }  
        catch (e) { throw theLayer + " Smart Object Did not Open"; }  
        if ($.os.search(/windows/i) != -1)  var workFile = new File( "~/AppData/Local/Temp/" +  name);  // Windows user ID system temp space
        else var workFile = new File( "~/AppData/Local/Temp/" +  name);  // should be in Mac users temp space I would think
        if (workFile.exists)a pp.open(File(workFile));
        if ( current == app.activeDocument) {   
            throw theLayer + " Smart Object Did not Open";
            }
    }
return app.activeDocument;
};

//save and close the smartobject
app.activeDocument.save();
app.activeDocument.close();

//save the file
myDocument.saveAs((new File(thePath + "/" + theName + "_" + theNewName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
This topic has been closed for replies.
Correct answer Stephen Marsh

You can find some example code in the following topic:

 

 
Key concepts...
 
A nominated folder containing files:
 
var inputFolder = Folder.selectDialog('Please select the input folder:');
 
Getting a list of file objects from the folder object:
 
var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif)$/i);

 

Using a For loop to iterate over the files, placing them one by one:

 

for (var i = 0; i < inputFiles.length; i++) {
    placeFile(new File(inputFiles[i]), false, 0, 0);
}

 

Hope this helps!

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
October 30, 2024

You can find some example code in the following topic:

 

 
Key concepts...
 
A nominated folder containing files:
 
var inputFolder = Folder.selectDialog('Please select the input folder:');
 
Getting a list of file objects from the folder object:
 
var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|png|psd|psb|gif)$/i);

 

Using a For loop to iterate over the files, placing them one by one:

 

for (var i = 0; i < inputFiles.length; i++) {
    placeFile(new File(inputFiles[i]), false, 0, 0);
}

 

Hope this helps!

Participant
October 30, 2024

You're a hero, thank you!

For anyone else finding this thread wondering how to save the files, I'm dropping the code below:

 

var newName = inputFiles[i].name.slice(0, -4); // Remove the file extension (.png)
myDocument.saveAs((new File(thePath + "/" + theName + "_" + newName + ".jpg")), jpgSaveOptions, true,Extension.LOWERCASE);
}