• 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.8K

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 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 !

I'm slightly confused...

Lets say you have a file:

originalFILEname.psd

The script will dupe/flatten and rename, should the result be:

originalFILEname-Suffix

or

originalFILEname- Suffix

or

originalFILEname - Suffix

or

{originalFILEname}-Suffix

etc?

Can you please provide more detailed info?

Are you Mac or Win based?

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

Copy link to clipboard

Copied

I'm on Windows 10.

Where is the rename script?

If I use IMAGE > DUPLICATE it will add copy at the end of it. If I change

it then when I run the action it will apply same name to each file. Which

is not what I want.

On Wed, Jun 19, 2019, 5:42 PM Stephen_A_Marsh <forums_noreply@adobe.com>

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

A rename script will need to be written for your exact requirements, which is why answering specific questions is required if somebody is going to help, without wasting time.

Otherwise search the scripting forum and see what you come up with yourself and then the forum can help refine the existing code, however, the forum will still need to know specifics.

Will the suffix be fixed or do you need to type in a new variable suffix each time?

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

Copy link to clipboard

Copied

Oh I see. Sorry about that.

If I have a file that is ORIGINAL.psd

Rename script should be

ORIGINAL-Suffix.psd

Suffix would need to be able to change with whatever I need.

Does that make sense?

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

That does make sense!

Do you wish to keep the original file open, or do you wish to close the original file? If close, save before closing or not?

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

Copy link to clipboard

Copied

Is there a way to do this within Photoshop , or does it require a script?

Carlos Rendon

crendonlds@gmail.com

On Wed, Jun 19, 2019 at 6:38 PM Stephen_A_Marsh <forums_noreply@adobe.com>

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

Is there a way to do this within Photoshop , or does it require a script?

A script is required. The script can be set to use a custom keyboard shortcut, or you can run the script from an action using the action shortcut.

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

Copy link to clipboard

Copied

Yes, I would like to keep the original file open .  

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

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 text editor (not a word processor), then save the plain text file with a .jsx extension, ensuring that it only has .jsx and NOT a double extension such as .jsx.txt

Then drop the file into your application/program folder for Photoshop/Presets/Scripts and restart Photoshop.

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

Copy link to clipboard

Copied

Oh wow! Thank you so much! I am going to try that right now and i will let you know

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

Apologies, remove lines 9 & 10 from my previous post or change line 10 to include the double forward // comment slashes to disable the code:

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

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

Copy link to clipboard

Copied

Ok, so this is what I got :   ..... can we make some changes. please ( I really appreciate your help on this! Thank so much Stephen)!   .... on the dialog can we make it say "Enter Filename"     also ,  (see image)  can we get rid of the word "copy"

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 ?

   2019-06-19 19_41_17-© CR-ELLIE STOCKHAM-058-W1 copy-Suffix @ 25% (RGB_8_) _.jpg

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

Ok, so this is what I got :   ..... can we make some changes. please ( I really appreciate your help on this! Thank so much Stephen)!   .... on the dialog can we make it say "Enter Filename"     also ,  (see image)  can we get rid of the word "copy"

Hmmm, this does not match my results on a Mac using CC2018, I don't get the word copy:

results.png

You can change the prompt text yourself...

Just change the original line 6 from:

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

To:

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

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

Copy link to clipboard

Copied

got it ! Everything works so great! This will help my work flow so much!  Do you have a website or instagram?

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

I used to have a website, it became too much work, I just use Blogger now – but the Google robot keeps flagging my site for Phishing and every few days it gets taken down, then I appeal and Google reply that it is a mistake... This has happened about 10 times over the last month or so. I honestly don't know why/how they think that there is something suspicious going on, but it is impossible to talk to Google/Blogger directly on this issue:

Prepression

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

I forgot to mention, the "prompt" method is quick-n-dirty as I did not have time to use proper scriptUI code, so if you hit cancel the script continues to run, just without the custom suffix text which would be replaced with a "null" value. I am only a beginner at scripting, so I have a long way to go.

P.S. I found that despite the duplicate command including a flattening step, that some files were not flattening, so you may wish to add the following line of code at the end of the script:

app.activeDocument.flatten();

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

Copy link to clipboard

Copied

Hey Stephen  ,  now that I have played with the script ,  is it possible to add a resize to long edge  to the script?

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

Ah, feature creep! I suppose you will end up wanting it to make you a cup of coffee in the morning too! :]

Just kidding...

Anything is possible, however it may not be probable when my scripting level is taken into account.

Search the forums for a script example and I'll let you know!

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

Copy link to clipboard

Copied

There may be a simpler "fit image" script out there, however this was the first one I found, an edit by the late Mike Hale. I just grafted it onto the end of my original script. Enter the same target pixel size into both fields and the longer edge will be resized to the target value, with the shorter edge proportionally scaling.

#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:", "");

var dupeName = baseName + sep + suffix;

origDoc.duplicate((dupeName), true);

app.activeDocument.flatten();

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

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

// c2008 Adobe Systems, Inc. All rights reserved.

// Written by Ed Rose

// based on the ADM Fit Image by Charles A. McBrian from 1997

// edited by Mike Hale added option to avoid resize on images already smaller than target size

/*

@@@BUILDINFO@@@ Fit Image.jsx 1.0.0.21

*/

/* Special properties for a JavaScript to enable it to behave like an automation plug-in, the variable name must be exactly

  as the following example and the variables must be defined in the top 1000 characters of the file

// BEGIN__HARVEST_EXCEPTION_ZSTRING

<javascriptresource>

<name>$$$/JavaScripts/FitImage/Name=Fit Image...</name>

<menu>automate</menu>

<enableinfo>true</enableinfo>

<eventid>3caa3434-cb67-11d1-bc43-0060b0a13dc4</eventid>

<terminology><![CDATA[<< /Version 1

                        /Events <<

                          /3caa3434-cb67-11d1-bc43-0060b0a13dc4 [($$$/AdobePlugin/FitImage/Name=Fit Image) /imageReference <<

                        /width [($$$/AdobePlugin/FitImage/Width=width) /pixelsUnit]

                        /height [($$$/AdobePlugin/FitImage/Height=height) /pixelsUnit]

                        /limit [($$$/AdobePlugin/FitImage/limit=Don't Enlarge) /boolean]

                          >>]

                        >>

                      >> ]]></terminology>

</javascriptresource>

// END__HARVEST_EXCEPTION_ZSTRING

*/

// enable double clicking from the Macintosh Finder or the Windows Explorer

#target photoshop

// debug level: 0-2 (0:disable, 1:break on error, 2:break at beginning)

// $.level = 2;

// debugger; // launch debugger on next line

// on localized builds we pull the $$$/Strings from a .dat file, see documentation for more details

$.localize = true;

var isCancelled = true; // assume cancelled until actual resize occurs

// the main routine

// the FitImage object does most of the work

try {

  // create our default params

  var sizeInfo = new SizeInfo();

  GlobalVariables();

  CheckVersion();

  var gIP = new FitImage();

  if ( DialogModes.ALL == app.playbackDisplayDialogs ) {

        gIP.CreateDialog();

        gIP.RunDialog();

  }

  else {

  gIP.InitVariables();

  ResizeTheImage(sizeInfo.width.value, sizeInfo.height.value);

  }

  if (!isCancelled) {

  SaveOffParameters(sizeInfo);

  }

}

// Lot's of things can go wrong

// Give a generic alert and see if they want the details

catch( e ) {

  if ( DialogModes.NO != app.playbackDisplayDialogs ) {

  alert( e + " : " + e.line );

  }

}

// restore the dialog modes

app.displayDialogs = gSaveDialogMode;

isCancelled ? 'cancel' : undefined;

