Mac OSX Terminal – Get Artist and Song From iTunes PlayList Export

| | | |

There are more elegant ways to do this but I got bored and thought I’d do something that is only done with base Mac OSX tools

#!/bin/sh

#  Read_iTunes_PlayList.sh
#
#  Get only the artist list - remove duplicates
#  Read_iTunes_PlayList.sh 1 iTunesPlayListFileName ~/Desktop
#
#  Get Artist and Song Name
#  Read_iTunes_PlayList.sh 2 iTunesPlayListFileName ~/Desktop
#
#  Created by David Kittell on 12/27/16.
#

clear

XMLOption=$1
XMLFileName=$2
XMLFilePath=$3

case $XMLOption in
1)
# Only Get the Artist

clear
#echo $XMLOption
#echo $XMLFileName
#echo $XMLFilePath

rm -rf $(echo $XMLFilePath/$XMLFileName).txt || :

#echo "Parsing XML"
music=`grep 'Artist' $(echo $XMLFilePath/$XMLFileName.xml) | \
sed 's/<key>Artist<\/key><string>//g'| sed 's/<\/string>//g' \
| sed 's/%20/ /g' | sort`;

old_IFS=$IFS

# IFS by default is space tab return, and stands for
# Internal Field Separator.
IFS='
'

for i in $music
do
`echo $i >> $(echo $XMLFilePath/$XMLFileName.txt)`;
done

IFS=$old_IFS

cat $(echo $XMLFilePath/$XMLFileName.txt) | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' > $(echo $XMLFilePath/$XMLFileName_01.txt)

sed -i -e "s|&#38;|\&|g" $(echo $XMLFilePath/$XMLFileName_01.txt)
sed -i -e "s|&#60;|\<|g" $(echo $XMLFilePath/$XMLFileName_01.txt)
sed -i -e "s|&#62;|\>|g" $(echo $XMLFilePath/$XMLFileName_01.txt)

sort $(echo $XMLFilePath/$XMLFileName_01.txt) | uniq | grep -v '<key>' > $(echo $XMLFilePath/$XMLFileName.txt)

rm -f  $(echo $XMLFilePath/$XMLFileName_01.txt)

cat $(echo $XMLFilePath/$XMLFileName.txt)

;;
2)
# Get the Artist and Song

clear

#echo $XMLOption
#echo $XMLFileName
#echo $XMLFilePath

rm -rf $(echo $XMLFilePath/$XMLFileName).txt || :

#echo "Parsing XML"
grep -E '<key>Artist</key>|<key>Name</key>' iPhone.xml | sed 's/<key>Artist<\/key><string>/|/g'| sed 's/<\/string>//g' | sed 's/%20//g' | sed 's/<key>Name<\/key><string>/~/g'| sed 's/<\/string>//g' | sed 's/%20//g' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/ /g' | sed -e "s|&#38;|\&|g" | sed -e "s|&#60;|\<|g" | sed -e "s|&#62;|\>|g" | tr '~' '\n' | sort > $(echo $XMLFilePath/$XMLFileName).txt

#cat $XMLFilePath/$XMLFileName.txt

#echo "Cleaning List - Start"
IFS=$'\n'
for next in `cat $filename`
do
song=$(echo $next | cut -d '|' -f1 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
artist=$(echo $next | cut -d '|' -f2 | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
echo "$artist \t $song" >> $(echo $XMLFilePath/$XMLFileName)Filtered.txt
done < $XMLFilePath/$XMLFileName.txt
#echo "Cleaning List - Stop"

#echo "Remove iTunes PlayList name"
sed -i '' -e '$ d' $(echo $XMLFilePath/$XMLFileName)Filtered.txt

#echo "Sort the results"
cat $(echo $XMLFilePath/$XMLFileName)Filtered.txt | sort > $(echo $XMLFilePath/$XMLFileName).txt

cat $(echo $XMLFilePath/$XMLFileName).txt


;;
esac
Originally Posted on December 28, 2016
Last Updated on March 6, 2017
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.