Highlighted

Batch Closes First Opened Document After Completed Action & Script

New Here ,
Oct 30, 2020

Copy link to clipboard

Copied

I am currently running into an issue that I think is a bug. I have an action that takes a string of images and creates a 3x3 collage out of the string of 9 images. I found a script online that allows me to do this, and that script is called OpenNextFile. It takes a file called "00000.jpg" and opens the next file "00001.jpg" in photoshop. Each iteration closes the document that executed that script, so "00000.jpg" was closed. Using this script, I can create a collage by opening each file in the sequence, copying the layer, and pasting it to a created document.

This action performs what I want it to when I take an image into photoshop and run the action in the actions menu. When I run into trouble is when I batch process this action. I tried to load up every 10th image in photoshop and run a batch using "opened files" as a source. The first iteration of the batch performs perfectly. This iteration starts with the last document that is opened, so "00037.jpg" if I want to batch 5 images. My bug occurs in the next iteration of the batch. For some reason, in each new iteration of the batch process, photoshop will go to the first document that is oepned, so "00000.jpg" and it closes this file. Then it goes to the next image in the sequence, "00028.jpg" and runs the action on this image. Once this action is finished, photoshop will go to the first image, "00010.jpg" now, and closes this file. Because of this, the batch only processes half the files that I want it to. I haven't ran into this issue yet and I would love to know what is causing this. Here is the code of the "OpenNextFile" and I will attach a screenshot of the action. My action is pretty inefficient right now and I apologize for the messiness, I am not great with scripting and wouldn't be able to script this action.

 

Code:

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;

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!");
}

 

TOPICS
Actions and scripting, Bug, Problem or error, Windows

Views

17

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Batch Closes First Opened Document After Completed Action & Script

New Here ,
Oct 30, 2020

Copy link to clipboard

Copied

I am currently running into an issue that I think is a bug. I have an action that takes a string of images and creates a 3x3 collage out of the string of 9 images. I found a script online that allows me to do this, and that script is called OpenNextFile. It takes a file called "00000.jpg" and opens the next file "00001.jpg" in photoshop. Each iteration closes the document that executed that script, so "00000.jpg" was closed. Using this script, I can create a collage by opening each file in the sequence, copying the layer, and pasting it to a created document.

This action performs what I want it to when I take an image into photoshop and run the action in the actions menu. When I run into trouble is when I batch process this action. I tried to load up every 10th image in photoshop and run a batch using "opened files" as a source. The first iteration of the batch performs perfectly. This iteration starts with the last document that is opened, so "00037.jpg" if I want to batch 5 images. My bug occurs in the next iteration of the batch. For some reason, in each new iteration of the batch process, photoshop will go to the first document that is oepned, so "00000.jpg" and it closes this file. Then it goes to the next image in the sequence, "00028.jpg" and runs the action on this image. Once this action is finished, photoshop will go to the first image, "00010.jpg" now, and closes this file. Because of this, the batch only processes half the files that I want it to. I haven't ran into this issue yet and I would love to know what is causing this. Here is the code of the "OpenNextFile" and I will attach a screenshot of the action. My action is pretty inefficient right now and I apologize for the messiness, I am not great with scripting and wouldn't be able to script this action.

 

Code:

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;

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!");
}

 

TOPICS
Actions and scripting, Bug, Problem or error, Windows

Views

18

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 30, 2020 0

Have something to add?

Join the conversation