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

Script to rename file in Photshop (before naming)

Explorer ,
Jun 19, 2019 Jun 19, 2019

Copy link to clipboard

Copied

Hello everyone ,   I have no knowledge of scripting just wondering if any of you can help.  

I am wanting to create an action that will DUPLICATE the image then FLATTEN then rename the file to {FILENAME}-whatever I want   (before saving )   i want the tab on the top of photoshop to say {FILENAME} -whatever I want ... 

I have no idea how to do the renaming?   can I rename files in photoshop like in Bridge or LR?   

thank you for your help !

TOPICS
Actions and scripting

Views

7.6K

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

correct answers 1 Correct answer

Community Expert , Jun 19, 2019 Jun 19, 2019

Here you go, try this code:

#target photoshop

// Dupe Original File with -Suffix.jsx

var origDoc = app.activeDocument

var baseName = origDoc.name.replace(/\.[^\.]+$/, '');

var sep = String ("-");

var suffix = prompt("Enter Suffix:", "Suffix");

var dupeName = baseName + sep + suffix;

origDoc.duplicate((dupeName), true);

// Optional Step: remove the // comment characters to enable

origDoc.close(SaveOptions.DONOTSAVECHANGES); // (SaveOptions.SAVECHANGES)

Copy the 10 lines of code above, paste into a plain tex

...

Votes

Translate

Translate
Adobe
Community Expert ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

Now that I have more time, I just wanted to post a minor update to include some very basic error checking around the prompt cancel & OK buttons.

 

If cancel is pressed the script will stop, if OK is pressed with a blank value in the prompt field, then the script will stop. I am still looking into adding more complex error checking to return only integer digits for the resize – however, even with the generous Adding a Prompt to a Variable of other forum members, this is easier said than done!

 

// https://forums.adobe.com/message/11130886

#target photoshop

    (function() {

        // Dupe Original File with -Suffix.jsx

        var origDoc = app.activeDocument

        var baseName = origDoc.name.replace(/\.[^\.]+$/, '');

        var sep = String("-");

        var suffix = prompt("Enter Suffix:", "");

        // https://forums.adobe.com/message/11043796

        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.

        var dupeName = baseName + sep + suffix;

        origDoc.duplicate((dupeName), true);

        app.activeDocument.flatten();

        // Optional Step Below: remove the // comment characters to enable saving the original file with or without changes

        // origDoc.close(SaveOptions.DONOTSAVECHANGES); // (SaveOptions.SAVECHANGES)

    })();

 

// https://forums.adobe.com/thread/1031437

// make document square with the document’s longer side’s length;

// 2012, use it at your own risk;

(function() {

    if (app.documents.length > 0) {

        var myDocument = app.activeDocument;

        var originalRulerUnits = preferences.rulerUnits;

        preferences.rulerUnits = Units.PIXELS;

        // set a simple GUI prompt for the longest edge value

        var longestEdge = prompt("Enter Longest Edge Dimension in Pixels:", "");

        // https://forums.adobe.com/message/11043796

        if (longestEdge == null) { return }; // Test if CANCEL returns null, then do nothing. 

        if (longestEdge == "") { return }; // Test if an empty string is returned, then do nothing.

        // get longer side;

        var theFactor = longestEdge / getTheLonger(Number(myDocument.width), Number(myDocument.height));

        // resize;

        myDocument.resizeImage(myDocument.width * theFactor, myDocument.height * theFactor, myDocument.resolution, ResampleMethod.BICUBIC);

        // reset;

        preferences.rulerUnits = originalRulerUnits;

    } else {};

 

    function getTheLonger(a, b) {

        if (a >= b) {

            var x = a

        } else {

            var x = b

        };

        return x;

    };

})();

Votes

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 Expert ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

I have now added some error handling to the value entered into the longest pixel size prompt. So if any non-digit character is entered it will be removed, while decimal values will be converted to an integer (for example, 800.1 px or 800.9 px would be 800 px).

 

// https://forums.adobe.com/message/11130886

#target photoshop

    (function() {

        // Dupe Original File with -Suffix.jsx

        var origDoc = app.activeDocument

        var baseName = origDoc.name.replace(/\.[^\.]+$/, '');

        var sep = String("-");

        var suffix = prompt("Enter Suffix:", "");

        // https://forums.adobe.com/message/11043796

        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

        var dupeName = baseName + sep + suffix;

        origDoc.duplicate((dupeName), true);

        app.activeDocument.flatten();

        // Optional Step Below: remove the // comment characters to enable saving the original file with or without changes

        // origDoc.close(SaveOptions.DONOTSAVECHANGES); // (SaveOptions.SAVECHANGES)

    })();

 

// https://forums.adobe.com/thread/1031437

// make document square with the document’s longer side’s length;

// 2012, use it at your own risk;

(function() {

    if (app.documents.length > 0) {

        var myDocument = app.activeDocument;

        var originalRulerUnits = preferences.rulerUnits;

        preferences.rulerUnits = Units.PIXELS;

        // set a simple GUI prompt for the longest edge value

        var longestEdge = prompt("Enter Longest Edge Dimension in Pixels:", "");

        // https://forums.adobe.com/message/11043796

        if (longestEdge == null) { return }; // Test if CANCEL returns null, then do nothing

        if (longestEdge == "") { return }; // Test if an empty string is returned, then do nothing

        var resizeInput = longestEdge.replace(/[^\d.]/g,'') // Remove anything that is not a digit or decimal point

        if (resizeInput == "") { return }; // Test if an empty string is returned, then do nothing

        // https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/      

        var resizeValue = parseInt(resizeInput, 10); // Assuming that parseInt is more appropriate than parseFloat

        // get longer side

        var theFactor = resizeValue / getTheLonger(Number(myDocument.width), Number(myDocument.height));

        // resize

        myDocument.resizeImage(myDocument.width * theFactor, myDocument.height * theFactor, myDocument.resolution, ResampleMethod.BICUBIC);

        // reset ruler units to original

        preferences.rulerUnits = originalRulerUnits;

    } else {};

 

    function getTheLonger(a, b) {

        if (a >= b) {

            var x = a

        } else {

            var x = b

        };

        return x;

    };

})();

Votes

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
Explorer ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

Awesome, so this is the great and best one?  thanks!

Votes

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 Expert ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

Awesome, so this is the great and best one? thanks!

So far... However I'd like to end up with something similar to this, a combined interface:

scriptUI.png

P.S. It is proving harder than expected to link up the existing code to the scriptUI code elements and have everything work the same as the prompts, two separate prompts are currently easier for me!

Votes

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
Advocate ,
Jul 04, 2019 Jul 04, 2019

Copy link to clipboard

Copied

Script window ui

 

 

 

win = new Window("dialog", "Flatten Dupe, Rename & Resize",undefined, {

closeButton: false

});

 

 

    win.orientation = "column";

    win.alignChildren = ["center","top"];

    win.spacing = 10;

    win.margins = 16;

 

 

var statictext1 = win.add("statictext");

    statictext1.text = "Enter filename suffix";

    statictext1.alignment = ["left","top"];

 

 

var edittext1 = win.add("edittext");

    edittext1.text = "Name";

    edittext1.preferredSize.width = 200;

    edittext1.preferredSize.height = 25;

 

 

var statictext2 = win.add("statictext");

    statictext2.text = "Enter longest edge in pixel";

    statictext2.alignment = ["left","top"];

 

 

var edittext2 = win.add("edittext");

    edittext2.text = "pixel";

    edittext2.preferredSize.width = 200;

    edittext2.preferredSize.height = 25;

   

    baton10 = win.add("group");

 

 

var button1 = baton10.add("button", undefined, undefined, {name:"Cancel"});

    button1.text = "Cancel";

    button1.preferredSize.width = 90;

    button1.justify = "center";

    button1.alignment = ["left","top"];

 

 

var button2 = baton10.add("button", undefined, undefined, {name:"Ok"});

    button2.text = "OK";

    button2.preferredSize.width = 90;

    button2.justify = "center";

    button2.alignment = ["right","top"];

 

 

 

 

button2.onClick = function () {

TEST_A()

close = true;

win.close();

};

 

 

 

 

 

 

win.show();

 

 

// FUNCTION

function TEST_A() { 

 

 

  (function() { 

        // Dupe Original File with -Suffix.jsx 

        var origDoc = app.activeDocument 

        var baseName = origDoc.name.replace(/\.[^\.]+$/, ''); 

        var sep = String("-"); 

        var suffix = edittext1.text

        // https://forums.adobe.com/message/11043796 

        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 

        var dupeName = baseName + sep + suffix; 

        origDoc.duplicate((dupeName), true); 

        app.activeDocument.flatten(); 

        // Optional Step Below: remove the // comment characters to enable saving the original file with or without changes 

        // origDoc.close(SaveOptions.DONOTSAVECHANGES); // (SaveOptions.SAVECHANGES) 

    })(); 

 

// https://forums.adobe.com/thread/1031437 

// make document square with the document’s longer side’s length; 

// 2012, use it at your own risk; 

(function() { 

    if (app.documents.length > 0) { 

        var myDocument = app.activeDocument; 

        var originalRulerUnits = preferences.rulerUnits; 

        preferences.rulerUnits = Units.PIXELS; 

        // set a simple GUI prompt for the longest edge value 

        var longestEdge = "" +  edittext2.text;

        // https://forums.adobe.com/message/11043796 

        if (longestEdge == null) { return }; // Test if CANCEL returns null, then do nothing 

        if (longestEdge == "") { return }; // Test if an empty string is returned, then do nothing 

        var resizeInput = longestEdge.replace(/[^\d.]/g,'') // Remove anything that is not a digit or decimal point 

        if (resizeInput == "") { return }; // Test if an empty string is returned, then do nothing 

        // https://gomakethings.com/converting-strings-to-numbers-with-vanilla-javascript/        

        var resizeValue = parseInt(resizeInput, 10); // Assuming that parseInt is more appropriate than parseFloat 

        // get longer side 

        var theFactor = resizeValue / getTheLonger(Number(myDocument.width), Number(myDocument.height)); 

        // resize 

        myDocument.resizeImage(myDocument.width * theFactor, myDocument.height * theFactor, myDocument.resolution, ResampleMethod.BICUBIC); 

        // reset ruler units to original 

        preferences.rulerUnits = originalRulerUnits; 

    } else {}; 

 

    function getTheLonger(a, b) { 

        if (a >= b) { 

            var x = a 

        } else { 

            var x = b 

        }; 

        return x; 

    }; 

})(); 

 

 

}

Votes

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 Expert ,
Jul 04, 2019 Jul 04, 2019

Copy link to clipboard

Copied

LATEST

Thanks geppettol66959005 that was what I was trying to do!

I'll study your code to see where I was going wrong, I'll probably come back with some questions if I may...

Votes

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
Valorous Hero ,
Jun 20, 2019 Jun 20, 2019

Copy link to clipboard

Copied

There should be no spaces after the "\".
The forum inserts several spaces at the end of each line.
You will have to delete everything manually.

Votes

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
Explorer ,
Jun 20, 2019 Jun 20, 2019

Copy link to clipboard

Copied

I don't see any   \   with spaces 

Votes

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 Expert ,
Jun 19, 2019 Jun 19, 2019

Copy link to clipboard

Copied

1 more thing.... I couldnt find the path to put the file in .... I had to do this via the browse diaglog   .  what is the location ?

 

Prepression: Downloading and Installing Adobe Scripts

 

 

Downloading JavaScript Files

 

Many scripts are offered in .js or .jsx or even .jsxbin formats. Simply download these files ready for installation (in some cases they may need to be decompressed from a .zip format archive).

 

 

Saving JavaScript Source Code

 

Some scripts are offered as “source code”, rather than saved into a ready to use file. This is often the case with scripts found at the Adobe User Forums, GitHub, SourceForge etc. Simply select and copy/paste the script code into a plain text document, saving the file with a .jsx filename extension. Ensure that a double extension is not incorrectly added, such as .jsx.txt

 

NOTE: Only paste the source code into plain text editors such as Notepad or Adobe ExtendScript Toolkit  (WordPad or MS Word are not plain text editors). Ensure that straight single or straight double-quote marks " do not become curly “.

 

source-adobe.png

 

source-github.png

 

source-raw-github.png

 

 

Saving & Compiling AppleScript Source Code

 

Although AppleScript source code is plain text, unlike JavaScript it must be “compiled” before it can be used. Further information at the Apple Developer site:

 

Creating a Script

 

Script Editor


compile.png

 

 

 

Adobe Bridge Script Installation Location

 

A quick way to locate the Startup Scripts folder:

 

  1. Open Adobe Bridge, then open Bridge’s preferences dialog
  2. Under Startup Scripts, click the "Reveal My Startup Scripts" button
  3. Copy/Paste or drag-n-drop your .jsx script files into the Startup Scripts folder/directory
  4. Quit and restart Bridge and answer "Yes" to enable the script.

 

Mac OS Example:

 

/Users/username/Library/Application Support/Adobe/Bridge CC 2018/Startup Scripts

 

Further information at the Adobe site:

Enable startup scripts

 

NOTE: If running, Adobe Bridge must be quit and restarted for newly added scripts to become accessible.

 

 

Adobe InDesign Script Installation Location

 

A quick way to locate the Scripts Panel folder:

 

  1. Right-click (Windows) or Control-click (Mac OS) a script in the Scripts panel
  2. Choose Reveal In Explorer (Windows) or Reveal In Finder (Mac OS)

 

Mac OS Example:

 

/Users/username/Library/Preferences/Adobe InDesign/Version 13.0/en_GB/Scripts/Scripts Panel

 

Further information at the Adobe site:

Scripting

 

NOTE: If running, Adobe InDesign will load newly added scripts without having to quit and restart the application. Scripts can be sorted and stored into operating system sub-folders, which will be reflected in the script panel interface.

 

 

Adobe Photoshop Script Installation Location

 

Scripts are installed in the /Presets/Scripts folder

 

Mac OS Example:

 

/Applications/Adobe Photoshop CC 2018/Presets/Scripts

 

Win OS Example:

 

C:\Program Files\Adobe\Adobe Photoshop CC 2018\Presets\Scripts

 

Alternatively, select File > Scripts > Browse, and navigate to the script file.

 

Further information at the Adobe site:

Scripting

 

NOTE: If running, Adobe Photoshop must be quit and restarted for newly added scripts to become accessible.



Adobe Illustrator Script Installation Location

 

Scripts are installed in the /Presets/Scripts folder

 

Mac OS Example:

 

/Applications/Adobe Illustrator CC 2018/Presets/Scripts

 

Win OS Example:

 

C:\Program Files\Adobe\Adobe Illustrator CC 2018\Presets\Scripts

 

Alternatively, select File > Scripts > Other Script, and navigate to the script file.

 

Further information at the Adobe site:

Automation with scripts

 

NOTE: If running, Adobe Illustrator must be quit and restarted for newly added scripts to become accessible.

Votes

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