8 Replies Latest reply on Jun 15, 2012 12:05 AM by michael.shen

    how to create a text layer with special characters

    michael.shen

      Hi guys,

       

      I'm doing the following things, but now i encountered some problems.

      I will get a xml file from remote host, then use this file to create a psd file, but if the file contains some special characters, the psd file  don't render properly. Can anyone help me to solve the problem? The xml file use UTF-8 encoding.

       

      the xml file just like:

      <?xml version="1.0" encoding="UTF-8" ?>
      <HelloEncodingWorld>Üöäüßßß Test!!!</HelloEncodingWorld>

       


      here is my sample coding.

       

      var script = '';

      var assetUrl = ServerURL+xmlFile;

      var http = new HttpConnection(assetUrl);  

      http.request;

      http.method = "GET";

      http.response;

      http.execute() ;   

      var tmpStr = String(http.response);

      var xmlString = new XML(tmpStr);

       

      //create psd file script

      script += "";

       

      //execute

      var bt = new BridgeTalk();

      bt.target = "photoshop";

      bt.body = script;

      bt.send();

        • 1. Re: how to create a text layer with special characters
          Paul Riggott Level 6

          Does this help....

           

           

          var newText = encodeURI("Üöäüßßß Test!!!");
          var bt = new BridgeTalk();
          bt.target = "photoshop";
          bt.body = "var ftn = " + script.toSource() + "; ftn(" + newText.toSource() +");"; 
          bt.send(8);
          function script(Content){
          var FontName = "Georgia"; 
          var FontSize = 14;
          var black = new SolidColor(); 
          black.rgb.hexValue = '000000'; 
          var startRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          app.documents.add(600,400);
          var newTextLayer = activeDocument.artLayers.add(); 
          newTextLayer.kind = LayerKind.TEXT; 
          newTextLayer.textItem.kind = TextType.POINTTEXT;
          newTextLayer.textItem.color = black; 
          newTextLayer.textItem.font = FontName;
          newTextLayer.textItem.size = FontSize; 
          newTextLayer.textItem.contents = decodeURI(Content);
          newTextLayer.textItem.position = Array(20, 20); 
          app.preferences.rulerUnits = startRulerUnits;
          }
          
          
          • 2. Re: how to create a text layer with special characters
            michael.shen Level 1

            thank you for your reply. It works excellently. But that doesn't match my requirement.

            Please see my demo code.

                                var assetUrl = 'http://localhost/d.xml';

                                var http = new HttpConnection(assetUrl);  

                                http.request;

                                http.method = "GET";

                                http.response;

                                http.execute() ;   

                                var tmpStr = String(http.response);

                                var xmlString = new XML(tmpStr);

                                var tmp = xmlString.workspace;

                                var templateobj = tmp.template;

                                var text = templateobj[0].txt;          //the text is incorrect, i got the value is ORâS, not OR’S, I think here must to be converted, but i dont't konw how to do it.

                                $.writeln("text.."+ text);

                                var newText = encodeURI(text);

                                var bt = new BridgeTalk();

                                bt.target = "photoshop";

                                bt.body = "var ftn = " + script.toSource() + "; ftn(" + newText.toSource() +");";

                                bt.send(8);

                                function script(Content){

                                    var FontName = "Georgia";

                                    var FontSize = 14;

                                    var black = new SolidColor();

                                    black.rgb.hexValue = '000000';

                                    var startRulerUnits = app.preferences.rulerUnits;

                                    app.preferences.rulerUnits = Units.PIXELS;

                                    app.documents.add(600,400);

                                    var newTextLayer = activeDocument.artLayers.add();

                                    newTextLayer.kind = LayerKind.TEXT;

                                    newTextLayer.textItem.kind = TextType.POINTTEXT;

                                    newTextLayer.textItem.color = black;

                                    newTextLayer.textItem.font = FontName;

                                    newTextLayer.textItem.size = FontSize;

                                    newTextLayer.textItem.contents = decodeURI(Content);

                                    newTextLayer.textItem.position = Array(20, 20);

                                    app.preferences.rulerUnits = startRulerUnits;

                                }

            the xml file is here:

             

            <?xml version="1.0" encoding="UTF-8"?>
            <workspace_set>
              <workspace id="0">
                <template id="0">
                    <txt><![CDATA[OR’S]]></txt>
                </template>
              </workspace>
            </workspace_set>
            
            • 3. Re: how to create a text layer with special characters
              Paul Riggott Level 6

              Ah, sorry I thought you were getting the right value but not getting the right result in Photoshop.

               

              I have never used HttpConnection and not sure how to set it up to test.

              Maybe the text should be encoded first? I don't know.

              • 4. Re: how to create a text layer with special characters
                michael.shen Level 1

                yes, i think it's character encoding problem. But i don't know what encoding are  about the Photoshop and the response content . Maybe they have to be converted.

                • 5. Re: how to create a text layer with special characters
                  Paul Riggott Level 6

                  Well I did manage to get it to work, but only using a file for the response, I don't know enough about this.

                   

                  Here is the code I used...

                   

                   

                  if( webaccesslib == undefined ) {
                      if( Folder.fs == "Windows" ) {
                          var pathToLib = Folder.startup.fsName + "/webaccesslib.dll";
                          } else {
                              var pathToLib = Folder.startup.fsName + "/webaccesslib.bundle";
                          }
                      var libfile = new File( pathToLib );
                      var webaccesslib = new ExternalObject("lib:" + pathToLib );
                  }
                  var assetUrl = "http://localhost/d.xml";
                  var http = new HttpConnection(assetUrl);  
                  var tmpFile= File(Folder.temp+"/tmp.xml");
                  http.response = tmpFile;
                  http.execute() ;  
                  http.response.close();
                  tmpFile.open('r');
                  var xmlString = new XML(tmpFile.read());
                  tmpFile.close();
                  var tmp = xmlString.workspace;
                  var templateobj = tmp.template;
                  var newText = decodeURI(templateobj[0].txt);   
                  var bt = new BridgeTalk();
                  bt.target = "photoshop";
                  bt.body = "var ftn = " + script.toSource() + "; ftn(" + encodeURI(newText).toSource() +");"; 
                  bt.send(8);
                  function script(Content){
                  var FontName = "Georgia"; 
                  var FontSize = 14;
                  var black = new SolidColor(); 
                  black.rgb.hexValue = '000000'; 
                  var startRulerUnits = app.preferences.rulerUnits;
                  app.preferences.rulerUnits = Units.PIXELS;
                  app.documents.add(600,400);
                  var newTextLayer = activeDocument.artLayers.add(); 
                  newTextLayer.kind = LayerKind.TEXT; 
                  newTextLayer.textItem.kind = TextType.POINTTEXT;
                  newTextLayer.textItem.color = black; 
                  newTextLayer.textItem.font = FontName;
                  newTextLayer.textItem.size = FontSize; 
                  newTextLayer.textItem.contents = decodeURI(Content);
                  newTextLayer.textItem.position = Array(20, 20); 
                  app.preferences.rulerUnits = startRulerUnits;
                  }
                  
                  
                   

                   

                  The XML file did need modifing to get the apostrophe to come accross.

                   

                   

                  <?xml version="1.0" encoding="UTF-8"?>
                  <workspace_set>
                    <workspace id="0">
                      <template id="0">
                          <txt><![CDATA[OR%27S]]></txt>
                      </template>
                    </workspace>
                  </workspace_set>
                  
                  
                   
                  1 person found this helpful
                  • 6. Re: how to create a text layer with special characters
                    Tom Ruark Adobe Employee

                    Make sure you save your jsx file with unicode markers. Here is the code I use: (Hopefully I can copy and you can paste from this page!) I'm using the Code2000 font so I don't have to find a font for each language!

                     

                    $.level = 0;

                    var fontAlert = false;

                     

                     

                    main();

                     

                     

                    function main()

                    {

                              var textArray = new Array();

                              textArray.push("1 Arabic عندما يريدالعالم أن يتكلّم, فهو يتحدّث بلغة يونيكود.");

                              textArray.push("2 Catalan Quan el món vol conversar, parla Unicode");

                              textArray.push("3 Chinese (Simplified) 当世界需要沟通时,请用Unicode!");

                              textArray.push("4 Chinese (Traditional) 當世界需要溝通時,請用統一碼(Unicode)");

                              textArray.push("5 Danish Når verden vil tale, taler den Unicode");

                              textArray.push("6 Dutch Als de wereld wil praten, spreekt hij Unicode ");

                              textArray.push("7 English When the world wants to talk, it speaks Unicode");

                              textArray.push("8 Esperanto Kiam la mondo volas paroli, ĝi parolas Unicode");

                              textArray.push("9 Finnish Kun maailma haluaa puhua, se puhuu Unicodea");

                              textArray.push("10 French Quand le monde veut communiquer, il parle en Unicode");

                              textArray.push("11 Georgian როდესაც მსოფლიოს ურთირთობა სურს, იგი Unicode-ის ენაზე ლაპარაკობს");

                              textArray.push("12 German Wenn die Welt miteinander spricht, spricht sie Unicode");

                              textArray.push("13 Hebrew           כאשר העולםרוצה לדבר, הוא מדבר ב־Unicode");

                              textArray.push("14 Hungarian Ha a világ beszélni akar, azt Unicode-ul mondja");

                              textArray.push("15 Irish Gaelic Nuair a bhualann fonn cainte an domhan, is as Unicode a labhrann sé");

                              textArray.push("16 Italian Quando il mondo vuole comunicare, parla Unicode");

                              textArray.push("17 Japanese 世界的に話すなら、Unicode です。");

                              textArray.push("18 Korean 세계를 향한 대화, 유니코드로 하십시오");

                              textArray.push("19 Norwegian (Bokmål) Når verden vil snakke, snakker den Unicode");

                              textArray.push("20 Norwegian (Nynorsk) Når verda ønskjer å snakke, talar ho Unicode");

                              textArray.push("21 Occitan Quan lo mond vòl conversar, parla en Unicode");

                              textArray.push("22 Portuguese (Brazil) Quando o mundo quer falar, fala Unicode");

                              textArray.push("23 Portuguese (Portugal) Quando o mundo quer falar, fala Unicode");

                              textArray.push("24 Romanian Când lumea vrea să comunice, vorbeşte Unicode");

                              textArray.push("25 Russian Когда мир желает общаться, он общается на Unicode");

                              textArray.push("26 Slovenian Ko se želi svet pogovarjati, govori Unicode");

                              textArray.push("27 Spanish Cuando el mundo quiere conversar, habla Unicode");

                              textArray.push("28 Swedish När världen vill tala, så talar den Unicode");

                              textArray.push("29 Yiddish אַז די װעלט װיל רעדן, רעדט זי אוניקאָד");

                     

                              var w = new UnitValue(20, "in");

                              var h = new UnitValue(1, "in");

                              app.documents.add(w, h, 72, textArray[0]);

                     

                     

                              var textColor = new SolidColor;

                              textColor.rgb.red = 255;

                              textColor.rgb.green = 0;

                              textColor.rgb.blue = 0;

                     

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

                              {

                                        var newTextLayer = activeDocument.artLayers.add();

                                        newTextLayer.name = textArray[i];

                                        newTextLayer.kind = LayerKind.TEXT;

                                        newTextLayer.textItem.contents = textArray[i];

                                        newTextLayer.textItem.position = Array(UnitValue(1, "in"), UnitValue(.5, "in"));

                                        newTextLayer.textItem.size = 36;

                                        newTextLayer.textItem.color = textColor;

                                        newTextLayer.textItem.font = "Code2000"; // "ArialUnicodeMS"; // "Code2000";  // "Times"; //

                            if (newTextLayer.textItem.font != "Code2000" && !fontAlert) {

                                      alert("Font does not exist! Code2000");

                                                  fontAlert = true;

                            }

                                        newTextLayer.visible = false;

                                        AdjustColor(textColor);

                              }

                     

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

                              {

                                        activeDocument.activeLayer = activeDocument.artLayers[i];

                                        activeDocument.activeLayer.visible = true;

                            app.refresh();

                                        var text = activeDocument.activeLayer.textItem.contents;

                                        if (text != textArray[textArray.length - i - 1])

                                        {

                                                  alert(text + " != " + textArray[textArray.length - i - 1]);

                                        }

                                        activeDocument.activeLayer.visible = false;

                              }

                     

                     

                              activeDocument.activeLayer.visible = true;

                    }

                     

                     

                    function AdjustColor(textColor)

                    {

                              var r = textColor.rgb.red;

                              var g = textColor.rgb.green;

                              var b = textColor.rgb.blue;

                     

                              r = r + 100;

                              if (r > 255)

                              {

                                        r = 0;

                                        g = g + 100;

                                        if (g > 255)

                                        {

                                                  g = 0;

                                                  b = b + 100;

                                                  if (b > 255)

                                                  {

                                                            b = 0;

                                                  }

                                        }

                              }

                              textColor.rgb.red = r;

                              textColor.rgb.green = g;

                              textColor.rgb.blue = b;

                    }

                    1 person found this helpful
                    • 7. Re: how to create a text layer with special characters
                      Paul Riggott Level 6

                      Thanks for that Tom but the problem here is that HttpConnection is stripping apostrophes, so the real question is how can you retain them without replacing each apostrophe with %27

                      • 8. Re: how to create a text layer with special characters
                        michael.shen Level 1

                        I have solved the problem.

                        First i download the xml to the local. Then use the another handle to open the file, can't use the first handle, otherwise it will get the incorrect data when create the psd file.

                         

                                    var http = new HttpConnection(assetUrl);  

                                    var tmpFile= File(AssetsPath + itemID+"/tmp.xml");

                                    http.response = tmpFile;

                                    http.execute() ; 

                                    http.response.close();

                                    tmpFile.close();

                                    var ucdXmlFile = new File(AssetsPath + itemID+"/tmp.xml");

                                    ucdXmlFile.open('r');

                                    var xmlString = new XML(ucdXmlFile.read());

                                    ucdXmlFile.close();

                                    ucdXmlFile.remove();