RubyAEOSA improvements, esp. for iTunes.

D

Dave Howell

Trying to do things faster than they can go with AppleScript and iTunes
by using Ruby, but found that a lot of the information about a track
doesn't get translated when going through to_rbobj.

In particular, the file track's "location" (a file alias) and "date
played" and "date added" (dates) came through in unusable form.

I've pasted my fix below. to_rbobj for TypeAlias now returns a File
object, since the whole point of an Alias is that it tracks a file via
its inode, not its name or path. Hopefully a File object will behave
similarly. TypeLongDateTime now comes back as a Date object.

There was also the problem of fields that in AppleScript would contain
"missing value," which appears in RubyAEOSA as an AEDesc with type
"TypeType" and value "msng." I just sort of attached a solution with
chewing-gum for that case, because I don't understand what a TypeType
record actually is. iTunes track properties that would be "missing
data" will read in Ruby as "nil".

module OSX
class AEDesc
require 'date'

def to_file
d = to_path
File.new(d)
end

def to_date
d = coerce(TypeLongDateTime)
Date.new(1904, 1, 1) + Date.time_to_day_fraction(0, 0,
d.rawdata.unpack('q')[0])
end

def to_type
d = rawdata
if d == 'msng' then nil else d end
end

def to_rbobj
case __typestr__
when TypeBoolean, TypeTrue, TypeFalse then to_bool
when TypeChar, TypeUnicodeText, TypeStyledUnicodeText,
TypeUTF8Text,
TypeEncodedString, TypeCString, TypePString then to_s
when TypeKeyword, TypeApplSignature then rawdata
when TypeFSS, TypeFSRef then to_path
when TypeAlias then to_file
when TypeShortInteger, TypeInteger, TypeMagnitude then to_i
when TypeShortFloat, TypeFloat, TypeExtended then to_f
when TypeAEList then to_a
when TypeAERecord then to_hash
when TypeLongDateTime then to_date
when TypeNull then nil
when TypeType then to_type
else self
end
end
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top