Search code examples
rubyitunes

How do I set a iTunes track location from ruby


I want to update the paths of tracks in itunes from ruby but can not get it working. For some reason my call to track.setLocation() does nothing. Here is the script I'm using:

require 'osx/cocoa'
include OSX
OSX.require_framework 'ScriptingBridge'

def getSourceLibrary(iTunes)
    iTunes.sources.each do |source|
        if (source.kind == 1800169826) # TV Kind
            return source
        end
    end
    return nil
end

iTunes = SBApplication.applicationWithBundleIdentifier:'com.apple.iTunes'
library=getSourceLibrary(iTunes)
libraryPlaylists=library.libraryPlaylists

libraryPlaylists[0].fileTracks.each do | track |
    if (track.videoKind==1800823892)
        loc = track.location.to_s
        puts "Before: #{loc}"
        loc = loc.gsub(/\/mounts\/TVShows/,'/mounts/TV')
        loc = loc.gsub(/\/mounts\/incoming/,'/mounts/TV')
        track.setLocation(loc)
        puts "After: #{track.location.to_s}"
    end
end

The script iterates though the iTunes library and attempts to change the paths. However the before and after are the same! Even though the regex search & replace worked. Any help with this would be much appreciated.


Solution

  • I figured out a solution to my question. In order to set the location you have to pass a object of type NSURL. So if I amend the code in the question with the following, it all works:

    track.setLocation(NSURL::URLWithString(loc))