2 Replies Latest reply on Oct 5, 2012 2:44 AM by CaraShop

    how to rename a file based on XMP values?

    CaraShop

      I have a files with multiple IDs in the keyword field of information tab.

       

      I want to rename the file with the values I have in the fields.

       

      For example, with file1.psd:

      in keywords I have: 36607675; 36607675; 20999012; 20999013;

      I want to have 5 files at the end :

      36607675.psd

      36607675.psd

      20999012.psd

      20999013.psd

      file1.psd

       

      I tried to create a script by using ScriptingListener plugin, but it didn't works and the code generated is a little be hard to understand. He doesn't take care of the metadata of the other files...

       

      After that i tried to find some tutorials but evryone try to add XMP data, not retrieve it for use them.

       

       

      Thanks in advance for your help and best regards

        • 1. Re: how to rename a file based on XMP values?
          Paul Riggott Level 6

          This should do a folder of PSD's....

           

          It requires Photoshop CS4 or newer!

           

           

          #target photoshop
          app.bringToFront();
          main();
          function main(){
          var selectedFolder = Folder.selectDialog( "Please select source folder"); 
          if(selectedFolder == null ) return;
          var fileList = selectedFolder.getFiles("*.psd");
          for(var a in fileList){
          var Keys = getKeys( fileList[a]);
          for(var k in Keys){
              var copyName = Keys[k] + ".psd";
              fileList[a].copy(File(selectedFolder + "/" + copyName));
              }
              }
          }
          function getKeys( file){
          if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
                  var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ );
                  var xmp = xmpf.getXMP();
                  var arrItem=[];
              var items =  xmp.countArrayItems(XMPConst.NS_DC, "subject");
              for(var i = 1;i <= items;i++){
                  arrItem.push(xmp.getArrayItem(XMPConst.NS_DC, "subject", i));
               }      
           return arrItem;
          }
          
           
          
          
          • 2. Re: how to rename a file based on XMP values?
            CaraShop Level 1

            Hi Paul,

             

            It works great thanks a lot!

             

            I add some condition in order to know which files are correctly processed and which are not.

             

            #target photoshop

            app.bringToFront();

            main();

            function main(){

                var i = 0;

            var selectedFolder = Folder.selectDialog( "Please select source folder");

            if(selectedFolder == null ) return;

            var fileList = selectedFolder.getFiles("*.eps");

            for(var a in fileList){

            var Keys = getKeys( fileList[a]);

            var name=fileList[a].name;

            for(var k in Keys){

                var test=Keys[k];

                var copyName = Keys[k] + ".eps";

                if (isNaN(test) == false){

                    fileList[a].copy(File(selectedFolder + "/Processed/" + copyName));

                    i=i+1;

                    }

                if (isNaN(test) == true){

                    fileList[a].copy(File(selectedFolder + "/Processed/" + name));

                    i=i+1;

                    }

            }

                if (i>0) {

                    fileList[a].copy(File(selectedFolder + "/OK/" + name));

                    }

                if (i==0){

                    fileList[a].copy(File(selectedFolder + "/NOK/" + name));

                    }

                }

            }

            function getKeys( file){

            if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');

                    var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ );

                    var xmp = xmpf.getXMP();

                    var arrItem=[];

                var items =  xmp.countArrayItems(XMPConst.NS_DC, "subject");

                for(var i = 1;i <= items;i++){

                    arrItem.push(xmp.getArrayItem(XMPConst.NS_DC, "subject", i));

                 }     

            return arrItem;

            }