Skip to main content
Known Participant
April 30, 2020
Answered

Script to place Logos in different phones cases in Photoshop

  • April 30, 2020
  • 6 replies
  • 9774 views

Hello everyone ,   to start i want to point out that i have no knowledge of scripting just wondering if any of you can help. i am using so far actions but i guess they're so limited even when using conditions.  

 

For my case i want to make a script that i can place a logo design over a phones cases (iphones cases for examples) i was able to make an "action" to place the designs but i have a small problem is that in some of the phones cases there's diffrent holes and different shapes and sizes so the "move and place" won't work in such a case, i want to make a condition at least by jpeg numbers, when it reach the 13 images excute this condition instead, that's the least that i can think off, i have attached an example of many please check it.

 

thank you in advance for your help !

This topic has been closed for replies.
Correct answer Stephen Marsh

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

 

myfile-RED.psd

myfile-GREEN.psd

myfile-BLUE.psd

 

An action corresponding to the specified matching portion of the filename would then be applied, overwriting the original files.

 

I have surrounded each condition in comments clearly identifying each code block so that you can easily copy/paste them to create additional conditions.

 

You will obviously need to change the name of the action and action set.

 

This script overwrites the originals, so work on copies.

 

 

/*

Batch Play Actions from Conditional Filename Parts.jsx

https://community.adobe.com/t5/photoshop/script-to-place-logos-in-diffrent-phones-cases-in-photshop/m-p/11098559
Script to place Logos in diffrent phones cases in Photshop

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

myfile-RED.psd
myfile-GREEN.psd
myfile-BLUE.psd

An action corresponding to the specified matching portion of the filename would then be applied, overwriting the original files.

The indexOf method is case sensitive:
    if (app.activeDocument.name.indexOf('-RED') != -1) {

An alternative is to use a case insensitive regular expression based match:
    if (app.activeDocument.name.match(/-RED/gi) != null) {

*/

#target photoshop
app.bringToFront();

if (!documents.length) {

    var savedDisplayDialogs = app.displayDialogs;

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    var inputFiles = inputFolder.getFiles();

    app.displayDialogs = DialogModes.NO;

    for (var a = 0; a < inputFiles.length; a++) {
        try {
            var inDoc = open(inputFiles[a]);

            // Start doing stuff

            /* CONDITION #1 */
            if (app.activeDocument.name.indexOf('-RED') != -1) {
                app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #1 */

                /* CONDITION #2 */
            } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #2 */

                /* CONDITION #3 */
            } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #3 */

            } else {
                /* DO SOMETHING ELSE */
            }

            app.activeDocument.close(SaveOptions.SAVECHANGES);

            // Finish doing stuff

        } catch (e) {
            continue;
        }
    }

    app.displayDialogs = savedDisplayDialogs;
    alert('Script completed!' + '\n' + 'Files saved to:' + '\n' + inputFolder.fsName);

} else {
    alert('Please close all open files before running this script');
}

 

 

6 replies

Stephen Marsh
Community Expert
Community Expert
February 21, 2024

Here's a related script topic link where the action to be run is determined by the folder name:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/ps-script-for-multiple-actions-for-files-in-different-folders-automatic/m-p/14435721

Stephen Marsh
Community Expert
Community Expert
May 3, 2020

This variation on the previous script will automatically save the JPEG files to a sub-folder titled "Processed Mockups" in the source/input folder using an interactive prompt for the suffix.

 

 

/*

Batch Play Actions from Conditional Filename Parts - Save JPEG to Source Sub-Folder.jsx

Changelog:
2nd May 2020 - Initial Release
3rd May 2020 - Added interactive suffix prompt and JPEG save to sub-folder in input folder

https://community.adobe.com/t5/photoshop/script-to-place-logos-in-diffrent-phones-cases-in-photshop/m-p/11098559
Script to place Logos in diffrent phones cases in Photshop

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

myfile-RED.psd
myfile-GREEN.psd
myfile-BLUE.psd

An action matching the specified matching portion of the filename would then be applied, overwriting the original files.

The indexOf method is case sensitive:
    if (app.activeDocument.name.indexOf('-RED') != -1) {

An alternative is to use a case insensitive regular expression based match:
    if (app.activeDocument.name.match(/-Red/gi) != null) {

*/

#target photoshop
app.bringToFront();

(function () {

    if (!documents.length) {

        var savedDisplayDialogs = app.displayDialogs;

        var inputFolder = Folder.selectDialog('Select the input folder:', ''); // Optionally add a default directory
        if (inputFolder == null) { return }; // Test if cancel returns null, then do nothing.  

        var outputFolder = new Folder(decodeURI(inputFolder) + '/Processed Mockups'); // Change name, retain the leading / forward slash
        if (outputFolder.exists == false) outputFolder.create();

        var suffix = prompt('Enter the suffix to append:', '');
        if (suffix == null) { return }; // Test if cancel returns null, then do nothing.  
        if (suffix == "") { return }; // Test if an empty string is returned, then do nothing.
        /* https://prepression.blogspot.com/2019/03/bridge-batch-rename-to-clean-invalid.html */
        var suffixCleansed = suffix.replace(/[^-_0-9a-zA-Z]/gi, '');

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|psd|psb|eps|png|bmp|gif)$/i); // Add or remove as required

        app.displayDialogs = DialogModes.NO;

        for (var a = 0; a < inputFiles.length; a++) {
            try {
                var inDoc = open(inputFiles[a]);

                // START DOING STUFF

                    /* CONDITION #1 */
                if (app.activeDocument.name.indexOf('-RED') != -1) {
                    app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                    /* CONDITION #1 */

                    /* CONDITION #2 */
                } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                    app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                    /* CONDITION #2 */

                    /* CONDITION #3 */
                } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                    app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                    /* CONDITION #3 */

                } else {
                    /* DO SOMETHING ELSE */
                }

                var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                var saveFileJPEG = new File(outputFolder + '/' + docName + '_' + suffixCleansed + '.jpg');
                SaveForWeb(saveFileJPEG);

                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                // FINISH DOING STUFF

                // JPEG S4W Options
                function SaveForWeb(saveFileJPEG, jpegQuality) {
                    /*
                    // Convert to sRGB Step
                        if (app.activeDocument.colorProfileName.substring(0, 4) != "sRGB")
                        app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither
                    */
                    var sfwOptions = new ExportOptionsSaveForWeb();
                    sfwOptions.format = SaveDocumentType.JPEG;
                    sfwOptions.includeProfile = true;
                    sfwOptions.optimized = true;
                    sfwOptions.quality = 70;
                    activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
                }

            } catch (e) {
                continue;
            }
        }

        app.displayDialogs = savedDisplayDialogs;
        alert('Script completed!' + '\n' + inputFiles.length + ' files saved to:' + '\n' + outputFolder.fsName);

    } else {
        alert('Please close all open files before running this script');
    }
}
)();

 

 

Stephen Marsh
Community Expert
Community Expert
May 3, 2020

"Well i think it's better to create a folder where the original files are auto like "Final rendering" or "Generated" anything,"

Yes, but where? A new folder inside the source folder? In order to automate, one needs to know the destination. This is not clear to me.

 

"well for my case the name comes from the design logo that's been added to the cases"

Is this the name of the input folder? In order to automate, one needs to know the source. This is not clear to me.

 

______________

 

Please try this revised code and let me know how it works for you.

 

This version adds a dialog window to select an output folder and uses Export/Save for Web to save to JPEG quality 70%, it is easy to change this in the code to another quality % level. There is an option to convert to sRGB which is not active, just remove the opening /* and closing comments */ to enable. A prompt will also ask for the suffix to add to the input filename, for example a suffix of "mySuffix" added to the input name of "myFile.psd" would be saved as "myFile_mySuffix.jpg" (the underscore delimiter is hardcoded and should not be included in the suffix).

 

 