//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////

function ResizeTheImage(width, height) {

  var oldPref = app.preferences.rulerUnits;

  var docWidth;

  var docHeight;

  var docRatio;

  var newWidth;

  var newHeight;

  var resolution = app.activeDocument.resolution;

  var limit = sizeInfo.limit;

    app.preferences.rulerUnits = Units.PIXELS; // save old preferences

  // original width, height

  docWidth = (1.0 * app.activeDocument.width * resolution) / 72.0; // decimal inches assuming 72 dpi (used in docRatio)

  docHeight = (1.0 * app.activeDocument.height * resolution) / 72.0; // ditto

  if (docWidth < 1.0 || docHeight < 1.0)

  return true; // error

  if (width < 1 || height < 1)

  return true; // error

if ( limit && ( app.activeDocument.width.as('px') <= width && app.activeDocument.height.as('px') <= height ) ){

  app.preferences.rulerUnits = oldPref; // restore old prefs

  isCancelled = false; // if get here, definitely executed

  return false; // no error

  }

  docRatio = docWidth / docHeight; // decimal ratio of original width/height

  newWidth = width;

  newHeight = ((1.0 * width) / docRatio); // decimal calc

  if (newHeight > height) {

  newWidth = docRatio * height; // decimal calc

  newHeight = height;

  }

    // resize the image using a good conversion method while keeping the pixel resolution

    // and the aspect ratio the same

    app.activeDocument.resizeImage(newWidth, newHeight, resolution, ResampleMethod.BICUBIC);

    app.preferences.rulerUnits = oldPref; // restore old prefs

  isCancelled = false; // if get here, definitely executed

  return false; // no error

}

// created in

function SaveOffParameters(sizeInfo) {

  // save off our last run parameters

  var d = objectToDescriptor(sizeInfo, strMessage);

  app.putCustomOptions("3caa3434-cb67-11d1-bc43-0060b0a13dc4", d);

  app.playbackDisplayDialogs = DialogModes.ALL;

  // save off another copy so Photoshop can track them corectly

  var dd = objectToDescriptor(sizeInfo, strMessage);

  app.playbackParameters = dd;

}

function GlobalVariables() {

  // a version for possible expansion issues

  gVersion = 1.1;

  gMaxResize = 300000;

  // remember the dialog modes

  gSaveDialogMode = app.displayDialogs;

  app.displayDialogs = DialogModes.NO;

    gInAlert = false;

  // all the strings that need to be localized

  strTitle = localize( "$$$/JavaScript/FitImage/Title=Fit Image" );

  strConstrainWithin = localize( "$$$/JavaScript/FitImage/ConstrainWithin=Constrain Within" );

  strTextWidth = localize("$$$/JavaScripts/FitImage/Width=&Width:");

  strTextHeight = localize("$$$/JavaScripts/FitImage/Height=&Height:");

  strTextPixels = localize("$$$/JavaScripts/FitImage/Pixels=pixels");

  strButtonOK = localize("$$$/JavaScripts/FitImage/OK=OK");

  strButtonCancel = localize("$$$/JavaScripts/FitImage/Cancel=Cancel");

  strTextSorry = localize("$$$/JavaScripts/FitImage/Sorry=Sorry, Dialog failed");

  strTextInvalidType = localize("$$$/JavaScripts/FitImage/InvalidType=Invalid numeric value");

  strTextInvalidNum = localize("$$$/JavaScripts/FitImage/InvalidNum=A number between 1 and 300000 is required. Closest value inserted.");

  strTextNeedFile = localize("$$$/JavaScripts/FitImage/NeedFile=You must have a file selected before using Fit Image");

  strMessage = localize("$$$/JavaScripts/FitImage/Message=Fit Image action settings");

  strMustUse = localize( "$$$/JavaScripts/ImageProcessor/MustUse=You must use Photoshop CS 2 or later to run this script!" );

  strLimitResize = localize("$$$/JavaScripts/FitImage/Limit=Don^}t Enlarge");

}

