Script to save current document as the previous document's file name

Community Beginner ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

Hi! I made an action log for my project that takes the previous document's image and places it as the base (first layer) image for the document I am working in. I am trying to figure out if there is a script that allows me to save the document I am working in as the previous document's file name. I am doing a large batch of photos and creating gifs for them and have thousands of documents that I don't want to individually save each file as the previous file's name. Help!

TOPICS
Actions and scripting , macOS

Views

113

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
community guidelines
Adobe Community Professional ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

Presuming that the "source" document is always the first open file:

 

var sourceDocName = app.documents[0].name.replace(/\.[^\.]+$/, '');

 

You would then reference the named variable in the script save code (this step is not possible via an action).

 

Where is the PSD being saved? The same location as the "source" document? The desktop? Where?

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
community guidelines
Adobe Community Professional ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

An example of the file saving:

 

#target photoshop;
var sourceDocName = app.documents[0].name.replace(/\.[^\.]+$/, '');
var outputFolder = app.documents[0].path.fsName;
// var outputFolder = "~/Desktop/";

var saveFilePSD = new File(new File(outputFolder + '/' + sourceDocName + '.psd'));
SavePSD(saveFilePSD);

function SavePSD(saveFilePSD) {
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    psdSaveOptions.layers = true;
    psdSaveOptions.annotations = true;
    psdSaveOptions.spotColors = true;
    // Save As (false)
    app.activeDocument.saveAs(saveFilePSD, psdSaveOptions, false, Extension.LOWERCASE);
    // Save As Copy (true)
    //app.activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
    //app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

 

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

 

EDIT: This version adds some error checking:

 

#target photoshop
if (app.documents.length === 2) {
    try {
        app.documents[0].path;
        var sourceDocName = app.documents[0].name.replace(/\.[^\.]+$/, '');
        var outputFolder = app.documents[0].path.fsName;
        // var outputFolder = "~/Desktop/";
        var saveFilePSD = new File(new File(outputFolder + '/' + sourceDocName + '.psd'));
        SavePSD(saveFilePSD);
    } catch (error) {
        alert("The source document is unsaved!");
    }
} else {
    alert("Only 2 documents should be open to use this script!");
}

function SavePSD(saveFilePSD) {
    psdSaveOptions = new PhotoshopSaveOptions();
    psdSaveOptions.embedColorProfile = true;
    psdSaveOptions.alphaChannels = true;
    psdSaveOptions.layers = true;
    psdSaveOptions.annotations = true;
    psdSaveOptions.spotColors = true;
    // Save As (false)
    app.activeDocument.saveAs(saveFilePSD, psdSaveOptions, false, Extension.LOWERCASE);
    // Save As Copy (true)
    //app.activeDocument.saveAs(saveFilePSD, psdSaveOptions, true, Extension.LOWERCASE);
    //app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

 

 

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
community guidelines
Community Beginner ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

OMG you are seriously THE BEST!!! Thank you so much! Seriously! Just tried it and it worked:) I have been stuck on this forever and you saved me future hours wasted.. So thank you again haha:)

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
community guidelines
Adobe Community Professional ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

@Sage23163048ecar 

 

Thank you, your welecome!

 

If you are happy that the reply is the "correct annswer" then please mark it as so (there can be more than one correct answer if others are also added). I wasn't sure if you wanted to save as or save as a copy and or close after saving, the code is easily modified with these options if desired).

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
community guidelines
Community Beginner ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

You're Awesome thank you! Marked it as the correct answer, thank you again!

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
community guidelines
Community Beginner ,
Feb 14, 2022 Feb 14, 2022

Copy link to clipboard

Copied

For some reason the script isn't working anymore? I restarted photoshop and I only have two documents open at a time?

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
community guidelines
Adobe Community Professional ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

I'm not sure why it stopped working... Is the first document saved, the script needs to pickup the saved file path for the save location.

 

Although it can be painful, sometimes resetting preferences fixes* issues that can't be explained...

(*except for when it doesn't)

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
community guidelines
Community Beginner ,
Feb 15, 2022 Feb 15, 2022

Copy link to clipboard

Copied

LATEST

I don't know why either:/ Yeah the first source document is saved. I only have the source document open and the second tab with the other document open.

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
community guidelines