Skip to main content
Participant
January 14, 2015
Answered

Please help - Rewriting script to affect all files in folder, not just one (Photoshop, JavaScript)

  • January 14, 2015
  • 4 replies
  • 787 views

Hey guys ,

I dont know much about scripting, but i somehow managed to put together a script, that takes *.ps (postscript) file, open it in photoshop with some settings, and then save it in two different sizes to two different folders as jpeg. But unfortunately, it only affects one *.ps file (in a specified path). I have hundreds of those *.ps files and just thinking about opening it one by one makes me feel dizzy Is there any way i could select folder with all those *.ps files and script would make those changes to all of them?

I would really appreciate any help .

Here's my script:

//open eps with settings 1050x750px+rgb

var epsOpts = new EPSOpenOptions();

epsOpts.antiAlias = true;

epsOpts.mode = OpenDocumentMode.RGB;

epsOpts.resolution = 72;

epsOpts.constrainProportions = true;

epsOpts.width = new UnitValue( 1050, 'px' )

var f = new File('C:/mypath/myfile.ps' );

app.open( f, epsOpts );

//save 1050px

var doc = app.activeDocument; 

var Path = 'C:/mypath/resize/1050/'; 

var Name = doc.name.replace(/\.[^\.]+$/, '');  

var saveFile = File(Path + "/" + Name + ".jpg"); 

SaveJPEG(saveFile, 12); 

 

function SaveJPEG(saveFile, jpegQuality){ 

jpgSaveOptions = new JPEGSaveOptions(); 

jpgSaveOptions.embedColorProfile = true; 

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

jpgSaveOptions.matte = MatteType.NONE; 

jpgSaveOptions.quality = jpegQuality; 

activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

//resize to 360px

app.activeDocument.resizeImage('360 pixels', '257 pixels', 72, ResampleMethod.BICUBIC);

//save 360px

var doc360 = app.activeDocument; 

var Path360 = 'C:/mypath/resize/360/'; 

var Name360 = doc.name.replace(/\.[^\.]+$/, '');  

var saveFile360 = File(Path360 + "/" + Name360 + ".jpg"); 

SaveJPEG360(saveFile360, 12); 

 

function SaveJPEG360(saveFile360, jpegQuality){ 

jpgSaveOptions = new JPEGSaveOptions(); 

jpgSaveOptions.embedColorProfile = true; 

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

jpgSaveOptions.matte = MatteType.NONE; 

jpgSaveOptions.quality = jpegQuality; 

activeDocument.saveAs(saveFile360, jpgSaveOptions, true,Extension.LOWERCASE); 

}

//close file

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

Thank you in advance !

This topic has been closed for replies.
Correct answer c.pfaffenbichler

This would present an alert for each file of the defined formats in the selected folder.

You can naturally insert the code you need in the for-clause.

var theFolder = Folder.selectDialog ("select folder");

if (theFolder) {

var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|ps)$/i);

for (var m = 0; m < theFiles.length; m++) {

  alert (theFiles)

  };

};

4 replies

MatuskovaAuthor
Participant
January 15, 2015

Thank you SO MUCH guys I finally managed to run script on all files in my folder (which I select through the dialogue).

Heres mi final script:

//select folder

var inFolder = Folder.selectDialog("Select folder");

if(inFolder != null){

var fileList = inFolder.getFiles(/\.(ps|)$/i);

}

for(var a = 0 ;a < fileList.length; a++)

{

var docRef = open(fileList,epsOpts);

//open eps with settings 1050x750px+rgb

var epsOpts = new EPSOpenOptions();

epsOpts.antiAlias = true;

epsOpts.mode = OpenDocumentMode.RGB;

epsOpts.resolution = 72;

epsOpts.constrainProportions = true;

epsOpts.width = new UnitValue( 1050, 'px' )

//save 1050px

var doc = app.activeDocument; 

var Path = 'x:/mypath/1050/'; 

var Name = doc.name.replace(/\.[^\.]+$/, '');  

var saveFile = File(Path + "/" + Name + ".jpg"); 

SaveJPEG(saveFile, 12); 

 

function SaveJPEG(saveFile, jpegQuality){ 

jpgSaveOptions = new JPEGSaveOptions(); 

jpgSaveOptions.embedColorProfile = true; 

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

jpgSaveOptions.matte = MatteType.NONE; 

jpgSaveOptions.quality = jpegQuality; 

activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

//resize to 360px

app.activeDocument.resizeImage('360 pixels', '257 pixels', 72, ResampleMethod.BICUBIC);

//save 360px

var doc360 = app.activeDocument; 

var Path360 = 'x:/mypath/360/'; 

var Name360 = doc.name.replace(/\.[^\.]+$/, '');  

var saveFile360 = File(Path360 + "/" + Name360 + ".jpg"); 

SaveJPEG360(saveFile360, 12); 

 

function SaveJPEG360(saveFile360, jpegQuality){ 

jpgSaveOptions = new JPEGSaveOptions(); 

jpgSaveOptions.embedColorProfile = true; 

jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

jpgSaveOptions.matte = MatteType.NONE; 

jpgSaveOptions.quality = jpegQuality; 

activeDocument.saveAs(saveFile360, jpgSaveOptions, true,Extension.LOWERCASE); 

}

//close file

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

Legend
January 14, 2015

Why not include it in an action and then Batch process the folder?

Chuck Uebele
Community Expert
Community Expert
January 14, 2015

You can go two routes with this. You can create a script that resizes and saves one file to the two different folders, then run that script using the batch command - most likely the easiest, as you don't need all that open stuff in your script, as the batch command takes care of that. You just need to size your file and have it rename in the save command.

The second way is to get put the files into an array, then loop through that array and open all the files. The code for getting the files would be something like:

var sourceFolder = new Folder('/c/source/');

var searchMask = '*.eps';

var fileList = sourceFolder.getFiles(searchMask);

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
January 14, 2015

This would present an alert for each file of the defined formats in the selected folder.

You can naturally insert the code you need in the for-clause.

var theFolder = Folder.selectDialog ("select folder");

if (theFolder) {

var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd|ps)$/i);

for (var m = 0; m < theFiles.length; m++) {

  alert (theFiles)

  };

};