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

Help With a Basic Script

Community Beginner ,
Aug 09, 2018 Aug 09, 2018

Hi, hoping someone can help. I am trying to create a scrip/batch process to crop multiples files, resize to 1280x1280 and then save as a PNG file. I am very new to scripting however after a while of Googling have managed to put together a script from various forums which works on a single file that is open in Photoshop. However I have found some code which I hope will allow me to select a whole folder and batch script all the files inside. I seem to have run into a error and hoping someone can assist.

The scrip runs and browse file window opens but when I select a folder I get the following error.

Error 1233: Expected a reference to an existing File/Folder

Line:6

->     app.open(File(theFiles))

Here is the code I have so far,

#target photoshop

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

if (theFolder) {

    var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);

    for (var m = 0; m < theFiles.length; m++) }

        app.open(File(theFiles))

doc = app.activeDocument;

doc.changeMode(ChangeMode.RGB);

var thumbDim = 1280; // the dimension of the square

// crop to a big square, conditionally, based on dimensions

if (doc.height > doc.width) {

    doc.resizeCanvas(doc.width,doc.width,AnchorPosition.TOPCENTER);

}

else {

    doc.resizeCanvas(doc.height,doc.height,AnchorPosition.MIDDLECENTER);

}

// specify that our units are in pixels, via creation of a UnitValue object

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);

Thanks

TOPICS
Actions and scripting
2.2K
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

LEGEND , Aug 09, 2018 Aug 09, 2018

Change:

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}

to

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);

Change:

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

to

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);}}

Translate
Adobe
LEGEND ,
Aug 09, 2018 Aug 09, 2018

Change:

for (var m = 0; m < theFiles.length; m++) }

to:

for (var m = 0; m < theFiles.length; m++) {

Change:

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);

to

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}

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 Beginner ,
Aug 09, 2018 Aug 09, 2018

Thank you very much for your help. That worked perfectly. I have found some more code which will then save as PNG and rename the files. I just tried it on 3 files however only one file was saved. Do I need to add something to it? Thank you.

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
LEGEND ,
Aug 09, 2018 Aug 09, 2018

You want me to advice for something I don't see?

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 Beginner ,
Aug 09, 2018 Aug 09, 2018

My mistake, apologies. Here is the whole code with the new bit in bold. Thank you.

#target photoshop

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

if (theFolder) {

    var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);

    for (var m = 0; m < theFiles.length; m++) {

        app.open(File(theFiles))

doc = app.activeDocument;

doc.changeMode(ChangeMode.RGB);

var thumbDim = 1280; // the dimension of the square

// crop to a big square, conditionally, based on dimensions

if (doc.height > doc.width) {

    doc.resizeCanvas(doc.width,doc.width,AnchorPosition.TOPCENTER);

}

else {

    doc.resizeCanvas(doc.height,doc.height,AnchorPosition.MIDDLECENTER);

}

// specify that our units are in pixels, via creation of a UnitValue object

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}

// our web export options

var options = new ExportOptionsSaveForWeb();

options.quality = 100;

options.format = SaveDocumentType.PNG;

options.optimized = true;

var newName = 'web-'+doc.name+'.png';

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

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
LEGEND ,
Aug 09, 2018 Aug 09, 2018

Change:

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);}}

to

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);

Change:

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);

to

doc.exportDocument(File(doc.path+'/'+newName),ExportType.SAVEFORWEB,options);}}

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 Beginner ,
Aug 10, 2018 Aug 10, 2018

Thank you very much for your help. It is now working perfectly, really appreciate 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
LEGEND ,
Aug 10, 2018 Aug 10, 2018

Btw you did mistake marking your question as correct answer. Change that as correct one was that from me.

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 ,
Aug 09, 2018 Aug 09, 2018

If you download my crafting actions package there  is a plug-in script in it that will enable you to create an simple action to to that.   The plug-in script can create aspect ratio selection selection or path for documents the are rectangular or elliptical.   So you can record a simple action to do what you want.

Step 1 Menu File>Automate>Aspect Ratio Selection

Step 2 Menu Image>Crop

Aspect Ratio Selection is my plug-ins script in its dialog you would set  a 1 1 aspect ratio, centered, rectangle, Replace, Selection, 0 feather  These settings will be recorded into the action step and used when the action is played no dialog will be displayed.

