Created
May 26, 2012 00:14
-
-
Save lad1337/2791364 to your computer and use it in GitHub Desktop.
applescript that will add mp3s to itunes with some extras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this script will go thru all sub folders (recursively) and look for mp3s it will add these mp3s to iTunes | |
# after all mp3s where added iTunes "Match" will be updated (optional) | |
# all mps3 will be deleted (optional) | |
# for every added files a growl message is send (optional) | |
# please see propertys bellow for settings. 0 = Off / No. 1 = ON / Yes | |
# USAGE: set this script as a folder action for a folder you want to watch or use it from the command line with: osascript add_mp3s_to_itunes.scpt <folder_to_process> | |
# for use with sabnzbd create a "sabToitunes.py" python script and place it next to this file. the contense is as follows (without the leading "(*" and trailing "*)"): | |
(* | |
#!/usr/bin/python | |
import sys, os | |
from subprocess import call | |
if len(sys.argv) < 2: | |
print "No folder supplied - is this being called from SABnzbd?" | |
sys.exit() | |
else: | |
scriptFile = os.path.join(os.path.dirname(sys.argv[0]), "add_mp3s_to_itunes.scpt") | |
call(['osascript', scriptFile, sys.argv[1]]) | |
*) | |
# NOTE: this script will log stuff in "~/Library/Logs/AppleScript-events.log" | |
# NOTE: the update_match currently only works with system language "en" or "de", because i dont know the menu names for other languages but they can be added | |
property update_match : 0 -- should i tell to itunes update match ? this might bring itunes into front (GUI scripting) | |
property delete_mp3s : 1 -- should the added mp3s be deleted ? | |
property send_growl_notifications : 1 -- should growl notifications be send for each file ? | |
property boxcar_user_email : "" -- fill in your email adress you used for boxcar to send boxcar notification on finish. leave blank for none | |
property DELAY_TIME_SECONDS : 3 -- How long to wait between checking file size. | |
property myFiletypes : {".mp3"} | |
property enable_dialog : 0 -- should there be a question dialog at all? dont know why you would wan that but its kinda a leftover from the development | |
property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer. it will say "no" after the amount of time | |
property test_folder : "HDD:lad1337:Desktop:add_mp3_test:" -- process this folder if no argument is given great for testing from within the script editor | |
property more_debug : 0 -- log more information | |
------------------- no need to edit the folowing stuff | |
property out_buffer : "" -- this does not concern you !! (its an output buffer with all log messages that is returned at the end for terminal usage) | |
property number_of_mp3s : 0 -- counting the mp3s added to iTunes per run | |
property scriptGrowlName : "Add mp3s to iTunes script" -- the name that will appear in the growl application list | |
-- logging function from | |
-- http://hints.macworld.com/article.php?story=2004121710493371 | |
on log_event(themessage) | |
set theLine to (do shell script ¬ | |
"date +'%Y-%m-%d %H:%M:%S'" as string) ¬ | |
& " " & themessage | |
-- the line break in next lines "" is not a misstake. its the newline for the output buffer | |
set out_buffer to out_buffer & theLine & " | |
" | |
set theLine to the quoted form of theLine -- make stuff console save | |
do shell script "echo " & theLine & " >> ~/Library/Logs/AppleScript-events.log" | |
end log_event | |
-- growl stuff from http://growl.info/documentation/applescript-support.php | |
-- growl init | |
on init_growl() | |
tell application "System Events" | |
set growlIsRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0 | |
end tell | |
if growlIsRunning then | |
tell application id "com.Growl.GrowlHelperApp" | |
-- Make a list of all the notification types | |
-- that this script will ever send: | |
set the allNotificationsList to {"Adding a file"} | |
-- Make a list of the notifications | |
-- that will be enabled by default. | |
set the enabledNotificationsList to {"Adding a file"} | |
-- Register our script with growl. | |
-- for this script's notifications. | |
register as application ¬ | |
scriptGrowlName all notifications allNotificationsList ¬ | |
default notifications enabledNotificationsList icon of application "Script Editor" | |
end tell | |
end if | |
end init_growl | |
-- growl send message function | |
on send_growl_msg(themessage) | |
if send_growl_notifications is 1 then | |
tell application "System Events" | |
set growlIsRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0 | |
end tell | |
if growlIsRunning then | |
tell application id "com.Growl.GrowlHelperApp" | |
-- Send a Notification... | |
notify with name ¬ | |
"Adding a file" title ¬ | |
"Adding a file to iTunes" description ¬ | |
themessage application name scriptGrowlName | |
end tell | |
end if | |
end if | |
end send_growl_msg | |
-- end growl helper functions | |
-- send boxcar notification | |
on send_boxcar_msg(msg) | |
set the msg to replace_chars(msg, " ", "+") | |
log_event("sending boxcar notification: " & msg) | |
try | |
set boxcarStatus to do shell script ("curl -S -d 'email=" & boxcar_user_email & "' -d '¬ification[from_screen_name]=" & scriptGrowlName & "' -d '¬ification[message]=" & msg & "' -D- http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications -o/dev/null | grep '^Status' | sed 's/Status:[ ]*//g'") | |
if more_debug is 1 then | |
log_event("boxcarStatus: " & boxcarStatus) | |
end if | |
set boxcarStatus to boxcarStatus as integer | |
return boxcarStatus | |
on error errorStr | |
log_event("send notification failed: " & errorStr) | |
end try | |
return 0 | |
end send_boxcar_msg | |
-- get the system language from http://macscripter.net/viewtopic.php?id=19528 | |
on get_sys_language() | |
set SystemLanguage to do shell script "defaults read .GlobalPreferences AppleLanguages | grep -v '^(' | head -n1 | sed 's/[ ,]*//g'" | |
return SystemLanguage | |
end get_sys_language | |
on replace_chars(this_text, search_string, replacement_string) | |
set AppleScript's text item delimiters to the search_string | |
set the item_list to every text item of this_text | |
set AppleScript's text item delimiters to the replacement_string | |
set this_text to the item_list as string | |
set AppleScript's text item delimiters to "" | |
return this_text | |
end replace_chars | |
-- recursive process folder | |
-- based on http://stackoverflow.com/questions/3896175/applescript-processing-files-in-folders-recursively | |
on process_folder(folderNameToProcess) | |
if more_debug is 1 then | |
log_event("now looking into folder " & folderNameToProcess) | |
end if | |
tell application "Finder" | |
set theItems to every file of folder folderNameToProcess | |
repeat with theFile in theItems | |
my process_file(theFile) | |
end repeat | |
set theFolders to name of folders of folder folderNameToProcess | |
repeat with theFolder in theFolders | |
copy theFolder as string to TheFolderName | |
set nextFolder to folderNameToProcess & ":" & TheFolderName | |
try | |
my process_folder(nextFolder) | |
on error errStr | |
my log_event("process_folder error") | |
my log_event("error: " & errStr) | |
end try | |
end repeat | |
end tell | |
end process_folder | |
-- looking at a file | |
-- basic idea from http://dougscripts.com/itunes/itinfo/folderaction01.php | |
-- and https://discussions.apple.com/thread/2489405?start=0&tstart=0 | |
on process_file(myfile) | |
set f to (myfile as alias) -- i dont know what this does / why its needed | |
set the file_path to the quoted form of the POSIX path of f | |
-- the POSIX path because logging "f" does not work / crashes when the files has a ->' | |
if more_debug is 1 then | |
log_event("looking at file " & file_path) | |
end if | |
tell application "Finder" to set myItemExtension to "." & (name extension of f) as string | |
-- do we have a mp3 file ? | |
if myFiletypes contains myItemExtension then | |
-- it will check if the file that was added is still beeing copied by checking the size every <DELAY_TIME_SECONDS> | |
set oldSize to 0 | |
set newSize to -1 | |
-- When newSize equals oldSize, it means the copy is complete because the size hasn't changed. | |
repeat while newSize is not equal to oldSize | |
if more_debug is 1 then | |
log_event("looking at file size") | |
end if | |
-- Get the file size. | |
set oldSize to size of (info for f) | |
delay DELAY_TIME_SECONDS | |
-- Sample the size again after delay for comparison. | |
set newSize to size of (info for f) | |
end repeat | |
if more_debug is 1 then | |
log_event("done looking at file size") | |
end if | |
if enable_dialog is 1 then | |
set alert_message to ("Add file to iTunes:" & file_path & return & return) as Unicode text | |
display dialog the alert_message buttons {"Yes", "No"} default button 2 with icon 1 giving up after dialog_timeout | |
set the user_choice to the button returned of the result | |
else | |
set the user_choice to "Yes" | |
end if | |
if user_choice is "Yes" then | |
-- HERE BEGINS THE ITUNES SPECIFIC STUFF | |
log_event("adding file to itunes: " & file_path) | |
send_growl_msg(file_path) | |
try | |
tell application "iTunes" | |
launch | |
add f | |
end tell | |
set number_of_mp3s to number_of_mp3s + 1 | |
if delete_mp3s is 1 then | |
do shell script ("rm -f " & file_path) | |
end if | |
on error errStr | |
log_event("error adding file to itunes: " & file_path) | |
log_event("error: " & errStr) | |
end try | |
-- HERE ENDS THE ITUNES SPECIFIC STUFF | |
end if | |
else | |
if more_debug is 1 then | |
log_event("not an mp3") | |
end if | |
end if | |
end process_file | |
-- main stuff | |
on main(root) | |
log_event("running " & scriptGrowlName) | |
set number_of_mp3s to 0 | |
-- growl stuff | |
init_growl() | |
-- this will look at all files and folder recursively | |
process_folder(root) | |
if update_match is 1 then | |
try | |
log_event("telling itunes to update match") | |
-- en and default menu item names | |
set match_menu_item to "Update iTunes Match" | |
set store_menu_item to "Store" | |
-- get system language | |
set cur_lang to get_sys_language() | |
-- feel free to add more languages | |
if cur_lang is "de" then | |
set match_menu_item to "iTunes Match aktualisieren" | |
set store_menu_item to "Store" | |
end if | |
tell application "iTunes" to activate | |
tell application "System Events" | |
tell application process "iTunes" | |
click menu item match_menu_item of menu 1 of menu bar item store_menu_item of menu bar 1 | |
end tell | |
end tell | |
tell application "System Events" | |
set visible of process "iTunes" to false | |
end tell | |
end try | |
end if | |
-- boxcar stuff | |
if boxcar_user_email is not "" then | |
tell application "Finder" | |
set folder_name to name of folder root | |
end tell | |
set boxcar_msg to "Added " & number_of_mp3s & " songs to iTunes from " & folder_name | |
set boxcar_status to send_boxcar_msg(boxcar_msg) | |
if boxcar_status is 401 then | |
log_event("subscribing to boxcar 'Monitoring' service") | |
do shell script ("curl -d 'email=" & boxcar_user_email & "' http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications/subscribe") | |
delay 3 | |
if send_boxcar_msg(boxcar_msg) is not 200 then | |
log_event("sending boxcar notification failed. check email and the 'Monitoring' service in boxcar") | |
end if | |
end if | |
end if | |
log_event(scriptGrowlName & " done") | |
end main | |
-- the run method. it is called when run with osascript or from the script editor | |
on run argv | |
set out_buffer to "" | |
if (count of argv) > 0 then | |
set pathToProcess to item 1 of argv | |
log_event("called with argument: " & pathToProcess) | |
set pathToProcess to POSIX file pathToProcess as string | |
main(pathToProcess) | |
else | |
-- test function | |
main(test_folder) | |
end if | |
return out_buffer | |
end run | |
-- folder action "hook" | |
-- this can also be used as folder action … it will always look at everything in that foler | |
on adding folder items to thisFolder after receiving theItems | |
set out_buffer to "" | |
main(thisFolder) | |
end adding folder items to |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, os | |
from subprocess import call | |
if len(sys.argv) < 2: | |
print "No folder supplied - is this being called from SABnzbd?" | |
sys.exit() | |
else: | |
scriptFile = os.path.join(os.path.dirname(sys.argv[0]), "add_mp3s_to_itunes.scpt") | |
call(['osascript', scriptFile, sys.argv[1]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment