Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to save as png file with a specific name and put it in a specific folder.

Explorer ,
Sep 28, 2022 Sep 28, 2022

I use 3D application and Photoshop at the same time.

I use Photoshop to edit textures and export them as png and see how it looks in 3D application.

For example I open psd file and edit image and export image as png with name "texture.png" and put it in a folder "D:\textures", and check how it looks in 3D application. After that I am going back to Photoshop and edit same image and export it as png with same name "texture.png" and put it in the same folder "D:\textures" and check it again in 3D appllication. I need to do this again and again. Right now I do this manually. it's really time consuming, so I would like to know if it is  possible to automate it with jsx script or not.  I would like to automate three process "save image as png","name it a specific name","and "put it in a specific folder." jsx can do it automattically?

TOPICS
Actions and scripting
4.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 29, 2022 Sep 29, 2022

@Rodan_Shiner 

 

I was delayed by some unexpected errors and debugging when I used your PNG save code, so I went back to my previous code. I also got bogged down a bit in exploring some edge-case error checking, hoping to improve on the code used in earlier scripts. It is all in one script, there is no need for two separate scripts, but that is OK too if you prefer it that way (I used to do that until I found out about keyboardState). The ALT/OPT key has to be depressed while the script is run t

...
Translate
Adobe
Community Expert ,
Sep 28, 2022 Sep 28, 2022

You can record action but if you are familiar with script I will tag few of them who can assist you @c.pfaffenbichler @jazz-y @Stephen Marsh 

 

You need action or script in case that you have layered file in Photoshop, otherwise save and keep open texture.png in Photoshop, change it, then use Save to save back changes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

Thank you. I need to edit psd which have layers and I need to save multiple images.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 28, 2022

This sounds pretty basic and shouldn't be hard. When you same name the file and put it in a folder, is the name always the same, and is the folder always the same? If so, that info can be coded into a script. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

I will be more specific..

For example, like I said before I open psd file which has lots of layers.

it has UV maps,diffuse maps,specular maps,roughness maps,normal maps, an adjustment layers. And I need to edit it. When  I need to edit diffuse map, I edit diffuse map layers and related adjustment layers, and when I need to edit normal map, I edit normal map layers and related adjustmen layers.Same things goes with other maps.And when I've done editing I need to export them individually. I export diffuse map as "diffuse_texture.png" and put it in "D:\textures\diffuse", and I export normal map as "normal_texture.png" and put int in "D:\textures\normal". 

 

So file name is not always same.And folder isn't same too. I awalys edit mutliple images and export them.

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2022 Sep 28, 2022

Using Save As:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-save-as-png-file-with-a-specific-name-and-put-it-in-a-specific-folder/td-p/13233189
*/

#target photoshop

// User variables
var outputFolder = Folder("d:\\textures");
var pngName = "texture.png";

if (!outputFolder.exists) outputFolder.create();
var savePNG = new File(outputFolder + "\\" + pngName);
var saveOptions = new PNGSaveOptions();
saveOptions.compression = 0; // compression value: 0-9
saveOptions.interlaced = false;
activeDocument.saveAs(savePNG, saveOptions, true);
app.beep();

 

Using Export Save for Web (Legacy):

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-save-as-png-file-with-a-specific-name-and-put-it-in-a-specific-folder/td-p/13233189
*/

#target photoshop

// User variables
var outputFolder = Folder("d:\\textures");
var pngName = "texture.png";

if (!outputFolder.exists) outputFolder.create();
var saveOptions = new PNGSaveOptions();
var pngOptions = new ExportOptionsSaveForWeb();
pngOptions.PNG8 = false;
pngOptions.transparency = true;
pngOptions.interlaced = false;
pngOptions.quality = 100;
pngOptions.includeProfile = true;
pngOptions.format = SaveDocumentType.PNG;
activeDocument.exportDocument(File(outputFolder + "\\" + pngName), ExportType.SAVEFORWEB, pngOptions);
app.beep();

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

Thank you. What I would like to do is written on replay to Chuck Uebele. I should have said that.Sorry.

var outputFolder = Folder("d:\\textures");
var pngName = "texture.png";

your scipt needs those two variables, but image name and folder name are changed.

In Photoshop, when I save image, at least Photoshop remembers last folder location.

So how about this. I save a image "diffuse_texture.png"manually in a folder "D:\textures\diffuse" and after that jsx does same thing again and again. I would like it to do "save image as png and name it diffuse_texture.png in a folder D:\textures\diffuse". And when I've done editing diffuse map then I start editing normal map. I manually save  "normal_texture.png" in a folder "D:\textures\normal" and after that jsx does same thing again and again. Is it possible? I always use same file format, so it dosn't need to be changed, but file name and folder location need to be changed.jsx can have variables "last used folder name" and "last used folder location"?

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

Yes, it would have been helpful to fully specify the process from the beginning.

 

OK, it is possible to remember the last save location and reset a save location etc. Do you need this to be remembered between Photoshop sessions (after a quit and restart of the software) or will you always be saving in the same open running session and don't need the last path to be remembered after exiting Photoshop?

 

Which PNG method is better for your 3D software, the Save As or Export Save for Web?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

I wrote a makeshift script and what I need in my newest post. Please check it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

That makes it a lot more complicated. So are you saving/exporting only a few layers each time, say just the diffusion layer and some adjustment layers? If so, are all the layers that you want to save in one group? If you're not putting them in a single group, it would be difficult to let the script know which layers need to be save together. The group should be named how you want the file named, like "texture\diffusion." To make the script more simple to get the file structure to save, the name should be in the order of the folder hierarchy and use back slashes, rather than forward slashes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

I can put them in a layer folder. it sounds like folder name can be used to name a file and save it in a specific folder? llike layer folder name "D:\textures\diffuse.png" and put it in a variable?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

Yes, you can do that, but using the scripting Convention it would be best to write the name like this: " D\textures\diffuse" as you don't need the colon, if you use back slashes. Also if all your saved files are the same name as the last word, then you wouldn't need to add .png to the name. To define a folder in a script would look like this:

 