// the main class

function FitImage() {

  this.CreateDialog = function() {

  // I will keep most of the important dialog items at the same level

  // and use auto layout

  // use overriding group so OK/Cancel buttons placed to right of panel

  var res =

  "dialog { \

  pAndB: Group { orientation: 'row', \

  info: Panel { orientation: 'column', borderStyle: 'sunken', \

  text: '" + strConstrainWithin +"', \

  w: Group { orientation: 'row', alignment: 'right',\

  s: StaticText { text:'" + strTextWidth +"' }, \

  e: EditText { preferredSize: [70, 20] }, \

  p: StaticText { text:'" + strTextPixels + "'} \

  }, \

  h: Group { orientation: 'row', alignment: 'right', \

  s: StaticText { text:'" + strTextHeight + "' }, \

  e: EditText { preferredSize: [70, 20] }, \

  p: StaticText { text:'" + strTextPixels + "'} \

  }, \

  l: Group { orientation: 'row', alignment: 'left', \

  c:Checkbox { text: '" + strLimitResize + "', value: false }, \

  } \

  }, \

  buttons: Group { orientation: 'column', alignment: 'top',  \

  okBtn: Button { text:'" + strButtonOK +"', properties:{name:'ok'} }, \

  cancelBtn: Button { text:'" + strButtonCancel + "', properties:{name:'cancel'} } \

  } \

  } \

  }";

  // the following, when placed after e: in w and h doesn't show up

  // this seems to be OK since px is put inside the dialog box

  //p: StaticText { text:'" + strTextPixels + "'}

  // create the main dialog window, this holds all our data

  this.dlgMain = new Window(res,strTitle);

  // create a shortcut for easier typing

  var d = this.dlgMain;

    // match our dialog background color to the host application

        d.graphics.backgroundColor = d.graphics.newBrush (d.graphics.BrushType.THEME_COLOR, "appDialogBackground");

  d.defaultElement = d.pAndB.buttons.okBtn;

  d.cancelElement = d.pAndB.buttons.cancelBtn;

  } // end of CreateDialog

  // initialize variables of dialog

  this.InitVariables = function() {

  var oldPref = app.preferences.rulerUnits;

  app.preferences.rulerUnits = Units.PIXELS;

  // look for last used params via Photoshop registry, getCustomOptions will throw if none exist

  try {

  var desc = app.getCustomOptions("3caa3434-cb67-11d1-bc43-0060b0a13dc4");

  descriptorToObject(sizeInfo, desc, strMessage);

  }

  catch(e) {

  // it's ok if we don't have any options, continue with defaults

  }

  // see if I am getting descriptor parameters

  var fromAction = !!app.playbackParameters.count;

  if( fromAction ){

  // reset sizeInfo to defaults

  SizeInfo = new SizeInfo();

  // set the playback options to sizeInfo

  descriptorToObject(sizeInfo, app.playbackParameters, strMessage);

  }

  // make sure got parameters before this

  if (app.documents.length <= 0) // count of documents viewed

  {

  if ( DialogModes.NO != app.playbackDisplayDialogs ) {

  alert(strTextNeedFile); // only put up dialog if permitted

  }

  app.preferences.rulerUnits = oldPref;

  return false; // if no docs, always return

  }

  var w = app.activeDocument.width;

  var h = app.activeDocument.height;

  var l = true;

  if (sizeInfo.width.value == 0) {

  sizeInfo.width = w;

  }

  else {

  w = sizeInfo.width;

  }

  if (sizeInfo.height.value == 0) {

  sizeInfo.height = h;

  }

  else {

  h = sizeInfo.height;

  }

  app.preferences.rulerUnits = oldPref;

  if ( DialogModes.ALL == app.playbackDisplayDialogs ) {

  var d = this.dlgMain;

  d.ip = this;

  d.pAndB.info.w.e.text = Number(w);

  d.pAndB.info.h.e.text = Number(h);

  d.pAndB.info.l.c.value = sizeInfo.limit;

  }

  return true;

  }

  // routine for running the dialog and it's interactions

  this.RunDialog = function () {

  var d = this.dlgMain;

  // in case hit cancel button, don't close

  d.pAndB.buttons.cancelBtn.onClick = function() {

  var dToCancel = FindDialog( this );

  dToCancel.close( false );

  }

  // nothing for now

  d.onShow = function() {

  }

  // do not allow anything except for numbers 0-9

  d.pAndB.info.w.e.addEventListener ('keydown', NumericEditKeyboardHandler);

  // do not allow anything except for numbers 0-9

  d.pAndB.info.h.e.addEventListener ('keydown', NumericEditKeyboardHandler);

  // hit OK, do resize

  d.pAndB.buttons.okBtn.onClick = function () {

            if (gInAlert == true) {

                gInAlert = false;

                return;

            }

            var wText = d.pAndB.info.w.e.text;

            var hText = d.pAndB.info.h.e.text;

            var lValue = d.pAndB.info.l.c.value;

            var w = Number(wText);

            var h = Number(hText);

            sizeInfo.limit = Boolean(lValue);

            var inputErr = false;

            if ( isNaN( w ) || isNaN( h ) ) {

                if ( DialogModes.NO != app.playbackDisplayDialogs ) {

                    alert( strTextInvalidType );

                }

                if (isNaN( w )) {

                    sizeInfo.width = new UnitValue( 1, "px" );

                    d.pAndB.info.w.e.text = 1;

                } else {

                    sizeInfo.height = new UnitValue( 1, "px" );

                    d.pAndB.info.h.e.text = 1;

                }

                    return false;

            }

            else if (w < 1 || w > gMaxResize || h < 1 || h > gMaxResize) {

                if ( DialogModes.NO != app.playbackDisplayDialogs ) {

                    gInAlert = true;

                    alert( strTextInvalidNum );

                }

            }

            if ( w < 1) {

  inputErr = true;

  sizeInfo.width = new UnitValue( 1, "px" );

  d.pAndB.info.w.e.text = 1;

  }

            if ( w > gMaxResize) {

  inputErr = true;

  sizeInfo.width = new UnitValue( gMaxResize, "px" );

  d.pAndB.info.w.e.text = gMaxResize;

            }

            if ( h < 1) {

  inputErr = true;

  sizeInfo.height = new UnitValue( 1, "px" );

  d.pAndB.info.h.e.text = 1;

            }

            if ( h > gMaxResize) {

  inputErr = true;

  sizeInfo.height = new UnitValue( gMaxResize, "px" );

  d.pAndB.info.h.e.text = gMaxResize;

            }

            if (inputErr == false)  {

                sizeInfo.width = new UnitValue( w, "px" );

                sizeInfo.height = new UnitValue( h, "px" );

                if (ResizeTheImage(w, h)) { // the whole point

                    // error, input or output size too small

                }

                d.close(true);

            }

  return;

  }

  if (!this.InitVariables())

  {

  return true; // handled it

  }

  // give the hosting app the focus before showing the dialog

  app.bringToFront();

  this.dlgMain.center();

  return d.show();

  }

}

