Resizing and renaming

Anyone, especially newbies, asking for help with photoshop scripting and photoshop automation - as opposed to those contributing to discussion about an aspect of photoshop scripting

Moderators: Patrick, Mike Hale, xbytor, Larry Ligon, Andrew, PS-Moderators

Resizing and renaming

Postby emccainaz » Mon Feb 20, 2012 6:02 pm

Hi,

I'm a newbie to PS Scripting and have been trying to hack my way through this, but am getting stuck and frustrated, so I will turn to the masters for help!

Here's a breakdown of what I need to do:

Take an image or folder full of images (or could I do this from Bridge?). Usually these will all be TIFFs, but in case there is some other format, I want to skip those or get an alert.

I want to open TIFF images with the suffix _m (only "filenames _m.tif") and perform the following actions:

1. Allow a user to use the spot healing brush and crop the image until they are satisfied (it make take one or more tries) Then they can hit a button and allow the rest of the script to run.

2. If the maximum dimension of the image is less than 5000, then do not resize it. Then perform a Save As TIFF without the "_m" suffix.

3. If one maximum dimension of the image is greater than 5000 Use Automate>Fit Image to reduce the largest size of the image to 5000 pixels. The perform a Save As TIFF (Adobe RGB 1998) without the "_m" suffix.

4. Use Automate>Fit Image to reduce the largest size of the image to 1050 pixels. Then perform a Save As JPEG (Level 12, sRGB) with the "_pp" suffix and the extension ".jpg"

5. Use Automate>Fit Image to reduce the largest size of the image to 250 pixels. Then perform a Save As JPEG (Level 12, sRGB) without the "_pp" suffix and adding the extension ".jpg"

This would be a great help.

Thanks,

Edward McCain
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby emccainaz » Mon Mar 12, 2012 4:40 pm

Hi All,

I'm wondering if there is something more I can do to get help with this. Is this a difficult script to write or do I need to post more information? Just curious about why I'm not getting any response to this post.

Thanks,

Edward
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby Mike Hale » Mon Mar 12, 2012 7:45 pm

Sorry you haven't gotten any responses to your question. I guess the other regular members have been as busy as I have been the last few weeks.

You said that you were trying to hack your way through this yet you didn't post any code. Does that mean that you want a script written from scratch or do you need help on the parts where you are stuck?

Having a script stop while the user makes corrections using the spot healing brush will be the biggest challenge. Normally when a script is running the user can not interact with Photoshop.

The other steps are common task and there are lots of example code post here. Most of those steps could also be done with an existing script such as Image Processor Pro.
Mike Hale
Site Admin
 
Posts: 4332
Joined: Fri Sep 30, 2005 10:52 pm
Location: USA

Re: Resizing and renaming

Postby Paul MR » Tue Mar 13, 2012 8:41 pm

Here is the basis of a script, it just need the resizing, saving code to be added.
It requires that the files to be processed are selected in Bridge.

Code: Select all
#target photoshop
app.bringToFront();

function main(){
desc1 = new ActionDescriptor();
UUID = 'c80495b0-6d15-11e1-b0c4-0800200c9a66';
Prefs={};
try{
desc1 = app.getCustomOptions(UUID);
Prefs = eval(desc1.getString(0));
}catch(e){
    //no setting call UI, open first doc, update custom options then return
if (!BridgeTalk.isRunning('bridge')){
    alert("You have not selected any files in Bridge");
    return;
    }
Prefs.fileList = GetFilesFromBridge();
if(Prefs.fileList.length == 0){
    alert("You have not selected any files in Bridge");
    return;
    }
app.open(File(Prefs.fileList.shift()));
desc1.putString(0, Prefs.toSource());
app.putCustomOptions(UUID, desc1, true );
return;
    }
//ok settings exist
if(!documents.length){
    //should not get here, documents closed not using this script
    //open next image, update custom options and return
    if(Prefs.fileList.length >0){
        app.open(File(Prefs.fileList.shift()));
        desc1.putString(0, Prefs.toSource());
        app.putCustomOptions(UUID, desc1, true );
        return;
        }
    }
//ok document is open
//resize, save documents, close, open next, update custom options
alert("your save code goes here");

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    if(Prefs.fileList.length >0){
        app.open(File(Prefs.fileList.shift()));
        desc1.putString(0, Prefs.toSource());
        app.putCustomOptions(UUID, desc1, true );
        return;
        }else{
app.eraseCustomOptions(UUID);
alert("Batch complete");
}
}
main();

