Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;“未知对象”;尝试从pict文件捕获艺术品并将其嵌入曲目时出错_Python_Sourceforge Appscript - Fatal编程技术网

Python &引用;“未知对象”;尝试从pict文件捕获艺术品并将其嵌入曲目时出错

Python &引用;“未知对象”;尝试从pict文件捕获艺术品并将其嵌入曲目时出错,python,sourceforge-appscript,Python,Sourceforge Appscript,我试图从一个pict文件中捕获艺术品,并使用python appscript将其嵌入iTunes上的一个曲目中。 我是这样做的: imFile = open('/Users/kartikaiyer/temp.pict','r') data = imFile.read() it = app('iTunes') sel = it.current_track.get() sel.artworks[0].data_.set(data[513:]) tell application "iTun

我试图从一个pict文件中捕获艺术品,并使用python appscript将其嵌入iTunes上的一个曲目中。 我是这样做的:

imFile = open('/Users/kartikaiyer/temp.pict','r')
data = imFile.read()
it = app('iTunes')
sel = it.current_track.get()
sel.artworks[0].data_.set(data[513:])
tell application "iTunes"
    set the_artwork to read (POSIX file "/Users/kartikaiyer/temp.pict") from 513 as picture
    set data of artwork 1 of current track to the_artwork
end tell
我收到一个错误,错误:-1731 消息:未知对象

类似的applescript代码如下所示:

imFile = open('/Users/kartikaiyer/temp.pict','r')
data = imFile.read()
it = app('iTunes')
sel = it.current_track.get()
sel.artworks[0].data_.set(data[513:])
tell application "iTunes"
    set the_artwork to read (POSIX file "/Users/kartikaiyer/temp.pict") from 513 as picture
    set data of artwork 1 of current track to the_artwork
end tell

我尝试使用ASTranslate,但它从未实例化\u artwork,然后在引用\u artwork

时抛出错误。快速猜测,Appscript引用,如AppleScript引用,使用1索引,而不是像Python列表那样的零索引。所以你可能需要写:

it.current\u track.artworks[1]。数据集(…)

(顺便说一句,原始脚本中的额外
get
命令是不必要的,尽管在这种情况下是无害的。)


对于ASTranslate,如果您希望它向应用程序和脚本添加发送命令并接收其结果,则需要启用“将事件发送到应用程序”复选框。通常,最好禁用此选项,以便在翻译具有潜在破坏性的命令(如
set
delete
)时不会发生任何不幸的事故,因此仅在您确实需要时启用此选项,并注意在执行此操作时运行的代码。

快速猜测,Appscript引用,像AppleScript引用一样,使用1索引,而不是像Python列表那样使用零索引。所以你可能需要写:

it.current\u track.artworks[1]。数据集(…)

(顺便说一句,原始脚本中的额外
get
命令是不必要的,尽管在这种情况下是无害的。)


对于ASTranslate,如果您希望它向应用程序和脚本添加发送命令并接收其结果,则需要启用“将事件发送到应用程序”复选框。通常,最好禁用此选项,以便在翻译具有潜在破坏性的命令(如
set
delete
)时不会发生任何不幸的事故,因此仅在您确实需要时启用此选项,并注意在执行此操作时运行的代码。

read
命令是脚本添加的一部分,ASTranslate无法转换为。使用ASDictionary为脚本添加创建粘合剂,方法是单击Dictionary菜单下的“选择已安装的脚本添加”,然后从列表中选择“脚本添加”。读取命令是脚本添加的一部分,ASTranslate不会转换为。使用ASDictionary为脚本添加创建粘合剂,方法是单击Dictionary菜单下的“选择已安装的脚本添加”,然后从列表中选择“脚本添加”。

这是一个较老的问题,但由于我现在在做同样的事情时遇到困难,我想我会发布我的解决方案,以防其他人会受益

selected = appscript.app('iTunes').selection.get()

for t in selected:
    myArt = open(/path/to/image.jpg,'r')
    data = myArt.read()
    t.artworks[1].data_.set(data) # no need to remove header but one-indexed as has said earlier
    myArt.close()

希望这能有所帮助。

这是一个老问题,但由于我现在在做同样的事情时遇到困难,我想我应该发布我的解决方案,以防其他人受益

selected = appscript.app('iTunes').selection.get()

for t in selected:
    myArt = open(/path/to/image.jpg,'r')
    data = myArt.read()
    t.artworks[1].data_.set(data) # no need to remove header but one-indexed as has said earlier
    myArt.close()

希望这能有所帮助。

Appscript使用零索引,因为它是Python,而不是AppleScript。Appscript使用一个索引,因为Apple事件管理器就是这样使用的。Appscript代码可能看起来像Python,但相似之处相当肤浅——只是Apple事件管理器API周围的一层薄薄的语法糖,它决定了实际的语义。Appscript使用零索引,因为它是Python,而不是AppleScript。Appscript使用一个索引,因为Apple事件管理器使用的是这种索引。Appscript代码可能看起来像python,但相似之处相当肤浅——只是Apple事件管理器API周围的一层薄薄的语法糖,它决定了实际的语义。您可能需要添加一个显式的数据类型:
t.artworks[1].data_uu.set(AE::AEDesc.new(KAE::TypeJPEG,data))
您可能需要添加一个显式的数据类型:
t.artworks[1].data_u.set(AE::AEDesc.new(KAE::TypeJPEG,data))