function CheckVersion() {

  var numberArray = version.split(".");

  if ( numberArray[0] < 9 ) {

  if ( DialogModes.NO != app.playbackDisplayDialogs ) {

  alert( strMustUse );

  }

  throw( strMustUse );

  }

}

function FindDialog( inItem ) {

  var w = inItem;

  while ( 'dialog' != w.type ) {

  if ( undefined == w.parent ) {

  w = null;

  break;

  }

  w = w.parent;

  }

  return w;

}

///////////////////////////////////////////////////////////////////////////////

// Function: objectToDescriptor

// Usage: create an ActionDescriptor from a JavaScript Object

// Input: JavaScript Object (o)

//        object unique string (s)

//        Pre process converter (f)

// Return: ActionDescriptor

// NOTE: Only boolean, string, number and UnitValue are supported, use a pre processor

//      to convert (f) other types to one of these forms.

// REUSE: This routine is used in other scripts. Please update those if you

//        modify. I am not using include or eval statements as I want these

//        scripts self contained.

///////////////////////////////////////////////////////////////////////////////

function objectToDescriptor (o, s, f) {

  if (undefined != f) {

  o = f(o);

  }

  var d = new ActionDescriptor;

  var l = o.reflect.properties.length;

  d.putString( app.charIDToTypeID( 'Msge' ), s );

  for (var i = 0; i < l; i++ ) {

  var k = o.reflect.properties.toString();

  if (k == "__proto__" || k == "__count__" || k == "__class__" || k == "reflect")

  continue;

  var v = o[ k ];

  k = app.stringIDToTypeID(k);

  switch ( typeof(v) ) {

  case "boolean":

  d.putBoolean(k, v);

  break;

  case "string":

  d.putString(k, v);

  break;

  case "number":

  d.putDouble(k, v);

  break;

  default:

  {

  if ( v instanceof UnitValue ) {

  var uc = new Object;

  uc["px"] = charIDToTypeID("#Pxl"); // pixelsUnit

  uc["%"] = charIDToTypeID("#Prc"); // unitPercent

  d.putUnitDouble(k, uc[v.type], v.value);

  } else {

  throw( new Error("Unsupported type in objectToDescriptor " + typeof(v) ) );

  }

  }

  }

  }

    return d;

}