function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
   var fileList;
      var bt = new BridgeTalk();
      bt.target = "bridge";
        bt.body = "var ftn = " + script.toSource() + "; ftn();";
      bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
      bt.onError = function( inBT ) { fileList = new Array(); }
      bt.send(8);
      bt.pump();
   if ( undefined == fileList ) fileList = new Array();
   return fileList;
}
Paul MR
 
Posts: 1472
Joined: Wed Oct 18, 2006 2:44 pm
Location: Bradford,UK

Re: Resizing and renaming

Postby emccainaz » Fri Mar 16, 2012 6:10 am

Thanks very much, Paul. Let me take a look at it and see if I can figure out the saving code part. Working from Bridge shouldn't be a problem.

Edward
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby emccainaz » Mon Mar 19, 2012 6:34 pm

So, I'm trying to add code to save as JPEG (copied from another script), but I can't seem to get it to work. I will also need to have a way to save at different sizes and to rename with and without a suffix, as I explained earlier. Here's what I have now:
Code: Select all
    #target photoshop
    app.bringToFront();

    function main(){
    desc1 = new ActionDescriptor();
    UUID = 'c80495b0-6d15-11e1-b0c4-0800200c9a66';
    Prefs={};
    try{
    desc1 = app.getCustomOptions(UUID);
    Prefs = eval(desc1.getString(0));
    }catch(e){
        //no setting call UI, open first doc, update custom options then return
    if (!BridgeTalk.isRunning('bridge')){
        alert("You have not selected any files in Bridge");
        return;
        }
    Prefs.fileList = GetFilesFromBridge();
    if(Prefs.fileList.length == 0){
        alert("You have not selected any files in Bridge");
        return;
        }
    app.open(File(Prefs.fileList.shift()));
    desc1.putString(0, Prefs.toSource());
    app.putCustomOptions(UUID, desc1, true );
    return;
        }
    //ok settings exist
    if(!documents.length){
        //should not get here, documents closed not using this script
        //open next image, update custom options and return
        if(Prefs.fileList.length >0){
            app.open(File(Prefs.fileList.shift()));
            desc1.putString(0, Prefs.toSource());
            app.putCustomOptions(UUID, desc1, true );
            return;
            }
        }
    //ok document is open
    //resize, save documents, close, open next, update custom options
    alert("your save code goes here");
    saveJPEG(saveFile);

    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        if(Prefs.fileList.length >0){
            app.open(File(Prefs.fileList.shift()));
            desc1.putString(0, Prefs.toSource());
            app.putCustomOptions(UUID, desc1, true );
            return;
            }else{
    app.eraseCustomOptions(UUID);
    alert("Batch complete");
    }
    }
    main();

    function GetFilesFromBridge() {
    function script(){
    var fL = app.document.selections;
    var tF=[];
    for(var a in fL){
        if(fL[a].type =='file'){
            tF.push(new File(encodeURI(fL[a].spec.fsName)));
            }
        }
    return tF.toSource();
    }
       var fileList;
          var bt = new BridgeTalk();
          bt.target = "bridge";
            bt.body = "var ftn = " + script.toSource() + "; ftn();";
          bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
          bt.onError = function( inBT ) { fileList = new Array(); }
          bt.send(8);
          bt.pump();
       if ( undefined == fileList ) fileList = new Array();
       return fileList;
    }



function SaveJPEG(saveFile){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;12
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}


Any help will be greatly appreciated.

Edward
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby Paul MR » Mon Mar 19, 2012 7:56 pm

Please try this...

Code: Select all
#target photoshop
app.bringToFront();

