4 Replies Latest reply on Oct 19, 2015 3:03 PM by Alexander Ladygin

    Add keywords with script - XMP

    LeoMari Level 2

      Hello everyone!

       

      I need a script to add words in my keywords. It's possible?

      I'm trying this:

       

      activeDocument.XMPString ("ok Script");

      ActiveDocument.Save (); // dont work

       

      Thank cooperation!

        • 1. Re: Add keywords with script - XMP
          Alexander Ladygin Level 2

          function save(doc, path){

              try{

                  doc.save();

              }catch(e){

                  doc.saveAs( Folder(path) );

              }

          }

          save( app.activeDocument, '~/Desktop' );

          • 2. Re: Add keywords with script - XMP
            Ten A Adobe Community Professional

            Try it:

            var key = prompt("Input keyword","");
            if (key!=null) {
              var st = app.activeDocument.XMPString;
              var str = "";
              if (st.match(/dc:subject/)==null) {
                 str = "<dc:subject><rdf:bag><rdf:li>"
                    + key + "</rdf:li></rdf:bag></dc:subject></dc:title>";
                 st.replace("</dc:title>",str);
                 app.activeDocument.XMPString = st;
              }
              else {
                 str = "<rdf:li>" + key + "</rdf:li></rdf:Bag>";
                 st = st.replace("</rdf:Bag>",str);
                 app.activeDocument.XMPString = st;
              }
              app.activeDocument.save();
            }
            
            • 3. Re: Add keywords with script - XMP
              LeoMari Level 2

              Hello my friends,

              I ran the scripts but my Keywords still empty.

              Am I doing something wrong?

              Screen Shot 2015-10-19 at 9.29.20 AM.png

              Thanks for the collaboration..

              • 4. Re: Add keywords with script - XMP
                Alexander Ladygin Level 2

                As an option:

                 

                function save(doc, path, name){

                    try{

                        doc.save();

                    }catch(e){

                        doc.saveAs( File(path + '/' + name) );

                    }

                }

                function setKeywords(keywords){

                    var xmp = activeDocument.XMPString.toString(),

                        startSubject = '<dc:subject><rdf:Bag>',

                        endSubject = '</rdf:Bag></dc:subject>',

                        words = keywords.split('\n');

                        keywords = '';

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

                        keywords += '<rdf:li>' + words[i] + '</rdf:li>';

                    }

                    if( xmp.indexOf(startSubject) > 0 ){

                        activeDocument.XMPString = xmp.replace( xmp.slice(xmp.indexOf(startSubject) + startSubject.length, xmp.indexOf(endSubject)), keywords );

                    }

                        else{

                            activeDocument.XMPString = xmp.replace('</dc:title>', '</dc:title>' + startSubject + keywords + endSubject);

                        }

                    save( app.activeDocument, '~/Desktop', 'Add keywords from script' );

                }

                setKeywords('One\nTwo\nThree\nFour\nFive');