///////////////////////////////////////////////////////////////////////////////

// Function: descriptorToObject

// Usage: update a JavaScript Object from an ActionDescriptor

// Input: JavaScript Object (o), current object to update (output)

//        Photoshop ActionDescriptor (d), descriptor to pull new params for object from

//        object unique string (s)

//        JavaScript Function (f), post process converter utility to convert

// Return: Nothing, update is applied to passed in JavaScript Object (o)

// NOTE: Only boolean, string, number and UnitValue are supported, use a post processor

//      to convert (f) other types to one of these forms.

// REUSE: This routine is used in other scripts. Please update those if you

//        modify. I am not using include or eval statements as I want these

//        scripts self contained.

///////////////////////////////////////////////////////////////////////////////

function descriptorToObject (o, d, s, f) {

  var l = d.count;

  if (l) {

    var keyMessage = app.charIDToTypeID( 'Msge' );

        if ( d.hasKey(keyMessage) && ( s != d.getString(keyMessage) )) return;

  }

  for (var i = 0; i < l; i++ ) {

  var k = d.getKey(i); // i + 1 ?

  var t = d.getType(k);

  strk = app.typeIDToStringID(k);

  switch (t) {

  case DescValueType.BOOLEANTYPE:

  o[strk] = d.getBoolean(k);

  break;

  case DescValueType.STRINGTYPE:

  o[strk] = d.getString(k);

  break;

  case DescValueType.DOUBLETYPE:

  o[strk] = d.getDouble(k);

  break;

  case DescValueType.UNITDOUBLE:

  {

  var uc = new Object;

  uc[charIDToTypeID("#Rlt")] = "px"; // unitDistance

  uc[charIDToTypeID("#Prc")] = "%"; // unitPercent

  uc[charIDToTypeID("#Pxl")] = "px"; // unitPixels

  var ut = d.getUnitDoubleType(k);

  var uv = d.getUnitDoubleValue(k);

  o[strk] = new UnitValue( uv, uc[ut] );

  }

  break;

  case DescValueType.INTEGERTYPE:

  case DescValueType.ALIASTYPE:

  case DescValueType.CLASSTYPE:

  case DescValueType.ENUMERATEDTYPE:

  case DescValueType.LISTTYPE:

  case DescValueType.OBJECTTYPE:

  case DescValueType.RAWTYPE:

  case DescValueType.REFERENCETYPE:

  default:

  throw( new Error("Unsupported type in descriptorToObject " + t ) );

  }

  }

  if (undefined != f) {

  o = f(o);

  }

}

