Photoshop CS - Add Keyword to Flagged or Selected Files

Upload Photoshop Scripts, download Photoshop Scripts, Discussion and Support of Photoshop Scripts

Moderators: Tom, Kukurykus

Andrew

Photoshop CS - Add Keyword to Flagged or Selected Files

Post by Andrew »

This CS script will add a keyword (eg Flag1) too all files either selected or flagged in the Photoshop file browser.

The value in having this is that different selection sets can be remembered (eg favourite, print, web etc), and also some external applications (eg QImage) can use keywords for the selection of files to be printed. The script was written following a request on the QImage forum for some way to recognise flagged PS files in QImage.

The script will add the user chosen Keyword to the selected (or flaged - depending on which radio-button gets ticked) images and will remove the Keyword from all images not selected. Because of the Keyword removal step it must open all qualifying images in the folder - it is fast on my P4 3.0 GHz but might be less so on a slower system.

Download the script here:

bb/files/ah-flag.zip

Installation and usage instructions are in the file ah-flag.js that should be opened in a text editor eg Notepad. Do NOT open in MS Word or any other word processor as it will break the code.

Andrew

PS

There is another non-scripting method that can be used to do the same thing based on 'Append Metadata' and 'Replace Metadata' in the file browser - see thread in General Discussion forum. This may be faster but is a little less easy to use.
Andrew

Photoshop CS - Add Keyword to Flagged or Selected Files

Post by Andrew »

This is the raw code - I am always open to suggestions for code improvement

Code: Select allvar StartDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;

function fbSelect( flagged ) {
   var fileArray = new Array();
   var ffIndex = 0;
   var ref = new ActionReference();
   var fileBrowserStrID = stringIDToTypeID( "fileBrowser" )
   ref.putProperty( charIDToTypeID( 'Prpr' ), fileBrowserStrID );
   ref.putEnumerated( charIDToTypeID( 'capp' ), charIDToTypeID( 'Ordn' ), charIDToTypeID( 'Trgt' ) );
   var desc = executeActionGet( ref );
   if ( desc.count > 0 && desc.hasKey( fileBrowserStrID ) ) {
      var fbDesc = desc.getObjectValue( fileBrowserStrID );
      var keyFilesList = charIDToTypeID( 'flst' );
      if ( fbDesc.count > 0 && fbDesc.hasKey( keyFilesList ) ) {
         var fileList = fbDesc.getList( keyFilesList );
         var flaggedID = stringIDToTypeID( "flagged" );
         var selectedID = charIDToTypeID( 'fsel' );
         var keyPath = charIDToTypeID( 'Path' );
         for ( var i = 0; i < fileList.count; i++ ) {
            var fileDesc = fileList.getObjectValue( i );
            if ( fileDesc.count > 0 && fileDesc.hasKey( keyPath )) {
               if ( flagged == true && fileDesc.hasKey( flaggedID ) && fileDesc.getBoolean( flaggedID )) {
                  var fileOrFolder = fileDesc.getPath( keyPath );
                  if ( fileOrFolder instanceof File ) {
                     fileArray[ffIndex++] = fileOrFolder;
                  }
               }
               else if ( flagged == false && fileDesc.hasKey( selectedID ) && fileDesc.getBoolean( selectedID )) {
                  var fileOrFolder = fileDesc.getPath( keyPath );
                  if ( fileOrFolder instanceof File ) {
                     fileArray[ffIndex++] = fileOrFolder;
                  }
               }
            }
         }
      }
   }
   return fileArray;
}

function deleteKey (keys, dKey)
{
   var nkeys = new Array ();
   for (var i = 0; i < keys.length; i++)
   {
      if (keys == dKey) continue;
      nkeys.push(keys);   
   }
   return nkeys;
}

function addKey (keys, aKey)
{
   for (var i = 0; i < keys.length; i++)
   {
      if (keys == aKey) return keys;
   }
   keys.push(aKey);
   return keys;
}

function getFilesByExt (tFolder)
{
   var pFiles = new Array();
   var filterAr = new Array ('tif','jpg','psd');
   var fFiles = tFolder.getFiles();
   for (var i = 0; i < fFiles.length; i++)
   {
      if (fFiles instanceof Folder) continue;
      var dotPos = fFiles.name.lastIndexOf( "." );
      if (dotPos < 1) continue;
      var process = false;
      fExtLc = fFiles.name.substring(dotPos+1).toLowerCase();
      for (var y = 0; y < filterAr.length; y++)
      {
         process =(filterAr[y] == fExtLc);
         if (process == true) break;
      }
      if (process == false) continue;// failed extension test
      pFiles.push(fFiles);
   }
   return pFiles;   
}


function main()
{
   var aSfxDlg =
         "dialog { text: 'AH - Flag to Keywords', bounds:[100,50,250,155], \
         fbSel: RadioButton { text:'FB Sel', value:'true', bounds:[10, 5, 70, 25]}, \
         fbFl: RadioButton { text:'FB Flag', bounds:[75, 5, 150, 25]}, \
         keySt: StaticText { text:'Flag', bounds:[10, 32, 40, 50]},  \
         fkey: EditText { text:'Flag1', bounds:[45, 30, 140, 50]},  \
         runBtn: Button { text:'Run', bounds:[10, 55, 140, 75], properties:{name:'ok'}}, \
         cancelBtn: Button { text:'Cancel', bounds:[10, 80, 140, 100], properties:{name:'cancel'}} \
   }";
   var aSfx = new Window(aSfxDlg);
   var doReview = aSfx.show();
   if (doReview == 2) return; // cancel button
   var fKey = aSfx.fkey.text;
   var procFiles = fbSelect(aSfx.fbFl.value);
   if (procFiles.length == 0) {alert('No Files Selected or Flagged');return;}
   var sourcePath = procFiles[0].path;
   var sFolder = new Folder (sourcePath);
   var folderFiles = getFilesByExt(sFolder);
   for (var i = 0; i < folderFiles.length; i=i+1)
   {
      open(folderFiles);
      var selected = false;
      var keys = app.activeDocument.info.keywords;
      for (var j = 0; j < procFiles.length; j=j+1)
      {
         if (procFiles[j].name == folderFiles.name)
         {
            selected = true;
            keys = addKey (keys, fKey);
            break;
         }         
      }
      if (selected == false) keys = deleteKey (keys, fKey);
      app.activeDocument.info.keywords = keys;
      app.activeDocument.close(SaveOptions.SAVECHANGES);
   }
}

main();
app.displayDialogs = StartDisplayDialogs;

Andrew