Skip to main content
Participant
March 23, 2020
Answered

Looking for Photoshop Script that gives a file a unique name, saves, and close.

  • March 23, 2020
  • 4 replies
  • 2541 views

Hello,

I am looking for a Photoshop Script that gives my file a unique name, saves it, then closes it. I am a storyboard artist and I deal with several files and I am looking for a shortcut so that I don't have to manually name all my files. I just want to push a button and then it's named and saved automatically.

 

For example, I'm done drawing a panel, hit a button, then a files saves out as: "Panel_001.PSD" Then I draw another panel, I hit a button, and that panel saves out as "Panel_002.PSD" etc. etc.

 

Thank you.

This topic has been closed for replies.
Correct answer jazz-y

 

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
       (o = new JPEGSaveOptions ()).quality = 12
       activeDocument.saveAs(createUniqueFileName(f), o)
       activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.jpg',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

 

4 replies

Stephen Marsh
Community Expert
Community Expert
March 25, 2020

An alternative approach is to create a new document with the unique filename:

 

https://community.adobe.com/t5/photoshop/vba-to-javascript/m-p/10967341

Legend
March 24, 2020

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
        activeDocument.saveAs(createUniqueFileName(f))
        activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.psd',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

* saving only in psd (with default params) to the same folder from which the document was opened, or to the specified folder (if the document has not yet been saved)

Geppetto Luis
Legend
March 25, 2020

DmitryEgorov
I find this script very interesting

I would need it for jpg files
you could edit it for me.

jazz-yCorrect answer
Legend
March 25, 2020

 

 

if (app.documents.length) {
    try { var f = app.activeDocument.fullName } catch (e) {
        var p = new Folder; f = p.selectDlg(); if (f) f = File(f + '/' + app.activeDocument.name)
    }

    if (f) {
       (o = new JPEGSaveOptions ()).quality = 12
       activeDocument.saveAs(createUniqueFileName(f), o)
       activeDocument.close()
    }
}

function createUniqueFileName(f) {
    var inPath = decodeURI(f.parent),
        inFile = decodeURI(f.name).replace(/\.[^\.]+$/, '').replace(/_\d+$/,''),
        inExt = '.jpg',
        uniqueFileName = File(inPath + '/' + inFile + inExt),
        fileNumber = 1

    while (uniqueFileName.exists) {
        uniqueFileName = File(inPath + '/' + inFile + "_" + ('000' + fileNumber).slice(-3) + inExt)
        fileNumber++;
    }
    return uniqueFileName
}

 

 

c.pfaffenbichler
Community Expert
Community Expert
March 24, 2020

You seem to have forgotten to talk about the location. 

S_Gans
Community Expert
Community Expert
March 23, 2020

From what it sounds like, you can create an action, but use the File>Automate>Batch command. Be sure to set the Source to Open Files. 

Your action can use any naming convention you want, when you set it up. 

Actions have a "button mode" option, so you can "hit a button"

 

Hope that helps. 

Adobe Community Expert / Adobe Certified Instructor