/*

Batch Play Actions from Conditional Filename Parts - Prompt for Suffix and JPEG Save Location.jsx

Changelog:
2nd May 2020 - Initial Release
3rd May 2020 - Added interactive JPEG save & suffix prompts

https://community.adobe.com/t5/photoshop/script-to-place-logos-in-diffrent-phones-cases-in-photshop/m-p/11098559
Script to place Logos in diffrent phones cases in Photshop

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

myfile-RED.psd
myfile-GREEN.psd
myfile-BLUE.psd

An action matching the specified matching portion of the filename would then be applied, overwriting the original files.

The indexOf method is case sensitive:
    if (app.activeDocument.name.indexOf('-RED') != -1) {

An alternative is to use a case insensitive regular expression based match:
    if (app.activeDocument.name.match(/-Red/gi) != null) {

*/

#target photoshop
app.bringToFront();

(function () {

    if (!documents.length) {

        var savedDisplayDialogs = app.displayDialogs;

        var inputFolder = Folder.selectDialog('Select the input folder:', ''); // Optionally add a default directory
        if (inputFolder == null) {
            return
        }; // Test if cancel returns null, then do nothing.

        var outputFolder = Folder.selectDialog('Select the JPEG output folder:', ''); // Optionally add a default directory
        if (outputFolder == null) {
            return
        }; // Test if cancel returns null, then do nothing.

        var suffix = prompt('Enter the suffix to append:', '');
        if (suffix == null) {
            return
        }; // Test if cancel returns null, then do nothing.
        if (suffix == "") {
            return
        }; // Test if an empty string is returned, then do nothing.
        /* https://prepression.blogspot.com/2019/03/bridge-batch-rename-to-clean-invalid.html */
        var suffixCleansed = suffix.replace(/[^-_0-9a-zA-Z]/gi, '');

        var inputFiles = inputFolder.getFiles(/\.(jpg|jpeg|tif|tiff|psd|psb|eps|png|bmp|gif)$/i); // Add or remove as required

        app.displayDialogs = DialogModes.NO;

        for (var a = 0; a < inputFiles.length; a++) {
            try {
                var inDoc = open(inputFiles[a]);

                // START DOING STUFF

                    /* CONDITION #1 */
                if (app.activeDocument.name.indexOf('-RED') != -1) {
                    app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                    /* CONDITION #1 */

                    /* CONDITION #2 */
                } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                    app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                    /* CONDITION #2 */

                    /* CONDITION #3 */
                } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                    app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                    /* CONDITION #3 */

                } else {
                    /* DO SOMETHING ELSE */
                }

                var docName = app.activeDocument.name.replace(/\.[^\.]+$/, '');
                var saveFileJPEG = new File(outputFolder + '/' + docName + '_' + suffixCleansed + '.jpg');
                SaveForWeb(saveFileJPEG);

                app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                // FINISH DOING STUFF

                // JPEG S4W Options
                function SaveForWeb(saveFileJPEG, jpegQuality) {
                    /*
                    // Convert to sRGB Step
                        if (app.activeDocument.colorProfileName.substring(0, 4) != "sRGB")
                        app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, false); // BPC, Dither
                    */
                    var sfwOptions = new ExportOptionsSaveForWeb();
                    sfwOptions.format = SaveDocumentType.JPEG;
                    sfwOptions.includeProfile = true;
                    sfwOptions.optimized = true;
                    sfwOptions.quality = 70;
                    activeDocument.exportDocument(saveFileJPEG, ExportType.SAVEFORWEB, sfwOptions);
                }

            } catch (e) {
                continue;
            }
        }

        app.displayDialogs = savedDisplayDialogs;
        alert('Script completed!' + '\n' + inputFiles.length + ' files saved to:' + '\n' + outputFolder.fsName);

    } else {
        alert('Please close all open files before running this script');
    }
})();

 

 

PytGamerAuthor
Known Participant
May 3, 2020

Is this the name of the input folder? In order to automate, one needs to know the source. This is not clear to me.

 

check this, this is where the action take the design image png (from the folder Design goes here) to add it to the cases

for this example the name of the design is Design so that will be added to the output cases names example: 'Condor 5 Clear Design.jpeg'

I was going to ask you how to make the script fetch the name from the whole image name but as i did tests and put the word in the start of the image name in the middle mixed it with other names and it's totally working and finding the name wherever it is in the image name that's really cool.

 

I already chose your respond as a correct one, you have answer all my queries and even more, you really have made my day and my Exam test, i really don't know how to thank you, thank you so much.

 

One last thing if you excuse me are you the one who made this script if so i hope you give me some refferences that you used to start writting scripts i am willing to start with this domain thanks.

Stephen Marsh
Community Expert
Community Expert
May 4, 2020

Yes, I made the script, in a similar way that Dr Frankenstein made his creation. An original bit here, a borrowed bit there etc. All the bits stuck together to create a whole, but perhaps not as clean or pretty as a natural birth! I tend to reuse previous code a lot and have a library of snippets for general reuse. Sadly I spend more time debugging and trying to work out why I have broken something that used to work in a similar but different context.

 

I found that it was very hard for me to study and learn 'generic' JavaScript with no specific goal apart from learning JavaScript... So participating in discussion threads such as this gives me a much better way to learn.

 

As for learning resources, Adobe docs/guides, these forums, internet searches and the W3C site are my usual resources.

 

After 2 years or so of playing with scripting, I'm still a beginner, just a little less green than when I started. I have been using Photoshop since the early 1990's (before layers were introduced) and have a lot of experience with creating actions, which helps.

 

Good luck!

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 2, 2020

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

 

myfile-RED.psd

myfile-GREEN.psd

myfile-BLUE.psd

 

An action corresponding to the specified matching portion of the filename would then be applied, overwriting the original files.

 

I have surrounded each condition in comments clearly identifying each code block so that you can easily copy/paste them to create additional conditions.

 

You will obviously need to change the name of the action and action set.

 

This script overwrites the originals, so work on copies.

 

 

/*

Batch Play Actions from Conditional Filename Parts.jsx

https://community.adobe.com/t5/photoshop/script-to-place-logos-in-diffrent-phones-cases-in-photshop/m-p/11098559
Script to place Logos in diffrent phones cases in Photshop

The following code example presumes that a folder of input files have a hyphen - delimiter with variable case-sensitive text, such as a colour name:

myfile-RED.psd
myfile-GREEN.psd
myfile-BLUE.psd

An action corresponding to the specified matching portion of the filename would then be applied, overwriting the original files.

The indexOf method is case sensitive:
    if (app.activeDocument.name.indexOf('-RED') != -1) {

An alternative is to use a case insensitive regular expression based match:
    if (app.activeDocument.name.match(/-RED/gi) != null) {

*/

#target photoshop
app.bringToFront();

if (!documents.length) {

    var savedDisplayDialogs = app.displayDialogs;

    var inputFolder = Folder.selectDialog('Select the input folder', '');
    var inputFiles = inputFolder.getFiles();

    app.displayDialogs = DialogModes.NO;

    for (var a = 0; a < inputFiles.length; a++) {
        try {
            var inDoc = open(inputFiles[a]);

            // Start doing stuff

            /* CONDITION #1 */
            if (app.activeDocument.name.indexOf('-RED') != -1) {
                app.doAction('Red Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #1 */

                /* CONDITION #2 */
            } else if (app.activeDocument.name.indexOf('-GREEN') != -1) {
                app.doAction('Green Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #2 */

                /* CONDITION #3 */
            } else if (app.activeDocument.name.indexOf('-BLUE') != -1) {
                app.doAction('Blue Action', 'Change Colour Action Set'); // Change action & action set name
                /* CONDITION #3 */

            } else {
                /* DO SOMETHING ELSE */
            }

            app.activeDocument.close(SaveOptions.SAVECHANGES);

            // Finish doing stuff

        } catch (e) {
            continue;
        }
    }

    app.displayDialogs = savedDisplayDialogs;
    alert('Script completed!' + '\n' + 'Files saved to:' + '\n' + inputFolder.fsName);

} else {
    alert('Please close all open files before running this script');
}

 

 

PytGamerAuthor
Known Participant
May 2, 2020

Wow thank you so much Stephen,  i really have no words to say, it really works, excuse me if am going to ask you one last thing, the script is saving my work as a PSD and it is asking me everytime where to save, can you tell me how to edit the script so that i save in specific format for my example here it's a Jpeg and how to add a line of code to ask me to choose a file where to save or create one automatically as it's asking me at the beggining, i am know that i am exaggerating but is there a way to add words to the image files names for example my original file name is "Iphone 5 red" and at the end of the script it will add 'liberty statue' so i it will be "Iphone 5 red liberty statue"thanks you so much in advance.

Stephen Marsh
Community Expert
Community Expert
May 2, 2020

The original script was more of a proof of concept than anything, I expected it to require fine tuning.


Yes, it is possible to save into a different format, such as JPEG. Save as or save for web? What options?

 

It is possible to save to a specific location. Do you want to pick an output folder? Or do you want the script to make a new output folder inside the input folder? What name and where would the name come from?

 

It is also possible to modify the input name with a prefix or suffix at output. I presume that this name would be variable and not fixed/static. Where would it come from? The file itself? From the action or action set in use? By typing into a dialog? From the save folder name?

Stephen Marsh
Community Expert
Community Expert
May 1, 2020

"... i already work on the actions i just want a script to launch those actions depends on the files names that's all and save them."

 

If you simply wish to run a specific action when a specific filename is found, then this script might help:

 

 

SIVA'S PHOTOSHOP CONDITIONAL ACTION

Download Siva's Photoshop Conditional Action

PytGamerAuthor
Known Participant
May 2, 2020

Hi Stephe, thanks for your replay, i already found your code when was exploring the internet and this forum, but your script is limited so i have to open it each time and do everything manually, how can turn it to choose multiple threads thanks.

Stephen Marsh
Community Expert
Community Expert
May 2, 2020

A semi-automated approach is better than nothing, right? :]

JJMack
Community Expert
Community Expert
April 30, 2020

You only show one design. If all your designs are the same aspect ratio what you need to do is create mock ups for each phone case. And have smart objects logo layers that are transformed and warped for each case then the cases can be populated by a batch populating script. The smart object layer can be masked so that the case holes will not get any logo information in them. However, the logo needs to be warped  to avoid the holes and maintain its contents.  The Logo in your example would be better done using two smart object layers one for the graphic image that when  masked would  have holes punched by the layers mask. The Second smart object layer for the text. So that can be located independently from the graphic. You problem is  related to the text placement.  You basically want two objects that  have two aspect aspect ratios a more or less square aspect ratio for  the graphoc and a rectangle area for the text.  Each smart Object has an associated object transform.  So each mock up can size, rotate, position and warp the layers object. Additionally smart filters can be also added. You just want two standard size objects in your templates. So a script can replace their content by either using Replace content or editing the standard size object and fitting the replacments.  The Smart Object layer's  object transform will takes care of the rest for the mock ups.  The transforms do not change. So the Object will be sized, rotated, position and warped for each mock up you populate the way you created the mocksups for your two standard size opjects.

 

 

JJMack
PytGamerAuthor
Known Participant
April 30, 2020

Hi JJMack; Thank you for responding to my post, yes the main problem in this scenario is the text it self, that's why i have opened this post, so if i can know the basics how to manipulate the text and move according to the phone shape and holes i can apply the same thing for any other design, this is not a mockup of a full background applied on the case but a design to be placed so it's complicated i guess for me to create conditions, if it's a background it would be easy using the smart object as you suggested, so how can i solve this particular problem thanks.

JJMack
Community Expert
Community Expert
May 1, 2020

Have you tried searching this forum.  Do you actually think your are the first person to have a question like yours. There are many threads on products mockups. What did you not understand about what I wrote?

 

JJMack