Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy BPMs and Keys calculated in Algoriddim djay for Mac to iTunes meta tags
(*
** THIS SCRIPT IS OUTDATED **
Check https://github.com/ofstudio/djay2itunes.js for new version
djay2itunes.scpt
Description: Copy BPMs and Keys calculated in Algoriddim djay for Mac to iTunes meta tags
Version: 0.0.1 / July 2014
Compatibility: Djay 4.2.2, iTunes 11.2.2, OS X 10.9.3
Contact: Oleg Fomin <ofstudio@gmail.com>
With this script you can set the bpm data for tracks selected in iTunes using values
analysed by the Djay application (algoriddim). Tracks that have no Djay bpm information
will not be touched. Key data will be added to the Groupings tag.
Use at your own risk. Don't forget to backup your iTunes library first to prevent dataloss.
Usage:
1. Analize tracks using Algoriddim Djay (menu Library -> Analyze)
2. Close Djay application (important!)
3. Open iTunes and select necessary tracks
4. Open this script in Script Editor (`/Applications/Utilities/AppleScript Editor.app`)
5. Run script (`Cmd+R` keyboard shortcut)
BPMs will be written to `BPM` tags, Keys will be writen at the beginning of `Grouping` tags
Example: `11B-A Bluesdance` or `9B-G WCS`
Based on `Get bpm and key from Djay.scpt` by @djmumbler
http://edmondcho.com/2012/02/28/copy-calculated-bpm-data-from-algoriddim-djay-to-itunes-using-applescript/
Modifications to orignal script:
1. Write Key at the _beginnig_ of Grouping tag so sorting can be applied.
Example: `11B-A Bluesdance` or `9B-G WCS`
2. Fixed djay manual calculated bpm workaround (file `djay Preset Library.plist`). See below.
Automatically calculated BPMs and Keys are stored in `djay Cached Data.plist`.
Djay uses iTunes persistent IDs as index values in this file.
Manually calculated BPMs (tapped or corrected) are stored in `djay Preset Library.plist`.
Index values ​in this file have the following format:
`song_name artist_name duration_in_seconds`
Fields are separated by tab ("\t") symbol. Fields are in _lowercase_. Example:
`we're ready buddy guy & junior wells 222`
*)
-- Specify the Djay plist containing the information of analyzed tracks (this is by default located in the user's Music folder)
set the auto_data_path to "~/Music/djay/djay Cached Data.plist"
set the manual_data_path to "~/Music/djay/djay Preset Library.plist"
set all_keys to {"8B-C", "8A-Am", "3B-Db", "3A-Bbm", "10B-D", "10A-Bm", "5B-Eb", "5A-Cm", "12B-E", "12A-C#m", "7B-F", "7A-Dm", "2B-Gb", "2A-Ebm", "9B-G", "9A-Em", "4B-Ab", "4A-Fm", "11B-A", "11A-F#m", "6B-Bb", "6A-Gm", "1B-B", "1A-G#m"}
tell application "iTunes"
if selection is not {} then
set f to display dialog "Which fields do you want?" buttons {"BPM", "Key", "Both"} giving up after 15 default button "Both"
set fields to button returned of f
set o to display dialog "Replace existing iTunes data?" buttons {"No", "Yes"} giving up after 15 default button "Yes"
set overwrite_tag to button returned of o
set selR to selection
set sel to a reference to selR -- faster
repeat with t from 1 to (length of selR)
set theT to item t of sel
if class of theT is file track then
try
tell theT
set theId to persistent ID
-- set manualKey according to index format of `djay Preset Library.plist`
set dur to duration
set manualKey to name & " " & artist & " " & (round dur rounding to nearest)
-- convert to lowercase
set manualKey to do shell script "shopt -u xpg_echo; export LANG='" & user locale of (system info) & ".UTF-8'; echo " & quoted form of manualKey & " | tr [:upper:] [:lower:]"
-- display dialog manual_key
if (fields is "BPM" or fields is "Both") then
if (bpm of theT is 0 or overwrite_tag is "Yes") then
-- get bpm data:
tell application "System Events"
-- reset variables
set bpm_djay to 0
set manual_bpm_djay to 0
set auto_bpm_djay to 0
-- look in Preset Library file
try
tell property list file (manual_data_path)
tell contents
tell property list item "Song Entries"
--set propertyKey to
set theItem to (property list item manualKey)
set manual_bpm_djay to (value of property list item "song.manualBpm" of (theItem))
--display dialog "Preset: " & presetlibrary_bpm_djay
end tell
end tell
end tell
end try
-- look in Cache Data file
try
tell property list file (auto_data_path)
tell contents
set theItem to (property list item theId)
set auto_bpm_djay to (value of property list item "bpm" of (theItem))
--display dialog "Cache: " & cache_bpm_djay
end tell
end tell
end try
end tell
-- round the bpm value:
if (manual_bpm_djay is not 0) then
set bpm_djay to manual_bpm_djay
else if (auto_bpm_djay is not 0) then
set bpm_djay to auto_bpm_djay
else
set bpm_djay to 0
end if
if (bpm_djay is not 0) then
-- write bpm data:
set bpm of theT to (round bpm_djay rounding to nearest)
end if
end if
end if
if (fields is "Key" or fields is "Both") then
if (grouping of theT is 0 or overwrite_tag is "Yes") then
-- reset variables
set key_djay to -1
-- get key data:
tell application "System Events"
-- look in Cache Data file
try
tell property list file (auto_data_path)
tell contents
set theItem to (property list item theId)
set key_djay to (value of property list item "key" of (theItem))
end tell
end tell
end try
end tell
if (key_djay is not -1) then
-- translate djay key data
set key_djay_string to item (key_djay + 1) of all_keys
-- look for existing grouping data
set old_grouping to grouping of theT
if old_grouping is not "" then
-- look for key data in grouping
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to space
set possible_key to first text item of old_grouping
set AppleScript's text item delimiters to tid
log possible_key
if all_keys contains possible_key then
set AppleScript's text item delimiters to possible_key
set old_grouping to text item 2 of old_grouping
set AppleScript's text item delimiters to tid
-- write key data:
set grouping of theT to key_djay_string & old_grouping
else
-- write key data:
set grouping of theT to key_djay_string & " " & old_grouping
end if
else
-- write key data:
set grouping of theT to key_djay_string
end if
end if
end if
end if
end tell
end try
end if
end repeat
try
if frontmost is true then display dialog "Done!" buttons {"Thanks"} ¬
default button 1 with icon 1 giving up after 15
end try
else
display dialog "Please select a few tracks in iTunes and try again!" buttons {"OK"} giving up after 15
end if
end tell
@ofstudio

This comment has been minimized.

Copy link
Owner

ofstudio commented Dec 28, 2014

THIS SCRIPT IS OUTDATED!

Check djay2itunes.js for new version

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