Skip navigation
Currently Being Moderated

Need help Saving file via script with a different file name

Jan 11, 2011 9:28 AM

Hi... new to scripting here, but I think this can be done... just not sure how.

 

I have to run an action on 1000s of images,and then save each file with a slightly modified filename.

 

  • original filename:  12345_1.jpg
  • I need to save it as 12345_3.jpg

 

 

Basically.... from what you see above, I need to change the last digit from a 1 to a 3.

 

I will also be doing this for changing a 2 to a 4

 

  • original filename:  12345_2.jpg
  • I need to save it as 12345_4.jpg

 

 

Any help would be greatly appreciated.

 
Replies
  • Currently Being Moderated
    Jan 12, 2011 12:36 AM   in reply to bfox2718

    You could give this a try:

    // saves jpg into same folder and changes last number 1 to 3 and 2 to 4;
    // be advised: this  overwrites existing jpgs of the same name without prompting. 
    // 2011, use it at your own risk;
    #target photoshop;
    if (app.documents.length > 0) {
    var thedoc = app.activeDocument;
    // getting the name and location;
    var docName = thedoc.name;
    if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
    else {var basename = docName};
    // change number at end of name;
    var lastLetter = basename.slice(basename.length - 1);
    switch (lastLetter) {
    case "1":
    basename = basename.slice(0, basename.length - 1) + "3";
    break;
    case "2":
    basename = basename.slice(0, basename.length - 1) + "4";
    break;
    default:
    break;
    };
    // getting the location, if unsaved save to desktop;
    try {var docPath = thedoc.path}
    catch (e) {var docPath = "~/Desktop"};
    // jpg options;
    var jpegOptions = new JPEGSaveOptions();
    jpegOptions.quality = 9;
    jpegOptions.embedColorProfile = true;
    jpegOptions.matte = MatteType.NONE;
    //save jpg as a copy:
    thedoc.saveAs((new File(docPath+'/'+basename+'.jpg')),jpegOptions,true);
    //that’s it; thanks to xbytor;
    };
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 12, 2011 8:26 AM   in reply to bfox2718

    This should do what you want, make sure you edit the doAction to suit!

     

    
    #target Photoshop
    main();
    function main(){
    var inputFolder= Folder.selectDialog ("Please select folder to process");
    if(inputFolder == null) return;
    var outputFolder= Folder.selectDialog ("Please select output folder");
    var fileList = inputFolder.getFiles("*.jpg");
    for (var a in fileList){
    var f = fileList[a];
    open(f);
    var Name = decodeURI(f.name).replace(/\.[^\.]+$/, '');
    var Ext = decodeURI(f.name).replace(/^.*\./,''); 
    var prefix = Name.match(/^\d+/);
    var suffix = Number(Name.match(/\d+$/))+1;
    var newName = prefix+"_"+suffix+"."+Ext;
    var saveFile = File(outputFolder+"/"+newName);
    ///////////////////////////////////////////////////////////////////////////////////
    //Amend to suit, this is case sensitive!
    doAction("ActionName","ActionSet");
    //////////////////////////////////////////////////////////////////////////
    SaveJPEG(saveFile, 9);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
    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);
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 12, 2011 10:20 AM   in reply to bfox2718

    All you need to do is change :

    var suffix = Number(Name.match(/\d+$/))+1;

    to

    var suffix = Number(Name.match(/\d+$/))+2;

     

    That will then increment the last numbers by 2.

    Hope this helps.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 12, 2011 10:59 AM   in reply to bfox2718

    Please try this now...

    
    #target Photoshop
    main();
    function main(){
    var inputFolder= Folder.selectDialog ("Please select folder to process");
    if(inputFolder == null) return;
    var outputFolder= Folder.selectDialog ("Please select output folder");
    var fileList = inputFolder.getFiles("*.jpg");
    for (var a in fileList){
    var f = fileList[a];
    open(f);
    var Name = decodeURI(f.name).replace(/\.[^\.]+$/, '');
    var Ext = decodeURI(f.name).replace(/^.*\./,''); 
    var prefix = Name.match(/^\d+/);
    var suffix = Number(Name.match(/\d+$/));
    ///////////////////////////////////////////////////////////////////////////////////
    //Amend to suit, this is case sensitive!
    if(suffix == 1 ) doAction("FINAL4","FINAL_4");
    if(suffix == 2 ) doAction("Rough Pencil Shading","Spring 2011");
    suffix += 2;
    //////////////////////////////////////////////////////////////////////////
    var newName = prefix+"_"+suffix+"."+Ext;
    var saveFile = File(outputFolder+"/"+newName);
    SaveJPEG(saveFile, 9);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
    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);
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Jan 12, 2011 11:10 AM   in reply to bfox2718

    The reason you are getting that error is you defined inputFolder inside the function main(). When you define a var inside a function it can only be accesed by the code in that function. You chould add the code for the second action call inside main() Like this

    function main(){
    var inputFolder= Folder.selectDialog ("Please select folder to process","J:\DRUMSFILES");
    if(inputFolder == null) return;
    var outputFolder= Folder.selectDialog ("Please select output folder","J:\DRUMSFILES");
    var fileListPose1 = inputFolder.getFiles("*_1.jpg");
    for (var a in fileListPose1){
    var f1 = fileListPose1[a];
    open(f1);
    var Name = decodeURI(f1.name).replace(/\.[^\.]+$/, '');
    var Ext = decodeURI(f1.name).replace(/^.*\./,'');
    var prefix = Name.match(/^\d+/);
    var suffix = Number(Name.match(/\d+$/))+2;
    var newName = prefix+"_"+suffix+"."+Ext;
    var saveFile = File(outputFolder+"/"+newName);
    ////////////////////////////////////////////////////////////////////// /////////////
    //Amend to suit, this is case sensitive!
    doAction("FINAL4","FINAL_4");
    ////////////////////////////////////////////////////////////////////// ////
    SaveJPEG(saveFile, 9);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    var fileListPose2 = inputFolder.getFiles("*_2.jpg");
    for (var a in fileListPose2){
    var f2 = fileListPose2[a];
    open(f2);
    var Name = decodeURI(f2.name).replace(/\.[^\.]+$/, '');
    var Ext = decodeURI(f2.name).replace(/^.*\./,'');
    var prefix = Name.match(/^\d+/);
    var suffix = Number(Name.match(/\d+$/))+2;
    var newName = prefix+"_"+suffix+"."+Ext;
    var saveFile = File(outputFolder+"/"+newName);
    ////////////////////////////////////////////////////////////////////// /////////////
    //Amend to suit, this is case sensitive!
    doAction("Rough Pencil Shading","Spring 2011");
    ////////////////////////////////////////////////////////////////////// ////
    SaveJPEG(saveFile, 9);
    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
    

    Or define inputFolder outside the function main() so the other code can access.

     

    Or handle both actions in one loop as Paul as done.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 13, 2011 3:30 AM   in reply to bfox2718

    There are most likely much neater math solutions out there for doing this… but I think this should work with your millisecond time gap.

     

    var t = 185000; // ms
     
    var h = Math.floor(t/3600000000);
    var r = t % 3600000000
     
    var m = Math.floor(r/60000);
    var r = r % 60000;
     
    var s = Math.floor(r/1000);
     
    alert('Hours: '+h+' Minutes: '+m+' Seconds: '+s);
    

     

    As for the number of files don't you just want…

     

    alert(fileList.length);
    

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points