var diffuseFolder = new Folder ('\d\diffuse\');
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022
quote
var diffuseFolder = new Folder ('\d\diffuse\');

By @Chuck Uebele

 

Thanks Chuck, I usually work with cross-platform code, Windows specific paths in JS are alien to me. I did test in Windows using the colon and escaped backslashes and it worked fine. As the path is Windows specific, I figured that it wouldn't matter as long as it works. I notice that in addition to removing the colon, you didn't escape the backslashes. I had it in my mind that they had to be escaped, while Mac/cross-platform JS forward slashes didn't need to be escaped?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

With forward slashes, you need the escape slash, but not back slashes. I use the back slash as it's cross platform compatible. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

Opps, got that backwards: forward slashes don't need the escape slash.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

OK. I wrote a script and what I need in my newest post. Please check it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

I wrote a script. It uses active layer name and save current layer state of psd file as png file.

Problem is save location is where the psd file is.And I use this png format, nothing special.

 

var docPath = app.activeDocument.path.fullName
var layerName = app.activeDocument.activeLayer.name
var newFileName = docPath.concat( "/", layerName, ".png")

fileObj = new File( newFileName )
pngOpt = new PNGSaveOptions();
pngOpt.interlaced = false;
activeDocument.saveAs(fileObj, pngOpt, true, Extension.LOWERCASE);

 

I think what I need is two jsx files. 1st jsx to decide save location, and 2nd jsx to save png file. 1st jsx must have this.

 

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

 

and once I set the folder location manually using Photoshop dialogue box I would like to use this folderLocation variable again and again. Is it possible? I guess I need to save folderLocation variable somewhere else? as txt format or something?

2nd jsx needs to use this folderLocation variable and saves png file.If I use the variable with above script It must be like this.

 

var docPath = folderLocation

 

And when I need to change save location I am gonna run that 1st jsx to decide folder location again and let 2nd jsx use renewed folderLocation variable and save png. Is this possible?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

That helps but you didn't answer my specific question whether the save location should be remembered after exiting Photoshop.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

I would like keep the location in txt file after exiting Photoshop to reuse it. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

Thank you, it can be stored in "temporary memory" via $.setenv() while Photoshop is running and available between different script executions, but will then be lost if Photoshop is quit/exited. I'll use a log file instead.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

$.setenv() and $.getenv() works, but I don't know how to use a log file. Could you tell me how?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022
quote

$.setenv() and $.getenv() works, but I don't know how to use a log file. Could you tell me how?


By @Rodan_Shiner

 

I'm about 3/4 of the way through writing the script... I'll post it soon!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

Thank you. I am waiting for your script.

I wrote 2 script but I don't know if it's the log file you mentioned. Is it correct?

 

1st jsx. It decides where to save png file.

var outputFolder = Folder.selectDialog("Save Folder")
var location_txt = File("C:/saveFile_Location.txt")
location_txt.open("w")
location_txt.write(outputFolder)
location_txt.open("r")
var folder_path_from_txt = location_txt.read()
alert(folder_path_from_txt)
location_txt.close()

 

2nd jsx. It saves current canvas state as png file with active layer name in the folder 1st jsx set.

var location_txt = File("C:/saveFile_Location.txt")
location_txt.open("r")
var folder_path_from_txt = location_txt.read()
location_txt.close() 

var docPath = folder_path_from_txt
var layerName = app.activeDocument.activeLayer.name
var newFileName = docPath.concat( "/", layerName, ".png")
fileObj = new File(newFileName)
pngOpt = new PNGSaveOptions()
pngOpt.interlaced = false;
activeDocument.saveAs(fileObj, pngOpt, true, Extension.LOWERCASE)

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2022 Sep 29, 2022

@Rodan_Shiner 

 

I was delayed by some unexpected errors and debugging when I used your PNG save code, so I went back to my previous code. I also got bogged down a bit in exploring some edge-case error checking, hoping to improve on the code used in earlier scripts. It is all in one script, there is no need for two separate scripts, but that is OK too if you prefer it that way (I used to do that until I found out about keyboardState). The ALT/OPT key has to be depressed while the script is run to set the save location. Thereafter, the save location will continue to be used until the script is run again with the ALT/OPT key held down.

 

Anyway, please try the following v1.0 code.

 

/*
Save PNG to Preference Log File Location.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-save-as-png-file-with-a-specific-name-and-put-it-in-a-specific-folder/td-p/13233189
v1.0 30th September 2022 - Stephen Marsh
*/

#target photoshop

if (app.documents.length) {
    try {
        /***** HOLD THE ALT/OPT KEY DOWN TO SET THE FOLDER PATH *****/
        if (ScriptUI.environment.keyboardState.altKey) {

            // Set the save folder path
            try {
                var prefFilePath = Folder.selectDialog('Select a folder to save the PNG image to:');
            } catch (e) {}

            // Log text file platform specific line feed options
            var os = $.os.toLowerCase().indexOf("mac") >= 0 ? "mac" : "windows";
            if (os === "mac") {
                prefFileOutLF = "Unix"; // Legacy = "Macintosh"
            } else {
                prefFileOutLF = "Windows";
            }

            // Create the preference file in the user home folder
            var prefFileOut = new File('~/_Last_PNG_Save_Path.txt');
            var dateTime = new Date().toLocaleString();
            if (prefFileOut.exists)
                prefFileOut.remove();
            prefFileOut.open("w");
            prefFileOut.encoding = "UTF-8";
            prefFileOut.lineFeed = prefFileOutLF;
            prefFileOut.write(dateTime + "\r" + app.activeDocument.name + "\r");
            prefFileOut.writeln(prefFilePath);
            prefFileOut.close();

            // Call the main PNG save function directly after setting the path
            savePNGfile();

        /***** IF THE ALT/OPT KEY WAS NOT HELD DOWN, RUN THE MAIN SCRIPT *****/
        } else {
            // Call the main PNG save function
            savePNGfile();
        }
    } catch (e) {}
} else {
    alert("A document must be open to use this script!");
}


function savePNGfile() {

    var prefFileIn = File('~/_Last_PNG_Save_Path.txt');
    if (File(prefFileIn).exists && File(prefFileIn).length > 0) {
        // Read the preference file
        prefFileIn.open('r');
        // Read (skip) the first two lines from the log file, a means to an end...
        var skipLineOne = prefFileIn.readln(1);
        var skipLineTwo = prefFileIn.readln(2);
        // Read the 3rd line from the preference file (file path)
        var prefFilePathValue = prefFileIn.readln(3);
        prefFileIn.close();
        if (prefFilePathValue.length === 0) {
            alert("The directory path is empty. Run the script again while holding down the ALT/OPT key!");
        }
        // Save as PNG
        var layerName = app.activeDocument.activeLayer.name;
        var savePNG = new File(prefFilePathValue + "/" + layerName + '.png');
        var saveOptions = new PNGSaveOptions();
        saveOptions.compression = 0; // compression value: 0-9
        saveOptions.interlaced = false;
        activeDocument.saveAs(savePNG, saveOptions, true, Extension.LOWERCASE);
        app.beep();
    } else {
        app.beep();
        alert('There is no valid file named "_Last_PNG_Save_Path.txt" in the user home folder!' + '\r' + 'Run the script again while holding down the ALT/OPT key.');
    }
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 29, 2022 Sep 29, 2022

it works. thank you. Nice script. I like alt key conditional branch. 

I always use jsx script as action button,is it supposed to be used as action button,right?

Because I can't push alt key when I execute jsx from "File - Script" menu.

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