开发者

Scripting itunes without applescript?

I am looking for a way of scripting iTunes using something else then AppleScript. I want to mass-manipulate the tit开发者_JAVA百科le-name and artist of tracks. Using some id3-tool won't help, because as far as I know the iTunes database will not update, if I don't use iTunes for manipulating this information. As I don't know how to code with AppleScript and don't really have the time to dive into this, I wonder: is there any way to do this task using javascript, lua, bash or php?

thanks,


I suggest you to learn AppleScript, at least for a day. The point is, the inter-GUI-app communication in OS X is done via Apple Events, whose construct follows that of its main language AppleScript. There are many bridges which allow you to call Apple Events from various languages, but you need to understand the concept of Apple Events first. So, at least you need a bit of familiarity with AppleScript.

This is just as in the case of Cocoa: you can code Cocoa apps in many languages, but most of the documentation and the concept are based on Objective-C. So you need to at least a bit of familiarity with Objective-C before coding Cocoa in other languages.

So, let me give you a very very short overview of Apple Events/ AppleScript system.

Each app implements an object-oriented system and exposes it to the outside world as a dictionary, which you can read with AppleScript Editor. Open the AppleScript Editor, and choose File → Open Dictionary, and choose iTunes. There, you see the list of commands, classes, methods in those classes, etc. Then, from AppleScript or Ruby or Lua, you access these objects and methods.

Suppose you want to rename the selected entries of iTunes from "A-B" to "B-A". Then the code would be

set text item delimiters to "-"  
tell application "iTunes"         -- following statements are targeted to iTunes
repeat with entry in selection -- "selection" is a concept implemented in iTunes 
    set s to name of entry      -- copy the name of entry to a local string s
    set x to text items of s    -- split the string s to a list according to text item delimiters
    set y to {item 2 of x, item 1 of x}  -- construct another list
    set name of entry to y as string -- set the name. Note that "as string" adds the delimiters
end repeat
end tell

Yes the grammar of AppleScript is a bit weird, but it basically has one-to-one correspondence with a regular imperative language. Just refer to the official language guide if you're confused. And text operations in AppleScript without a good OSAX (AppleScript's extension system) is a chore. So I agree it's not a bad idea to first get familiar with the concept of AppleScript, and then use it from Ruby or any of your favorite languages.

But remember, open the dictionary in AppleScript editor, because that's where you find what each app implements and exposes to the system!


You can send Apple Events from JavaScript with JavaScriptOSA. However I'd recommend you investigate appscript (Python, Ruby) instead as it's more up-to-date and supported.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