Script For Opening Next Sequential File From Folder
I am currently working on an action that takes an opened jpg file, creates a new document, and pastes that file into the document. The action will then use a script to open the next sequential jpg file and paste that file into the created document. It will then do this recursively for the next 7 files, ultimately having 9 files loaded into that document.
I found a script that takes a file (called "00000.jpg") and will open the next file ("00001.jpg") in photoshop. This script opens the next file perfectly, but my problem is that I would have to batch rename all of my jpg files to follow this naming rule. For instance, I'm creating this action to use for thousands of files. These files are separated into their own respective folders and the folders could have anywhere from 30-500 pictures in them. These folders have different starting file names and one could be name "image5352.jpg." I need a script that can take any file, using this name rule, and open the next sequential file (in this case "image5353.jpg") into photoshop.
This is the script that I found that opens the next file using the "00000.jpg" name rule:
var folderPath = activeDocument.path
var filePieces = activeDocument.name.split('.');
var fileName = filePieces[0];
var fileExtension = filePieces[1];
// We need to unpad the string for some reason, otherwise PS-JS
// thinks it's a past layer some times when using parseInt.
fileNameStart = 0;
for (var i = 0; i < fileName.length - 1; i++) {
digit = fileName[i];
if (digit == '0') {
fileNameStart++;
} else {
break
};
}
fileNameUnpadded = fileName.slice(fileNameStart);
var nextFileNumber = parseInt(fileNameUnpadded) + 1;
// Yes it's haxxy I know...
var padTemp = ("000000000000000" + nextFileNumber.toString());
var nextFileName = padTemp.slice(padTemp.length - 5);
var nextFilePath = folderPath + '/' + nextFileName + '.' + fileExtension;
activeDocument.close(SaveOptions.SAVECHANGES);
try {
var docRef = open(new File(nextFilePath));
}
catch (e) {
alert("No more files in the directory!");
}
Thank you for the help