You would batch this action using menu File>Automate>Image Processor Pro...

Image Processor Pro is a Plug-in Script you can download from the web and install into Photoshop. You can save many type of Image file resized to fit area sizes.

https://sourceforge.net/projects/ps-scripts/files/Image%20Processor%20Pro/v3_2%20betas/

My Crafting Actions Package UPDATED Aug 10, 2014 Added Conditional Action steps to Action Palette Tips.

Contains

Example

Download

JJMack
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 Beginner ,
Aug 10, 2018 Aug 10, 2018

Thank your for the info. Will keep this in mind for future projects.

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 Beginner ,
Aug 10, 2018 Aug 10, 2018

Just curious but how could I achieve a tighter crop on the image. So less of a border around the image? What code would I need to edit. Thanks

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
LEGEND ,
Aug 10, 2018 Aug 10, 2018

Post screenshot of that you want to achieve so I can imagine it fully.

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 Beginner ,
Aug 10, 2018 Aug 10, 2018

Hi here is an example. The first image is what I am looking for. Less of a border with the crop.

1.jpg2.jpg

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 ,
Aug 10, 2018 Aug 10, 2018

If the subject is smaller  width and height are both less than 1280 the document would be trimmed smaller then 1280 by 12880. However,  the script  could the add canvas to make the document 1280 by 1280 the subject have four transparent boarders. If the subject width and height are not both smaller than 1280 the  trimmed document will be  larger than 1280x1280 at least in width or height and will most likely not be square.  The script could then add canvas to make the canvas square and then resize the document 1280x1280 the subject will just have transparent boarders on two side they will be across from each other.

JJMack
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 ,
Aug 10, 2018 Aug 10, 2018

If the boarder is a consistent color you may be able to use Image Trim to trim off the boarders.  Or you may be able to use use the magic want in contiguous mode or quick selection tools to select the boarder then invert the selection to select the subject or use select subject to select the subject once you have that active selection you can crop to the  active selections bounds.

JJMack
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 Beginner ,
Aug 10, 2018 Aug 10, 2018

Hi, there is no border in the images. Would doing this change the size? I want to maintain dimensions of 1280x1280. Thanks

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
LEGEND ,
Aug 10, 2018 Aug 10, 2018

function crp() {

     aD = activeDocument

     displayDialogs = DialogModes.NO

     rulerUnits = preferences.rulerUnits

     preferences.rulerUnits = Units.PIXELS

     function sTT(v) {return stringIDToTypeID(v)}

     res = aD.resolution, h = aD.height, w = aD.width

     dsc1 = new ActionDescriptor(), dsc2 = new ActionDescriptor()

     dsc2.putUnitDouble(sTT('top'), sTT('pixelsUnit'), t = h / 10)

     dsc2.putUnitDouble(sTT('left'), sTT('pixelsUnit'), l = w / 10)

     dsc2.putUnitDouble(sTT('bottom'), sTT('pixelsUnit'), h - t)

     dsc2.putUnitDouble(sTT('right'), sTT('pixelsUnit'), w - l);

     dsc1.putObject(sTT('to'), sTT('rectangle'), dsc2)

     dsc1.putUnitDouble(sTT('angle'), sTT('angleUnit'), 0)

     dsc1.putEnumerated(sTT('cropAspectRatioModeKey'),

     sTT('cropAspectRatioModeClass'), sTT('targetSize'))

     dsc1.putUnitDouble(sTT('width'), sTT('pixelsUnit'), w)

     dsc1.putUnitDouble(sTT('height'), sTT('pixelsUnit'), h)

     dsc1.putUnitDouble(sTT('resolution'), sTT('densityUnit'), res)

     executeAction(sTT('crop'), dsc1), preferences.rulerUnits = rulerUnits

}

crp()

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 Beginner ,
Aug 10, 2018 Aug 10, 2018

Thank you. In relation to the rest of the code. Would I paste this at the start?

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
LEGEND ,
Aug 10, 2018 Aug 10, 2018

Put it after:

doc.resizeImage(UnitValue(thumbDim,"px"),null,null,ResampleMethod.BICUBIC);

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 Beginner ,
Aug 10, 2018 Aug 10, 2018
LATEST

Thanks very much. yes sorry I believe I thanked myself in error.

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