///////////////////////////////////////////////////////////////////////////////

// Function: SizeInfo

// Usage: object for holding the dialog parameters

// Input: <none>

// Return: object holding the size info

///////////////////////////////////////////////////////////////////////////////

function SizeInfo() {

    this.height = new UnitValue( 0, "px" );

    this.width = new UnitValue( 0, "px" );

    this.limit = false;

}

///////////////////////////////////////////////////////////////////////////////

// Function: NumericEditKeyboardHandler

// Usage: Do not allow anything except for numbers 0-9

// Input: ScriptUI keydown event

// Return: <nothing> key is rejected and beep is sounded if invalid

///////////////////////////////////////////////////////////////////////////////

function NumericEditKeyboardHandler (event) {

    try {

        var keyIsOK = KeyIsNumeric (event) ||

  KeyIsDelete (event) ||

  KeyIsLRArrow (event) ||

  KeyIsTabEnterEscape (event);

        if (! keyIsOK) {

            //    Bad input: tell ScriptUI not to accept the keydown event

            event.preventDefault();

            /*    Notify user of invalid input: make sure NOT

        to put up an alert dialog or do anything which

                  requires user interaction, because that

                  interferes with preventing the 'default'

                  action for the keydown event */

            app.beep();

        }

    }

    catch (e) {

        ; // alert ("Ack! bug in NumericEditKeyboardHandler: " + e);

    }

}

//    key identifier functions

function KeyHasModifier (event) {

    return event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;

}

function KeyIsNumeric (event) {

    return  (event.keyName >= '0') && (event.keyName <= '9') && ! KeyHasModifier (event);

}

function KeyIsDelete (event) {

    //    Shift-delete is ok

    return ((event.keyName == 'Backspace') || (event.keyName == 'Delete')) && ! (event.ctrlKey);

}

function KeyIsLRArrow (event) {

    return ((event.keyName == 'Left') || (event.keyName == 'Right')) && ! (event.altKey || event.metaKey);

}

function KeyIsTabEnterEscape (event) {

  return event.keyName == 'Tab' || event.keyName == 'Enter' || event.keyName == 'Escape';

}

// End Fit Image.jsx

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

Copy link to clipboard

Copied

Or this one may work too: Scripting Fit Image Command

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

Thanks stephen  so the one with the link didn't work well for me, 

I am wanting a dialog to come up (like on the naming one) as well that says "How many pixels do you want the long edge to be?   I would like to avoid the Fit Image Dialog altogether ,  

also when I tried the super long code you gave me , it gave me this error.   2019-06-20 10_22_34-Adobe Photoshop CC 2019.jpg

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

Copy link to clipboard

Copied

The modified Adobe Fit Image script code that I found worked fine in CC2018, I can understand the error message but not sure why you get it (version issue, copy/paste issue etc). Not worth bothering with if you wish to avoid fit image anyway in preference of a single field.

The link above uses a hard-coded resolution for the longest edge, which needs to be swapped out for a variable and a prompt or a proper GUI entry field.

As I said earlier, if you can find the code you like for the longest edge resize, then it should just be a case of adding at the end of my script.

EDIT: Try this code, it is basic with no error checking, but it should get the job done –

#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:", "");

var dupeName = baseName + sep + suffix;

origDoc.duplicate((dupeName), true);

app.activeDocument.flatten();

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

// 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;

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:", "");

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

Copy link to clipboard

Copied

It works! Thank you so much!

Just 37 lines

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

Copy link to clipboard

Copied

Glad it works for you!

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