function main(){
desc1 = new ActionDescriptor();
UUID = 'c80495b0-6d15-11e1-b0c4-0800200c9a66';
Prefs={};
try{
desc1 = app.getCustomOptions(UUID);
Prefs = eval(desc1.getString(0));
}catch(e){
    //no setting call UI, open first doc, update custom options then return
if (!BridgeTalk.isRunning('bridge')){
    alert("You have not selected any files in Bridge");
    return;
    }
Prefs.fileList = GetFilesFromBridge();
if(Prefs.fileList.length == 0){
    alert("You have not selected any files in Bridge");
    return;
    }
app.open(File(Prefs.fileList.shift()));
desc1.putString(0, Prefs.toSource());
app.putCustomOptions(UUID, desc1, true );
return;
    }
//ok settings exist
if(!documents.length){
    //should not get here, documents closed not using this script
    //open next image, update custom options and return
    if(Prefs.fileList.length >0){
        app.open(File(Prefs.fileList.shift()));
        desc1.putString(0, Prefs.toSource());
        app.putCustomOptions(UUID, desc1, true );
        return;
        }else{
app.eraseCustomOptions(UUID);
alert("Batch complete");
return;
}
    }
//ok document is open
//resize, save documents, close, open next, update custom options
//create a folder to put the processed files
var doc = app.activeDocument;
var saveFile = '';
var outputFolder = Folder(doc.path + "/Processed");
if(!outputFolder.exists) outputFolder.create();
var Name = decodeURI(doc.name).replace(/\.[^\.]+$/, '');
Name = Name.replace(/_m$/i,'');
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var maxPixels = Math.max(doc.width,doc.height);
if(maxPixels > 5000){
    FitImage( 5000, 5000 );
    saveFile = File(outputFolder + "/" + Name + ".tif");
    SaveTIFF(saveFile);
    }else{
        saveFile = File(outputFolder + "/" + Name + ".tif");
        SaveTIFF(saveFile);
        }
FitImage( 1050, 1050 );
saveFile = File(outputFolder + "/" + Name + "_pp.jpg");
SaveJPEG(saveFile, 12);
FitImage( 250, 250 );
saveFile = File(outputFolder + "/" + Name + ".jpg");
SaveJPEG(saveFile, 12);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
app.preferences.rulerUnits = startRulerUnits;
    if(Prefs.fileList.length >0){
        app.open(File(Prefs.fileList.shift()));
        desc1.putString(0, Prefs.toSource());
        app.putCustomOptions(UUID, desc1, true );
        return;
        }else{
app.eraseCustomOptions(UUID);
alert("Batch complete");
}
}
main();

function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
   var fileList;
      var bt = new BridgeTalk();
      bt.target = "bridge";
        bt.body = "var ftn = " + script.toSource() + "; ftn();";
      bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
      bt.onError = function( inBT ) { fileList = new Array(); }
      bt.send(8);
      bt.pump();
   if ( undefined == fileList ) fileList = new Array();
   return fileList;
}
function FitImage( inWidth, inHeight ) {
   if ( inWidth == undefined || inHeight == undefined ) {
      alert( "FitImage requires both Width & Height!");
      return 100;
   }
   var desc = new ActionDescriptor();
   var unitPixels = charIDToTypeID( '#Pxl' );
   desc.putUnitDouble( charIDToTypeID( 'Wdth' ), unitPixels, inWidth );
   desc.putUnitDouble( charIDToTypeID( 'Hght' ), unitPixels, inHeight );
   var runtimeEventID = stringIDToTypeID( "3caa3434-cb67-11d1-bc43-0060b0a13dc4" );   
   executeAction( runtimeEventID, desc, DialogModes.NO );
}
function SaveJPEG(saveFile, jpegQuality){
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpegQuality;
activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
}
function SaveTIFF(saveFile){
tiffSaveOptions = new TiffSaveOptions();
tiffSaveOptions.embedColorProfile = true;
tiffSaveOptions.alphaChannels = true;
tiffSaveOptions.layers = true;
tiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW;
activeDocument.saveAs(saveFile, tiffSaveOptions, true, Extension.LOWERCASE);
}
Paul MR
 
Posts: 1472
Joined: Wed Oct 18, 2006 2:44 pm
Location: Bradford,UK

Re: Resizing and renaming

Postby emccainaz » Wed Mar 21, 2012 5:59 am

Hi Paul,

I'm not sure what is happening here. I have selected TIFF files (Test_01_m.tif, Test_02_m.tif), but I usually get a notice that the Batch is complete, however I don't see any new files in the folder. Sometime the TIFF is opened with the crop tool selected. I'm running the script from the ExtendScript Toolkit, if that matters.

