8 Replies Latest reply: Apr 20, 2011 11:20 PM by John.Kordas RSS

    JS CS3 Annotations

    John.Kordas Community Member

      I found a site called PS-Scripts that had some functions for creating and adding text into annotations.

       

      I had no issues with the creation part but when I tried to add text (string) to the annotation it kept giving me a "Could not complete the command because of a program error."

       

      Is it possible to add text into an annotation?

       

      Cheers, John

        • 1. Re: JS CS3 Annotations
          John.Kordas Community Member

          After a little more investigation and running the ScriptListener I found that the issue was the string for the text.

           

          The example for the function I found call the function like this function addAnnoText(string,noteNumber) {

           

          So by using addAnnoText("test",0); I expected that the word test would be created in the annotation.

           

          Running the ScriptListener and going through the steps I found that "Test" was captured as:

           

          String.fromCharCode( 255, 254, 84, 0, 101, 0, 115, 0, 116, 0 )

           

          So is there a way to just use "Test" instead of  String.fromCharCode( 255, 254, 84, 0, 101, 0, 115, 0, 116, 0 ).

           

           

          Cheers, John.

          • 2. Re: JS CS3 Annotations
            John.Kordas Community Member

            So I know that the line desc11.putData( id43, String.fromCharCode( 255, 254, 84, 0, 101, 0, 115, 0, 116, 0 ) ); in the ScriptListener will enter the word Test in the annotation.

             

            Now if I run the following:

             

            var myS = new String('Test');

            for(var t =0; t<myS.length;t++ ){
            $.write(myS.charCodeAt(t));
            $.write(",");
            }

             

            The values returned are 84,101,115,116 to try and work out what 255,254 and 0 are I used:

             

            function Chr(AsciiNum)
            {
                 return String.fromCharCode(AsciiNum)
            }
            Chr(255)

             

            This returned 255 = ÿ 254 = þ and 0 = " ".

             

            Can anyone tell me why 255, 254 and 0 are used in fromCharCode( 255, 254, 84, 0, 101, 0, 115, 0, 116, 0 )

             

            Cheers, John.

            • 3. Re: JS CS3 Annotations
              xbytor2 Community Member

              Try this:

               

              addAnno(String("\uFFEE"  + "Test"), 0);
              • 4. Re: JS CS3 Annotations
                xbytor2 Community Member

                \uFFFE indicates UTF16 encoding, the 0's are there becase JS strings are 16bit Unicode.

                • 5. Re: JS CS3 Annotations
                  John.Kordas Community Member

                  Thanks xbytor2 when I try addAnnoText(String("\uFFEE"  + "Test"),0); I still get an error "Could not complete the command because of the program error"

                   

                  script stops at  executeAction( cTID('setd'), desc47, DialogModes.NO );

                   

                   

                  I'll look into UTF16 a little more.

                  • 6. Re: JS CS3 Annotations
                    xbytor2 Community Member

                    My bad. "\uFFEE"  should (probably) be "\uFFFE" ...

                    • 7. Re: JS CS3 Annotations
                      John.Kordas Community Member

                      Thanks xbytor2 I saw the difference in your posts last night but it did not fix the issue.  I did some more testing at home and what is strange is that last night I tried the following code on my PC at home and it worked:

                      var idsetd = charIDToTypeID( "setd" );
                      var desc38 = new ActionDescriptor();
                      var idnull = charIDToTypeID( "null" );
                      var ref29 = new ActionReference();
                      var idannotation = stringIDToTypeID( "annotation" );
                      ref29.putIndex( idannotation, 0 );
                      desc38.putReference( idnull, ref29 );
                      var idT = charIDToTypeID( "T   " );
                      var desc39 = new ActionDescriptor();
                      var idTxtD = charIDToTypeID( "TxtD" );
                      desc39.putData( idTxtD, String("Test") );
                      var idtext = stringIDToTypeID( "text" );
                      desc39.putString( idtext, "Test" );
                      var idannotation = stringIDToTypeID( "annotation" );
                      desc38.putObject( idT, idannotation, desc39 );
                      executeAction( idsetd, desc38, DialogModes.NO );

                      When I try it this morning at work I get the error. Will need try it on a few different PCs.

                       

                      Forgot to mention that at home I'm running Windows7 and at work XP.  The OS wouldn't be the issue would it?

                       

                      Cheers John.

                      • 8. Re: JS CS3 Annotations
                        John.Kordas Community Member

                        There does seems to be an intermittent bug with CS3 and XP.  I've tried this on a numbers of PC's and I keep getting the same result. I use the following line (see function below):

                        desc48.putData( cTID('TxtD'),String(string));

                        The script will through an error and stop. If I exit Photoshop start it again and try the script it might work. There does not seem to be a pattern as it may not work for the next 7 or 9 goes (shutting & starting Photoshop each time) then it may work for the next 5 to 10.  I have noticed that once it does work if you just open and close files it's ok.  So it's like there is a process that has not loaded correctly and that is why it errors.

                         

                        For some reason using the following code works every time.

                        desc48.putData( cTID('TxtD'),String.fromCharCode( 255, 254, 84, 0, 101, 0, 115, 0, 116, 0 ));

                         

                        I've tried entering different text and all seem to pick up the 255, 254 and then 0, after each character. If I run:

                         

                        var nStr = "Test"
                        var nArr = [];
                        nArr[0] = 255;
                        nArr[1] = 254;
                        var t=2;
                        while (t <=nStr.length-1){
                        nArr.push(nStr.charCodeAt(t));
                        nArr.push(0);
                        t++
                        }
                        $.write(nArr);

                         

                        nArr will return 255, 254, 84, 0, 101, 0, 115, 0, 116, 0

                         

                        so passing nArr to the following function should work:

                         

                        addAnnoText(nArr,0);


                        function addAnnoText(string,noteNumber) {
                          function cTID(s) { return app.charIDToTypeID(s); };
                          function sTID(s) { return app.stringIDToTypeID(s); };
                            var desc47 = new ActionDescriptor();
                                var ref21 = new ActionReference();
                                ref21.putIndex( sTID('annotation'), noteNumber );
                            desc47.putReference( cTID('null'), ref21 );
                                var desc48 = new ActionDescriptor();
                                  desc48.putData( cTID('TxtD'),String.fromCharCode(string));  
                           desc47.putObject( cTID('T   '), sTID('annotation'), desc48 );
                            executeAction( cTID('setd'), desc47, DialogModes.NO );

                        };

                         

                        Is String.fromCharCode(nArr ) the same as String.fromCharCode( 255, 254, 84, 0, 101, 0, 115, 0, 116, 0 )?

                         

                        Cheers John.