Hello there,

Like always, I want to put in front that I'm not a script expert. I'm not even a basic coder. I just try to learn, little by little about coding when I have these challenges. Please be through enough for a Dummy!


I need to transfer the metadata information of the author of a PDF book to the Author tag in my iTunes library. (Yes! I got rid of iBooks and got back Books in iTunes)

Recently here in this site, with the help of one of the members I got a script problem solved. Or sort of.


I say sort of, because even when the script will do the task flawlessly if no special character is in the name, the script has a huge limitation: It won't read, write or both, text containing special characters.


Mercé Lamás del Jagüey Nuñez becomes in iTunes Merce\U0301 Lama\U0301s del Jagu\U0308ey Nun\U0303ez-

I appreciate your help to find a solution. I need to automate this task to properly curate the +4000 PDF books in my iTunes library.

Thank you!


tell application "iTunes"

set sel to the selection

if sel is {} then display dialog "Please select something" buttons {"Cancel"}

repeat with i from 1 to count of sel

    set the_track to item i of sel

    set the_posix_path to POSIX path of (location of the_track as alias)

    set authorfield to paragraph 2 of (do shell script "mdls -name kMDItemAuthors " & quoted form of the_posix_path & "|  awk -F\\\" '{print $2'}")

    set artist of the_track to (authorfield & "-") as string
end repeat  
end tell


enter image description here


You can use perl to convert escaped unicode character from ASCII text :

tell application "iTunes"
    set sel to the selection
    if sel is {} then display dialog "Please select something" buttons {"Cancel"}
    set tc to count sel
    repeat with i from 1 to tc
        tell (get item i of sel)
            set loc to location
            set authorfield to my getAuthors(loc)
            if authorfield is not "" then set artist to authorfield & "-"
        end tell
    end repeat
end tell

on getAuthors(f)
    do shell script "mdls -name kMDItemAuthors  " & (quoted form of POSIX path of f) & " | /usr/bin/perl -ne 'if (m/^ /) {s/^ +[\"]?|\"$//g;s/\\\\u([[:xdigit:]]{1,4})/chr(eval(\"0x$1\"))/egi;print}' "
end getAuthors

Or python :

do shell script "export PYTHONIOENCODING=UTF-8; /usr/bin/python -c 'import sys; print (sys.argv[1].replace(\"\\U\",\"\\u\").decode(\"unicode-escape\"))' \"$(mdls -name kMDItemAuthors  " & (quoted form of POSIX path of f) & " |  sed -nE 's/\"$//;s/^ +[\"]?//p')\""
  • Ah, the beauty of an AppleScript perverted by these barbaric Unix commands! I hope there is another solution. – Nicolas Barbulesco Apr 19 '15 at 9:14
  • Purist or not that solution works like a charm in both alternatives! – Jostma Apr 19 '15 at 14:35
  • @jackjr300: Wow! Thank you so much pal! Both, mdls and python flavors work perfectly! I really appreciate! You made my day. I can process my +4000 pdf books in iTunes library flawlessly and in almost no time. Thank you so much! – Jostma Apr 19 '15 at 14:40
  • @Jostma - What "both alternatives"? – Nicolas Barbulesco Sep 26 '15 at 10:53

At the end of your script, replace "as string" with "as Unicode string". And try. If this does not solve your problem, try with "as Unicode text".

  • "as Unicode string" formula not even pass the compiler attempt: "Expected class name but found identifier."---the "as Unicode text" passes compilation but gives the same bad result: "Merce\U0301 Lama\U0301s del Jagu\U0308ey Nun\U0303ez-" – Jostma Apr 19 '15 at 14:34
  • @Jostma - So reading and writing work but incorrectly? In your question, you say that reading and writing do not work. I don't understand. – Nicolas Barbulesco Sep 26 '15 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .