Skip to content

Instantly share code, notes, and snippets.

@mattlubner
Last active October 7, 2016 23:18
Show Gist options
  • Save mattlubner/9746041 to your computer and use it in GitHub Desktop.
Save mattlubner/9746041 to your computer and use it in GitHub Desktop.
Useful shell script that takes a file list as stdin and returns only those files not loaded into the active iTunes library
#!/bin/bash
permitted_file_extensions="mov|mp4|m4v|mpg|mpeg|m2v|mp2|ite|aac|m4a|m4b|m4p|mp3|caf|aiff|aif|aifc|au|sd2|wav|snd|amr|3ga"
eval "active_library="$(defaults read com.apple.iApps iTunesRecentDatabases)
printf -v active_library '%b' "${active_library[0]//%/\\x}"
active_library="${active_library#file://localhost}"
if [[ ! -e "$active_library" ]]; then
exit 1
fi
function url_encode_for_itunes()
{
local encoded="$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "${1}")"
encoded="${encoded//\%2F//}"
encoded="${encoded//\%28/(}"
encoded="${encoded//\%29/)}"
encoded="${encoded//\%27/\'}"
encoded="${encoded//\%2C/,}"
REPLY="${encoded}"
}
function filter_existing_itunes_media_files()
{
# If stdin is a directory...
if [[ -d "$1" ]]; then
# Check all files having the permitted file extensions
find "$1" -depth 1 -type f | grep -E -i "\.($permitted_file_extensions)$" | sort | while read path
do
filter_existing_itunes_media_files "$path"
done
# Recurse into directories
find "$1" -depth 1 -type d | while read path
do
filter_existing_itunes_media_files "$path"
done
# Else, if stdin is a file...
elif [[ -f "$1" ]]; then
url_encode_for_itunes "$1"
grep -q "${REPLY}" "$active_library" &>/dev/null || echo -e "$1"
fi
}
while read path
do
filter_existing_itunes_media_files "$path"
done
Copyright (c) 2014, Matt Lubner <matt@mattlubner.com>
The "ISC" license:
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
@mattlubner
Copy link
Author

The Automator workflow is available at: https://github.com/mattlubner/Import-to-iTunes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment