Posted: Sun Jul 11, 2010 6:18 pm Post subject: Batch import specific images from nested folders
Hi,
I have an action that is complete all but the last stage which I am having trouble with:
I would like to batch import all files called 'folder.jpg' within a nested file structure of about 1000 folders. I need to be able to import each folder.jpg seperately to the stage, resize, place at specific x,y co-ords. Then save and close, overwriting the original then move on to the next image. I suspect I am going to have a problem because my file structure has more than one image in each sub folder and I only want to import the 'folder.jpg' i.e.
Posted: Wed Jul 14, 2010 11:38 am Post subject: Re: Batch import specific images from nested folders
I don't understand exactly what you are trying to do, but if you can do all that you want in an action except opening and saving, the following code should be fit for purpose.
NB: you will have to amend the doAction line to suit!
PLease do a test first to make sure all works as it should!!!!!!
Code:
main();
function main(){
var folderList=[];
var topLevelFolder = Folder.selectDialog("Please select top level folder");
if (topLevelFolder == null) return;
getFolderList(topLevelFolder);
folderList.unshift(topLevelFolder);
var FileList =[];
for (var a in folderList){
var file = File(folderList[a]+"/folder.jpg");
if(file.exists) FileList.push(file);
}
for(var f in FileList){
open(FileList[f]);
// Amend to suit your action , the fields are case sensitive!
doAction("ActionName","ActionSet");
overWriteJPG();
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
alert(FileList.length + " files have been modified");
function getFolderList(folder) {
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof Folder) {
folderList.push(file);
getFolderList(file);
}
}
};
}
function overWriteJPG(){
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 8;
var saveFile = new File(decodeURI(activeDocument.fullName.fsName));
activeDocument.saveAs(saveFile,saveOptions);
}
I will explain what I am attempting to do. I have some external hardrives with my DVD's ripped on to it. Each movie is in its own subfolder with the following files:
Movie.avi
folder.jpg (front cover of DVD)
backdrop.jpg (background picture)
2 or more folder.jpg's in this tree, one for the series Dexter itself, and one specifically for season(s) 1/2/3/4
My folder.jpg are just plain images. I have some rendering actions which will overlay them onto a nice DVD box. The rendering action is broken down into 2 seperate actions. Action 1 creates the template from which to import the folder.jpg then i need to resize it and place at co-rds x,y Then I need to run Action 2 on the template and once the action is finished save the new file as folder.jpg but backup the original as folderbkp.jpg, both images being saved/renamed in the folder/subfolder from where they originated. (sorry I didnt mention the backup last time). Then I need to move on to the next image in the next folder/subfolder.
I hope this clarifies more what I am attempting to do.
Thank you for the coding. Would you be able to tell me what i need to save the code as (which extension .js .txt) and where do i put it in photoshop. I have only just started to learn PS and coding isn't something that has come up for me yet.
Copy and paste the code into a text editor or better still ExtendScript toolkit this is installed at the same time as Photoshop and can be found here:-
PC: C:\Program Files\Adobe\Adobe Utilities
MAC: <hard drive>/Applications/Utilities/Adobe Utilities
From Photoshop CS2 onwards scripts are saved with an extention of ".jsx" this automatically done when using ExtendScript Toolkit.
The script should be placed into:
PC: C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts
MAC: <hard drive>/Applications/Adobe Photoshop CS#/ Presets/Scripts
N.B. If you are using Vista or Windows 7 you can not write the file directly into the folder, you will have to save the script somewhere else then copy it to the correct folder.
If Photoshop was open, close and restart so that it can pick up the new script.
To run the script:- File - Scripts - select the script.
You will need to amend the script for the actionname and actionset, you can duplicate this line as many times as you want so you could run as many actions as you require.
Make sure you do a test first to make sure your actions are doing what you want!
All the best and good luck.
Code:
/****************************************
This script will open each folder.jpg in turn
backup the original, run the action(s) ,
save and close the documents
****************************************/
main();
function main(){
var folderList=[];
var topLevelFolder = Folder.selectDialog("Please select top level folder");
if (topLevelFolder == null) return;
//Get a list of all sub folders and put them in an array
getFolderList(topLevelFolder);
folderList.unshift(topLevelFolder); //add selected folder to array
var FileList =[];
//get a complete list of folder.jpg's
for (var a in folderList){
var file = File(folderList[a]+"/folder.jpg");
if(file.exists) FileList.push(file);
}
//process all folder.jpgs
for(var f in FileList){
open(FileList[f]); //open each file in turn
FileList[f].copy(FileList[f].path +"/folderbkp.jpg"); //backup file
// Amend to suit your action , the fields are case sensitive!
//This runs an action of your choice you can duplicate this line so that you can run another action.
doAction("ActionName","ActionSet"); //action to run
overWriteJPG(); //write the new file
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close the document
}
alert(FileList.length + " files have been modified");
function getFolderList(folder) { //recursive function
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof Folder) {
folderList.push(file);
getFolderList(file);
}
}
};
}
function overWriteJPG(){
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 8;
var saveFile = new File(decodeURI(activeDocument.fullName.fsName));
activeDocument.saveAs(saveFile,saveOptions);
}
Copy and paste the code into a text editor or better still ExtendScript toolkit this is installed at the same time as Photoshop and can be found here:-
PC: C:\Program Files\Adobe\Adobe Utilities
MAC: <hard drive>/Applications/Utilities/Adobe Utilities
From Photoshop CS2 onwards scripts are saved with an extention of ".jsx" this automatically done when using ExtendScript Toolkit.
The script should be placed into:
PC: C:\Program Files\Adobe\Adobe Photoshop CS#\Presets\Scripts
MAC: <hard drive>/Applications/Adobe Photoshop CS#/ Presets/Scripts
N.B. If you are using Vista or Windows 7 you can not write the file directly into the folder, you will have to save the script somewhere else then copy it to the correct folder.
If Photoshop was open, close and restart so that it can pick up the new script.
To run the script:- File - Scripts - select the script.
You will need to amend the script for the actionname and actionset, you can duplicate this line as many times as you want so you could run as many actions as you require.
Make sure you do a test first to make sure your actions are doing what you want!
All the best and good luck.
Code:
/****************************************
This script will open each folder.jpg in turn
backup the original, run the action(s) ,
save and close the documents
****************************************/
main();
function main(){
var folderList=[];
var topLevelFolder = Folder.selectDialog("Please select top level folder");
if (topLevelFolder == null) return;
//Get a list of all sub folders and put them in an array
getFolderList(topLevelFolder);
folderList.unshift(topLevelFolder); //add selected folder to array
var FileList =[];
//get a complete list of folder.jpg's
for (var a in folderList){
var file = File(folderList[a]+"/folder.jpg");
if(file.exists) FileList.push(file);
}
//process all folder.jpgs
for(var f in FileList){
open(FileList[f]); //open each file in turn
FileList[f].copy(FileList[f].path +"/folderbkp.jpg"); //backup file
// Amend to suit your action , the fields are case sensitive!
//This runs an action of your choice you can duplicate this line so that you can run another action.
doAction("ActionName","ActionSet"); //action to run
overWriteJPG(); //write the new file
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close the document
}
alert(FileList.length + " files have been modified");
function getFolderList(folder) { //recursive function
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof Folder) {
folderList.push(file);
getFolderList(file);
}
}
};
}
function overWriteJPG(){
saveOptions = new JPEGSaveOptions();
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
saveOptions.quality = 8;
var saveFile = new File(decodeURI(activeDocument.fullName.fsName));
activeDocument.saveAs(saveFile,saveOptions);
}
I have the script saved and running in PS but I am having a couple of difficulties.
I need to copy the contents of the first opened document (folder.jpg) and paste them onto the stage after the first action is performed and made the template. There is only 1 layer in the opened folder.jpg called 'Background' so I have been trying this:
Code:
for(var f in FileList){
open(FileList[f]); //open each file in turn
FileList[f].copy(FileList[f].path +"/folderbkp.jpg"); //backup file
// Amend to suit your action , the fields are case sensitive!
//This runs an action of your choice you can duplicate this line so that you can run another action.
app.activeDocument.artLayers.getByName("Background").copy();
doAction("Step 1 :: Create","DVD"); //action to run
app.activeDocument.artLayers.paste()
//end of actions
overWriteJPG(); //write the new file
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close the document
}
The action 'DVD' 'Step1 :: Create' runs sucessfully and creates the template but my selection from folder.jpg is not being copied and pasted onto the newly formed template? I am assuming I have added the wrong lines of code?
Ok, I have modified the code so that it opens folder.jpg, backs up the file and removes the original. From there it runs your action after the action it will then move folder.jpg into your new document and close folder.jpg
At this stage you might want to run another action to move the layer to where you want it?
It will then save the new document as folder.jpg and move onto the next.
Code:
main();
function main(){
var folderList=[];
var topLevelFolder = Folder.selectDialog("Please select top level folder");
if (topLevelFolder == null) return;
//Get a list of all sub folders and put them in an array
getFolderList(topLevelFolder);
folderList.unshift(topLevelFolder); //add selected folder to array
var FileList =[];
//get a complete list of folder.jpg's
for (var a in folderList){
var file = File(folderList[a]+"/folder.jpg");
if(file.exists) FileList.push(file);
}
//process all folder.jpgs
for(var f in FileList){
open(FileList[f]); //open each file in turn
FileList[f].copy(FileList[f].path +"/folderbkp.jpg"); //backup file
var saveFile = new File(decodeURI(FileList[f].path)+"/folder.jpg");
if(File(FileList[f].path +"/folderbkp.jpg").exists) FileList[f].remove();
// Amend to suit your action , the fields are case sensitive!
//This runs an action of your choice you can duplicate this line so that you can run another action.
doAction("Step 1 :: Create","DVD"); //action to run
//these lines will put the folder.jpg into the center of your new document
var folderJPG = app.documents[0];
activeDocument = folderJPG;
folderJPG.activeLayer.duplicate(documents[1]);
//close folder.jpg only the new document should now exist.
folderJPG.close(SaveOptions.DONOTSAVECHANGES);
//You might want to move the layer to where you want with another action here?
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
SaveJPEG(saveFile, 8); //write the new file
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); //close the document
}
alert(FileList.length + " files have been modified");
function getFolderList(folder) { //recursive function
var fileList = folder.getFiles()
for (var i = 0; i < fileList.length; i++) {
var file = fileList[i];
if (file instanceof Folder) {
folderList.push(file);
getFolderList(file);
}
}
};
}
function SaveJPEG(saveFile, jpegQuality){
activeDocument.flatten();
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}
My problem now is that i need to transform the image on folder.jpg from size 749x1064 to 1529x2155 whilst keeping it centered on the stage. Been trying:
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum