Shell script to automatically add mp3s to iTunes

sabnzbd_logo

If you’re using Sabnzbd to download from usenet groups, it allows you to use a shell script for post processing. This script can be used to move any music files from your download location to a remote hard drive (or any mounted drive) as well as automatically adding the downloaded music to iTunes using AppleScript.

To use this with Sabnzbd, simply open a text editor and save it as “music_sort.sh”. (Or what ever you like!) – then tell Sabnzbd where to find the script. Here’s the script in it’s entirety. I’ll go through it bit by bit below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash

#1 = Full path to the downloaded file
#3 = Folder name of the download

#delete any files which you don't want to copy
echo "Deleting the bumfluff..."

rm -f "$1"/*.nfo
rm -f "$1"/*.srr
rm -f "$1"/*.sfv
rm -f "$1"/*.nzb
rm -f "$1"/*.m3u
rm -f "$1"/*.jpg

echo "[done]"

#move the file to your remote location
echo "Moving..."
mv -fv "$1" /volumes/myDrive/music
echo "[done]"

#add the file to iTunes
echo "Adding to iTunes…"
osascript << APPLESCRIPT
tell application "iTunes" to add "volumes:myDrive:music:$3" to playlist "Library"
APPLESCRIPT


echo "[done]"
exit

One thing to note here (and it took me a while to find it out) is that the path to add files to iTunes automatically has to have : and not a back or forward slash.

I’m sure this script can be improved, so please post if you spot anything!

4 Comments

  1. Simon says:

    How do I get it to move my movies to?

  2. Amit says:

    I wasn’t able to get htis to work..I made sure the file was SH. Then made sure it was used as a post-process script….here’s my code:

    #!/bin/bash

    #1 = Full path to the downloaded file
    #3 = Folder name of the download

    #delete any files which you don’t want to copy
    echo “Deleting the bumfluff…”

    rm -f “$1″/*.nfo
    rm -f “$1″/*.srr
    rm -f “$1″/*.sfv
    rm -f “$1″/*.nzb
    rm -f “$1″/*.m3u
    rm -f “$1″/*.jpg

    echo “[done]”

    #move the file to your remote location
    echo “Moving…”
    mv -fv “$1″ /Volumes/Media/Music
    echo “[done]”

    #add the file to iTunes
    echo “Adding to iTunes”
    osascript << APPLESCRIPT
    tell application "iTunes" to add "volumes:Macintosh HD:Users:amit:Music:iTunes Music:$3" to playlist "Library"
    APPLESCRIPT

    echo "[done]"
    exit

    TIA!

  3. AE says:

    I use a simpler alternative. You can have a one line script to just copy/move the files to your “Automatically Add to iTunes” directory:
    cp -fv “$1″/*.mp3 “~/Music/iTunes/iTunes Media/Automatically Add to iTunes/”

  4. mattfacer says:

    Thanks for this! I’m going to modify my script soon so will try this solution.

    Cheers, Matt

Leave a Comment

Powered by WordPress | Deadline Theme : An AWESEM design