Skip to main content
gregreser
Legend
August 2, 2011
Answered

catenateArrayItems returns incorrect character encoding

  • August 2, 2011
  • 1 reply
  • 1276 views

I need to create a semicolon separated string from an array and I am using the catenateArrayItems function.

     var X = XMPUtils.catenateArrayItems(xmpData, IPTC_DC, "subject", '; ',  '"',  XMPConst.SEPARATE_ALLOW_COMMAS);

This works, but if the values contain special characters (diacritics), the resulting string characters are not correct.  I don't have this problem when retrieving  simple propertues or when retrieving individual parts of the array, only when I use XMPUtils.catenateArrayItems.

Example data:

         <dc:subject>

            <rdf:Bag>

               <rdf:li>façade</rdf:li>

               <rdf:li>Jacques de Liége</rdf:li>

               <rdf:li>Téte à téte</rdf:li>

               <rdf:li>Suiksŏng i nop´ŭn yŏltae megi rŭl tŏ mani kirŭja!</rdf:li>

               <rdf:li>Это красивое описание красивое здание</rdf:li>

            </rdf:Bag>

         </dc:subject>

Results from

XMPUtils.catenateArrayItems(xmpData,XMPConst.NS_DC, "subject", '; ',  '"',  XMPConst.SEPARATE_ALLOW_COMMAS);

          façade; Jacques de Liége; Téte à téte; SuiksÅ&#143;ng i nop´ŭn yÅ&#143;ltae megi rÅ­l tÅ&#143; mani kirÅ­ja!; ЭÑ&#130;о кÑ&#128;аÑ&#129;ивое опиÑ&#129;ание кÑ&#128;аÑ&#129;ивое здание

--------------------------------------

Results from

for(var i = 1;i <= items;i++)

{

arrItem = xmpData.getArrayItem(XMPConst.NS_DC, "subject", i);

IPTCxRite.write ('\t' + arrItem);

$.writeln(arrItem);

}

     façade

     Jacques de Liége

     Téte à téte

     Suiksŏng i nop´ŭn yŏltae megi rŭl tŏ mani kirŭja!

     Это красивое описание красивое здание

Is there something else I need to do to get XMPUtils.catenateArrayItems to return the correct characters or is there another way to join the array items into a single string?

Thanks,

Greg Reser

This topic has been closed for replies.
Correct answer Paul Riggott

You could do something like this...

if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = app.document.selections[0].spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
var xmp = myXmpFile.getXMP();
var count = xmp.countArrayItems(XMPConst.NS_DC, "subject");
var Keys='';
for(var a = 1;a <= count;a++){
Keys += xmp.getArrayItem(XMPConst.NS_DC, "subject", a);
    if(a < count) Keys += ';';
}
$.writeln(Keys);

1 reply

Paul Riggott
Paul RiggottCorrect answer
Inspiring
August 2, 2011

You could do something like this...

if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = app.document.selections[0].spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
var xmp = myXmpFile.getXMP();
var count = xmp.countArrayItems(XMPConst.NS_DC, "subject");
var Keys='';
for(var a = 1;a <= count;a++){
Keys += xmp.getArrayItem(XMPConst.NS_DC, "subject", a);
    if(a < count) Keys += ';';
}
$.writeln(Keys);

gregreser
gregreserAuthor
Legend
August 3, 2011

Paul,

Thanks, this created a single string but it brought up an interesting new problem.  While the string that appears in the Extenscript Toolkit JAVASCRIPT CONSOLE window appears correct, i.e., has the correct characters, the data won't write to a .txt file (nothing is written at all).  In fact, what I originally thought was an incorrectly encoded string actually does write correctly to a .txt file.  I guess this means that what I'm seeing in the  JAVASCRIPT CONSOLE window using the $.writeln() function is not actually what is passed to a file.

Solved first problem:

For bag arrays like dc:subject, correctly encoded string writes to .txt file using XMPUtils.catenateArrayItems( ).

New problem:

For lang alt arrays like dc:title & description getArrayItem( ) returns correctly encoded string in the JAVASCRIPT CONSOLE window but won't write to a .txt file

XMPUtils.catenateArrayItems( ) does not work with lang alt arrays.

Greg

Paul Riggott
Inspiring
August 3, 2011

Did you set the text file to UTF-8 encoding?

This works for me Greg...

var file = new File(Folder.desktop + "/Test Keywords.txt");
file.open("e", "TEXT", "????");
$.os.search(/windows/i)  != -1 ? file.lineFeed = 'windows'  : file.lineFeed = 'macintosh';
file.encoding = "UTF8";
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var selectedFile = app.document.selections[0].spec;   
var myXmpFile = new XMPFile( selectedFile.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
var xmp = myXmpFile.getXMP();
var count = xmp.countArrayItems(XMPConst.NS_DC, "subject");
var Keys='';
for(var a = 1;a <= count;a++){
Keys += xmp.getArrayItem(XMPConst.NS_DC, "subject", a);
    if(a < count) Keys += ';';
}
file.seek(0,2);
file.writeln(Keys);
file.close();
file.execute();