Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 120997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:54:00+00:00 2026-05-11T03:54:00+00:00

I’m writing an Applescript playlist generator. Part of the process is to read the

  • 0

I’m writing an Applescript playlist generator. Part of the process is to read the iTunes Library XML file to get a list of all of the genres in a user’s library. This is the python implementation, which works as I’d like:

    #!/usr/bin/env python  # script to get all of the genres from itunes  import re,sys,sets   ## Boosted from the internet to handle HTML entities in Genre names def unescape(text):     def fixup(m):         text = m.group(0)         if text[:2] == '&#':             # character reference             try:                 if text[:3] == '&#x':                     return unichr(int(text[3:-1], 16))                 else:                     return unichr(int(text[2:-1]))             except ValueError:                 pass         else:             # named entity             try:                 text = unichr(htmlentitydefs.name2codepoint[text[1:-1]])             except KeyError:                 pass         return text # leave as is     return re.sub('&#?\w+;', fixup, text)   # probably faster to use a regex than to try to walk # the entire xml document and aggregate the genres try:     xml_path = '/Users/%s/Music/iTunes/iTunes Music Library.xml' % sys.argv[1] except:     print '\tUsage: python '+sys.argv[0]+' <your OSX username>'     raise SystemExit  pattern = '<key>Genre</key><string>([^<]+)</string>'   try:     xml = file(xml_path,'r').read() except:     print '\tUnable to load your iTunes Library XML file'     raise SystemExit  matches = re.findall(pattern,xml) uniques = map(unescape,list(sets.Set(matches))) ## need to write these out somewhere so the applescript can read them sys.stdout.write('|'.join(uniques)) raise SystemExit 

The problem is, I’d like the Applescript to be self-contained and not require that this additional file be present (I plan on making this available to other people). And, as far as I can tell, Applescript doesn’t offer any type of regular expression capabilities out of the box. I could loop over each track in the library to get all of the genres, but this is a prohibitively long process that I already do once when building the playlist. So, I’m looking for alternatives.

Since Applescript allows me to run a shell script and capture the results, I imagine that I can accomplish the same behavior using some type of shell command, be it grep, perl, or something else. My *nix command line skills are extremely rusty and I’m looking for some guidance.

So, in short, I’d like to find a way to translate the above python code into something I can call directly from the shell and get a similar result. Thanks!

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:54:01+00:00Added an answer on May 11, 2026 at 3:54 am

    Why are you using regex to parse XML? Why not use a proper XML library? Python has some great utilities like ElementTree that make walking the DOM a lot easier, and it yields nice, friendly objects rather than untyped strings.

    Here are some ways of parsing XML using Applescript:

    Applescript XML Parser (Available since Tiger apparently)

    XML Tools you can also use with Applescript

    Remember, just like Applescript can hook into iTunes, it can hook into other installed utilities like these.

    Lastly, why not just write the whole thing in Python since it has way better development tools for debugging and runs a lot faster. If you’re running Leopard, you have Python 2.5.1 pre-installed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.