21 Replies Latest reply: Aug 23, 2009 7:03 AM by riskypotato RSS

    caption to keywords, minus grammar

    riskypotato Community Member

      Hi all,

      I need a script that will take the caption of a photo (description field of IPTC core), then remove all pronouns, prepositions, articles, conjunctions, plus of course all punctuation, and then place the remaining words into the keywords field. There may be some other words I'll want removed too. I have never written a script and know nothing about writing scripts. I was hoping someone could point me in the right direction. Thanks for any help!

      Phil

        • 1. Re: caption to keywords, minus grammar
          Paul Riggott Community Member

          Here is a start for you.

          You will need to create a dictionary of words, I just created a text file with one word per line IE:

          most 
          much  
          my
          myself
          neither
          no one  
          nobody
          none
          nothing
          one
          one another

           

          NB: You will need to amend the code with the correct path to your dictionary!!!

          The script will show up in the right click menu

           

          #target bridge  
             if( BridgeTalk.appName == "bridge" ) { 
          CreateKeys = new MenuElement("command", "Keys from Description", "at the end of Thumbnail");
          }
          CreateKeys.onSelect = function () {
             keysFromDesc();
             }

          function keysFromDesc(){
          ////////////////////////////////////////////////////////////////////////////////////////// /
          // AMEND PATH TO SUIT!
          var pronouns =File("~/desktop/pronouns.txt");
          ////////////////////////////////////////////////////////////////////////////////////////// //
          if(!pronouns.exists){
            alert("Dictionary does not exit " + pronouns);
            return;
          }
          pronounsList=[];
          pronouns.open("r");
          while(!pronouns.eof){
          var line = pronouns.readln();
          if(line.length>2){
          line=line.replace(/^[ \t]+|[ \t]+$/g,'');
          pronounsList.push(line+" ");
          }
          }
          pronouns.close();
          pronounsList=pronounsList.toString().replace(/,/g,'|');
          var items = app.document.selections;
          var items2 = new Array;
          for (var a =0; a<items.length;a++){
            if(items[a].type == "file") items2.push(items[a]);
            }
          items = items2;

          for (var i = 0; i < items.length; ++i) {
          var item = items[i];
          var md = item.synchronousMetadata;
          var str = md.read("http://purl.org/dc/elements/1.1/","dc:description");
          md.namespace = "http://ns.adobe.com/photoshop/1.0/";
          md.Keywords = formatDesc(str);
          }

          function formatDesc(str){
          str=" "+str+" ";
          str=str.replace (/[\!\"\£\$\%\^\&\*\(\)\:\;\,\'\\\/]/g,'');
          var rex = new RegExp (pronounsList,'gi');
          var keys= str.replace (rex, '').split(' ');
          return  ReturnUniqueSortedList(keys).join(';');
          }

          function ReturnUniqueSortedList(ArrayName){
          var unduped = new Object;
          for (var i = 0; i < ArrayName.length; i++) {  
          unduped[ArrayName[i]] = ArrayName[i];
          }
          var uniques = new Array;
          for (var k in unduped) {
             uniques.push(unduped[k]);
             }
          return uniques.sort();
          }
          }

          • 2. Re: caption to keywords, minus grammar
            riskypotato Community Member

            Paul,

            Wow this is fantastic! Okay, let's say I have this list of words: as, on, in, the, and, or, but (the real list will be much longer), and they are in a folder called "wordsnot" on the root level of my Macintosh HD. Would this be correct?

             

            var pronouns =File("Macintosh HD/wordsnot.txt");

             

            Also, what about punctuation marks in the caption, such as commas and periods?

             

            Many thanks!

             

            Phil

            • 3. Re: caption to keywords, minus grammar
              Paul Riggott Community Member

              Ah I don't think I have have checked for period and cr lf,will do that. Not too sure on the mac filepath as I try to keep well away from my Macs unless I have to. The list at the moment need to be one per line but could be changed if required.

              Paul.

              • 4. Re: caption to keywords, minus grammar
                riskypotato Community Member

                Paul,

                Well, I have composed a "starter" list of words, and inserted it into your code. But, 1, do I now need to make it into a .jsx file, and 2, where do I place this file? As I mentioned, I know nothing about this. Anyway, here's the .txt file, as it stands now:

                 

                the

                a

                it

                and

                but

                on

                over

                under

                near

                around

                as

                they

                he

                she

                it

                aka

                in

                or

                are

                is

                has

                have

                been

                becoming

                his

                her

                hers

                our

                ours

                of

                our

                you

                your

                yours

                their

                theirs

                myself

                yourself

                them

                after

                although

                because

                since

                than

                unless

                until

                when

                while

                both

                either

                neither

                not

                are

                also

                plus

                before

                then

                now

                can

                only

                to

                does

                do

                doing

                how

                • 5. Re: caption to keywords, minus grammar
                  Paul Riggott Community Member

                  As you say you need to make the jsx file, to do this use "ExtendScript Toolkit" this comes with Photoshop and you should find it on your Mac

                  <hard drive>/Applications/Utilities/Adobe Utilities
                  Copy and paste the code into a new window, you can then Start Bridge -
                  Edit - Preferences -Startup Scripts
                  At the bottom click the "Reveal Button" this will open the folder where the script should be placed.

                  Close and restart Bridge so that it will pick up the script and it should be at the bottom of the right click menu.

                  I still haven't checked the mac path yet, but cr/lf and periods are now been checked along with other punctuation.

                  NB: The script only works on selected documents.

                  This new version works with a file with comma delimited words, this is what I used to test it...

                   

                  all,another,any,anybody,anyone,anything,both,each,each other,either,everybody,
                  everyone,everything,few,he,her ,hers ,herself,him,himself,his,it ,its,itself
                  little,many,me,mine,more,most,much,my ,myself,neither ,no one,nobody,
                  none,nothing,one,one another,other,others,our ,ours,ourselves,several,she,some  
                  somebody,someone,something,that,their,theirs,them,themselves,these,these
                  they,this,those,one,us,we,what,whatever,which,whichever,who,whoever,whom,whomever  
                  whose,you,your,yours,yourself,yourselves

                   

                  #target bridge  
                  if( BridgeTalk.appName == "bridge" ) { 
                  CreateKeys = new MenuElement("command", "Keys from Description", "at the end of Thumbnail");
                  }
                  CreateKeys.onSelect = function () {
                     keysFromDesc();
                     }

                  keysFromDesc();
                  function keysFromDesc(){
                  ////////////////////////////////////////////////////////////////////////////////////////// /
                  // AMEND PATH TO SUIT!
                  var pronouns =File("~/desktop/pronouns.txt");
                  ////////////////////////////////////////////////////////////////////////////////////////// //
                  if(!pronouns.exists){
                    alert("Dictionary does not exit " + pronouns);
                    return;
                  }
                  pronounsList=[];
                  pronouns.open("r");
                  while(!pronouns.eof){
                  var line = pronouns.readln();
                  line=line.split(',');
                  for(var a in line){
                  var item =line[a].toString().replace(/^[ \t]+|[ \t]+$/g,'');
                  if(item != ' ' || item != '') pronounsList.push(item+" ");
                  }
                  }
                  pronouns.close();
                  pronounsList = ReturnUniqueSortedList(pronounsList);
                  if(pronounsList[0]== ' ') pronounsList.shift();
                  pronounsList=pronounsList.toString().replace(/,/g,'|');
                  var items = app.document.selections;
                  var items2 = new Array;
                  for (var a =0; a<items.length;a++){
                    if(items[a].type == "file") items2.push(items[a]);
                    }
                  items = items2;

                  for (var i = 0; i < items.length; ++i) {
                  var item = items[i];
                  var md = item.synchronousMetadata;
                  var str = md.read("http://purl.org/dc/elements/1.1/","dc:description");
                  md.namespace = "http://ns.adobe.com/photoshop/1.0/";
                  md.Keywords = formatDesc(str).replace(/^;/,'');
                  }

                  function formatDesc(str){
                  str=" "+str+" ";
                  str=str.replace (/[\!\"\£\$\%\^\&\*\(\)\:\;\.\,\'\\\/]|\n|\r/g,'');
                  var rex = new RegExp (pronounsList,'gi');
                  var keys= str.replace (rex, '').split(' ');
                  return  ReturnUniqueSortedList(keys).join(';');
                  }

                  function ReturnUniqueSortedList(ArrayName){
                  var unduped = new Object;
                  for (var i = 0; i < ArrayName.length; i++) {  
                  unduped[ArrayName[i]] = ArrayName[i];
                  }
                  var uniques = new Array;
                  for (var k in unduped) {
                     uniques.push(unduped[k]);
                     }
                  return uniques.sort();
                  }
                  }

                  • 6. Re: caption to keywords, minus grammar
                    riskypotato Community Member

                    Paul,

                    Once again, many thanks for this tremendous help. In the next few days I'm hoping to apply this script and will keep you informed. It's something I think a lot of photographers might want to use.

                    Phil

                    • 7. Re: caption to keywords, minus grammar
                      riskypotato Community Member

                      Paul,

                      Some problems getting this to work. I copied your script, named my

                      text file "pronouns.txt" and placed it on mydesktop. The error message

                      is attached, the .jsx file is in the correct folder I believe. Not

                      sure if I ran ExtendScript Toolkit correctly: I set the left window

                      for Bridge CS4, copied this text into it, and saved as "pronouns.jsx".

                      Here's the script I made into the .jsx file:

                      #target bridge

                      if( BridgeTalk.appName == "bridge" ) {

                      CreateKeys = new MenuElement("command", "Keys from Description", "at

                      the end of Thumbnail");

                      }

                      CreateKeys.onSelect = function () {

                         keysFromDesc();

                         }

                      keysFromDesc();

                      function keysFromDesc(){

                      var pronouns =File("~/desktop/pronouns.txt");

                      if(!pronouns.exists){

                        alert("Dictionary does not exit " + pronouns);

                        return;

                      }

                      pronounsList=[];

                      pronouns.open("r");

                      while(!pronouns.eof){

                      var line = pronouns.readln();

                      line=line.split(',');

                      for(var a in line){

                      var item =line[a].toString().replace(/^[ \t]|$/g,'');

                      if(item != ' ' || item != '') pronounsList.push(item+" ");

                      }

                      }

                      pronouns.close();

                      pronounsList = ReturnUniqueSortedList(pronounsList);

                      if(pronounsList[0]== ' ') pronounsList.shift();

                      pronounsList=pronounsList.toString().replace(/,/g,'|');

                      var items = app.document.selections;

                      var items2 = new Array;

                      for (var a =0; a<items.length;a++){

                        if(items[a].type == "file") items2.push(items[a]);

                        }

                      items = items2;

                      for (var i = 0; i < items.length; ++i) {

                      var item = items+;

                      var md = item.synchronousMetadata;

                      var str = md.read("http://purl.org/dc/elements/1.1/","dc:description");

                      md.namespace = "http://ns.adobe.com/photoshop/1.0/";

                      md.Keywords = formatDesc(str).replace(/^;/,'');

                      }

                      function formatDesc(str){

                      str=" "str" ";

                      str=str.replace (/[\!\"\£\$\%\^\&\*\(\)\:\;\.\,\'
                      \/]|\n|\r/g,'');

                      var rex = new RegExp (pronounsList,'gi');

                      var keys= str.replace (rex, '').split(' ');

                      return  ReturnUniqueSortedList(keys).join(';');

                      }

                      function ReturnUniqueSortedList(ArrayName){

                      var unduped = new Object;

                      for (var i = 0; i < ArrayName.length; i++) {

                      unduped[ArrayName] = ArrayName;

                      }

                      var uniques = new Array;

                      for (var k in unduped) {

                         uniques.push(unduped[k]);

                         }

                      return uniques.sort();

                      }

                      }

                      • 8. Re: caption to keywords, minus grammar
                        Paul Riggott Community Member

                        What error are you getting? There isn't anything attached in your last post.

                        • 9. Re: caption to keywords, minus grammar
                          riskypotato Community Member

                          hmmmm... attached it to the email but it didn't show up. Anyway:

                           

                          "An error occurred while running a startup script named "pronouns". It

                          may not be compatible with this version of Bridge. You can check for

                          available updates.... etc.... The script has been disabled... etc..."

                           

                          Then, below, it says:

                           

                          "Error in /Users/phil/Library/Application Support/Adobe/Bridge

                          CS4/startup scripts/pronouns.jsx

                          Line 29: var items = app.document.selections; undefined is not an object"

                           

                          That's the whole of it.

                           

                          Thanks for any help!

                          Phil

                          • 10. Re: caption to keywords, minus grammar
                            Paul Riggott Community Member

                            Ooops yes there was a mistake! Sorry! This should now work, I've tested it it in CS3 and CS4.

                             

                            #target bridge  
                            if( BridgeTalk.appName == "bridge" ) { 
                            CreateKeys = new MenuElement("command", "Keys from Description", "at the end of Thumbnail");
                            }
                            CreateKeys.onSelect = function () {
                               keysFromDesc();
                               }
                            function keysFromDesc(){
                            ////////////////////////////////////////////////////////////////////////////////////////// /
                            // AMEND PATH TO SUIT!
                            var pronouns =File("~/desktop/pronouns.txt");
                            ////////////////////////////////////////////////////////////////////////////////////////// //
                            if(!pronouns.exists){
                              alert("Dictionary does not exit " + pronouns);
                              return;
                            }
                            pronounsList=[];
                            pronouns.open("r");
                            while(!pronouns.eof){
                            var line = pronouns.readln();
                            line=line.split(',');
                            for(var a in line){
                            var item =line[a].toString().replace(/^[ \t]+|[ \t]+$/g,'');
                            if(item != ' ' || item != '') pronounsList.push(" "+item+" ");
                            }
                            }
                            pronouns.close();
                            pronounsList = ReturnUniqueSortedList(pronounsList);
                            if(pronounsList[0]== ' ') pronounsList.shift();
                            pronounsList=pronounsList.toString().replace(/,/g,'|');
                            var items = app.document.selections;
                            var items2 = new Array;
                            for (var a =0; a<items.length;a++){
                              if(items[a].type == "file") items2.push(items[a]);
                              }
                            items = items2;

                            for (var i = 0; i < items.length; ++i) {
                            var item = items[i];
                            var md = item.synchronousMetadata;
                            var str = md.read("http://purl.org/dc/elements/1.1/","dc:description");
                            md.namespace = "http://ns.adobe.com/photoshop/1.0/";
                            md.Keywords = formatDesc(str).replace(/^;/,'');
                            }

                            function formatDesc(str){
                            str=" "+str+" ";
                            str=str.replace (/[\!\"\£\$\%\^\&\*\(\)\:\;\.\,\'\\\/]|\n|\r/g,'');
                            var rex = new RegExp (pronounsList,'gi');
                            var keys= str.replace (rex, ' ').split(' ');
                            return  ReturnUniqueSortedList(keys).join(';');
                            }

                            function ReturnUniqueSortedList(ArrayName){
                            var unduped = new Object;
                            for (var i = 0; i < ArrayName.length; i++) {  
                            unduped[ArrayName[i]] = ArrayName[i];
                            }
                            var uniques = new Array;
                            for (var k in unduped) {
                               uniques.push(unduped[k]);
                               }
                            return uniques.sort();
                            }
                            }

                            • 11. Re: caption to keywords, minus grammar
                              riskypotato Community Member

                              Okay, thanks. No error message. But... how do I run it? It shows up as

                              a startup script in the prefs, but I can't find it in any menus.

                              Phil

                              Phil

                              • 12. Re: caption to keywords, minus grammar
                                Paul Riggott Community Member

                                It should be the last entry in the Right Mouse Click menu. It should show as "Keys from Description"

                                 

                                Just select the document you want to run the script on and selected the script from the menu.

                                Hope that helps.

                                • 13. Re: caption to keywords, minus grammar
                                  riskypotato Community Member

                                  Obviously am missing something fundamental. Not sure what is the Mac

                                  equivalent of the right-hand click. I don't see it in any of the

                                  menus. Sorry to be so slow on the uptake. /Phil

                                  • 14. Re: caption to keywords, minus grammar
                                    Paul Riggott Community Member

                                    You can simulate a mouse with a left and right button I think its CTRL/click on a Mac this should give you the right click menu.

                                    • 15. Re: caption to keywords, minus grammar
                                      riskypotato Community Member

                                      Paul,

                                      Indeed it does (I think i was supposed to know that!). However, when I

                                      do that, nothing happens, keyword-wise.

                                      Phil

                                      • 16. Re: caption to keywords, minus grammar
                                        Paul Riggott Community Member

                                        Hi Phil, I wonder if you have selected and documents, as it works on selected documents only?

                                        • 17. Re: caption to keywords, minus grammar
                                          riskypotato Community Member

                                          Hi Paul,

                                          Yeah, had one selected. Does the script work for you? Grateful for all

                                          the time you've put into this.

                                          Phil

                                          • 18. Re: caption to keywords, minus grammar
                                            Paul Riggott Community Member

                                            Yes it works fine with CS3 and CS4 here are a few screen shots.

                                            2009-08-22_183839.jpg

                                            2009-08-22_183906.jpg

                                             

                                            2009-08-22_184152.jpg

                                            It might be worth checking that the script is enabled in the preferences Phil.

                                            • 19. Re: caption to keywords, minus grammar
                                              riskypotato Community Member

                                              Paul,

                                              Hmmm... it's working for you, though have you tried an example where

                                              it removes the pronouns/articles/prepositions?

                                              Still not happening for me, and it is enabled in prefs. Perhaps I ran

                                              ExtendScript ToolKit wrong.

                                              Phil

                                              • 20. Re: caption to keywords, minus grammar
                                                Paul Riggott Community Member

                                                Yes it does remove all words that are in the dictionary. I can't understand why it isn't happening for you unless the text is in a different field?

                                                • 21. Re: caption to keywords, minus grammar
                                                  riskypotato Community Member

                                                  Not sure what you mean by the text being in a different field...?