If it simplifies things, let's forget about the cropping and healing brush stuff. I can open the file and do that and then execute the script to do the rest of the work.

Thanks for your help thus far.

Edward
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby Paul MR » Wed Mar 21, 2012 8:10 am

Good morning Edward.
The script as it is at the present is putting the files in a folder called Processed off the selected folder.
If you want the files in the same folder, change...
Code: Select all
var outputFolder = Folder(doc.path + "/Processed");
if(!outputFolder.exists) outputFolder.create();

To:
Code: Select all
var outputFolder = Folder(doc.path);


You can reset the settings before running the script so that you are starting from scratch with this script.
Code: Select all
UUID = 'c80495b0-6d15-11e1-b0c4-0800200c9a66';
app.eraseCustomOptions(UUID);


Hope this helps.
Paul MR
 
Posts: 1472
Joined: Wed Oct 18, 2006 2:44 pm
Location: Bradford,UK

Re: Resizing and renaming

Postby emccainaz » Thu Mar 22, 2012 5:53 am

Hi Paul,

I'm finally getting to the end of the day here and had time to check the script. I've tried making the changes you suggest. The addition of the code
Code: Select all
UUID = 'c80495b0-6d15-11e1-b0c4-0800200c9a66';
app.eraseCustomOptions(UUID);


does seem to make things more consistent, but after the cropping tool comes up, nothing happens. Is there something I need to do to trigger the rest of the action? The Javascript console says that "Execution finished" but there is no Processed folder and there are no derivative files. I've tried running the script from the Toolkit and from within Photoshop with no luck. Perhaps we should do away with the cropping and healing brush and just do the resizing and renaming parts?

Thanks,

Edward
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby Paul MR » Thu Mar 22, 2012 9:57 am

What should happen (first time),
1) Run the script it will create a list of the selected files in Bridge, open the first file.
2) The picture is now open in Photoshop, so do what you need to do
3) Run the script again and it will do the saves and resize. Close the document and open the next document.
4) goto step 2 until all files have been processed
Paul MR
 
Posts: 1472
Joined: Wed Oct 18, 2006 2:44 pm
Location: Bradford,UK

Re: Resizing and renaming

Postby emccainaz » Thu Mar 22, 2012 5:15 pm

Hmm. That's not the behavior I"m seeing.

Right now, I select two images in Bridge. I run the script. The first file opens (no list that I see). I crop the first file and then run the script again, but nothing seems to happen.

I'm running CS4, does that make any difference?
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Re: Resizing and renaming

Postby Paul MR » Fri Mar 23, 2012 9:43 am

I have tested the code with Photoshop CS4 and CS5 and it works without a problem

The only thing I can think of, is you are running the reset code more than once, this you must not do as it removes all data that it requires when you run the script subsequent times.
Paul MR
 
Posts: 1472
Joined: Wed Oct 18, 2006 2:44 pm
Location: Bradford,UK

Re: Resizing and renaming

Postby Mike Hale » Fri Mar 23, 2012 4:43 pm

One thing comes to mind, the custom options are stored in the Photoshop preferences file. On some systems ( such as schools ) the preferences are reset each time Photoshop opens.

So if the OP is not running the reset code more than once it could be a preference file issue. He may need to check with his IT people.
Mike Hale
Site Admin
 
Posts: 4332
Joined: Fri Sep 30, 2005 10:52 pm
Location: USA

Re: Resizing and renaming

Postby emccainaz » Sat Mar 24, 2012 4:19 am

Is the reset code this:
Code: Select all
app.eraseCustomOptions(UUID);

or this:
Code: Select all
UUID = 'c80495b0-6d15-11e1-b0c4-0800200c9a66';
app.eraseCustomOptions(UUID);


If its the second one, I'm only seeing it once. The first statement appears several times in the script.

I'm not running this on an institutional machine right now, so I don't think that is it.

How would I get rid of the spot healing and cropping functions and just leave the resizing and renaming functions, so that I can open the files manually and crop and spot and then just run the script. This would be sufficient to save a lot of time. Or perhaps if I can get that working, we could figure out the other functions?

Many thanks for your help thus far,

Edward
emccainaz
 
Posts: 10
Joined: Fri Feb 10, 2012 6:31 pm

Next

Return to Help Me

Who is online

Users browsing this forum: No registered users and 2 guests