如何使用Python和Scripting Bridge将轨道添加到iTunes播放列表 [英] How to add a track to an iTunes playlist using Python and Scripting Bridge

查看:214
本文介绍了如何使用Python和Scripting Bridge将轨道添加到iTunes播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了如何在上一个问题中创建播放列表,但现在我不知道如何添加曲目。现在我有:

I learned how to create a playlist in a previous question, but now I can't figure out how to add tracks to it. Right now I have:

tracks.sort(key=lambda tup: tup[0])
i = 0
for trackList in generatePlaylists(tracks,10):
    i += 1
    playlistname = str(i)
    p = {'name': playlistname}
    playlist = iTunes.classForScriptingClass_("playlist").alloc().initWithProperties_(p)
    iTunes.sources()[0].playlists().insertObject_atIndex_(playlist, 0)

    # Find the playlist I just made
    for playlist in iTunes.sources()[0].playlists():
        if playlist.name() == playlistname:
            newPlaylist = playlist

    # Add the tracks to it
    for track in trackList:
        print track[1].name()
        iTunes.add_to_(track[1],newPlaylist)

我的曲目在元组列表 tracks 元素的元素是分数,第二个是实际轨道对象。 generatePlaylists 是一个迭代器,将所有库轨道拆分为10个列表。

My tracks are in a list of tuples tracks, where the first element of the tuple is a score and the second is the actual track object. generatePlaylists is an iterator which splits all library tracks into 10 lists.

上述代码运行没有错误,但在iTunes中,播放列表为空。

The above code runs without error, but in iTunes the playlists are empty.

推荐答案

首先,简短的答案:

track.duplicateTo_(newPlaylist)

问题是 iTunes.add_to _ 发送 add 命令,它接收一个文件(别名)并将其导入播放列表;您要发送 duplicate 命令,它接收任何对象并创建对象的另一个副本。你没有文件,你有一个轨道。 (您可以通过 track.location()获得一个文件,但不想重新导入该文件,只需复制该轨道。)

The problem is that iTunes.add_to_ sends the add command, which takes a file (alias) and imports it into a playlist; you want to send the duplicate command, which takes any object and makes another copy of the object. You don't have a file, you have a track. (You could get a file via track.location(), but you don't want to re-import the file, just copy the track over.)

此外,在这种情况下,您需要在轨道上调用该方法,而不是在应用程序上调用它并传递给轨道。

Also, in this case, you need to call the method on the track, rather than calling it on the app and passing it the track.

如果没有对iTunes对象模型(以及它下面的AE模型)的充分理解,这个问题的前半部分很难解释。但你真的不需要理解它。在大多数情况下,通过查看iTunes脚本字典(在AppleScript编辑器中)和尝试和错误(在AppleScript编辑器或与py-appscript),你可以找出你想要的。 (只需确保你正在使用一个废料库,或有备份...)在这种情况下,唯一的命令可能是添加 copy duplicate move 他们是这样。或者,去dougscripts下载一堆样品,找到一个你想要的样品。

The first half of this is hard to explain without a solid understanding of the iTunes object model (and the AE model underneath it). But you don't really need to understand it. In most cases, by looking over the iTunes scripting dictionary (in AppleScript Editor) and trial and error (in AppleScript Editor or with py-appscript) you can figure it out what you want. (Just make sure you're working on a scrap library, or have a backup…) In this case, the only commands it could possibly be are add, copy, duplicate, or move, so just try them all and see what they do. Or, alternatively, go to dougscripts and download a bunch of samples and find one that does what you want.

下半年,找出如何翻译成ScriptingBridge ...好吧,我不能解释它,没有进入一个长期的咆哮SB(如果你想读一个,它比我更好)。但基本是这样的:就iTunes而言, duplicate 是一个命令。如果你给它一个直接对象(告诉应用程序iTunes复制到播放列表),它会使用;如果没有,你要求主题重复自己(告诉theTrack复制到播放列表)。它的工作原理就像英语。但是SB坚持一个面向对象的模型,其中 duplicate 是某个对象上的一个方法。所以,这两种形式中只有一种工作。一般来说,你可以通过查看 dir(iTunes) dir(track)来了解哪个一个有一个看起来像你想要的命令的方法。

The second half of this, figuring out how to translate to ScriptingBridge… well, I can't explain it without going into a long rant on SB (which hhas does much better than me, if you want to read one). But the basics are this: As far as iTunes is concerned, duplicate is a command. If you give it a direct object (tell application "iTunes" to duplicate theTrack to thePlaylist) it'll use that; if not, you're asking the subject to duplicate itself (tell theTrack to duplicate to thePlaylist). It works exactly like English. But SB insists on an object-oriented model, where duplicate is a method on some object. So, only one of those two forms is going to work. In general, you can figure out which by just looking at dir(iTunes) and dir(track) to see which one has a method that looks like the command you want.

从上面可以看出,你有很多的试验和错误提前你,如果你试图做任何复杂的事情。祝你好运,并继续问。

As you can tell from the above, you've got a lot of trial and error ahead of you if you're trying to do anything complicated. Good luck, and keep asking.

PS,我不知道你的代码为什么默默地失败。 add_to _ 方法应该转换为命令的显而易见的方式应该引发一个-1708错误(如appscript iTunes.add(track,to = newPlaylist)或AppleScript 将该曲目添加到newPlaylist 都会...)。

PS, I have no idea why your code fails silently. The obvious way the add_to_ method should translate into a command should raise a -1708 error (as appscript iTunes.add(track, to=newPlaylist) or AppleScript add theTrack to newPlaylist both do…).

这篇关于如何使用Python和Scripting Bridge将轨道添加到iTunes播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