gpt4 book ai didi

macos - 将 AppleScript 转换为 SwiftAutomation

转载 作者:行者123 更新时间:2023-12-03 16:40:20 24 4
gpt4 key购买 nike

我在这里找到的这个 AppleScript 可以满足我的需求:

tell application "iTunes"
set matchtrack to tracks in playlist 1 whose persistent ID is "C70EA9CDC276CB6D"
if matchtrack is not {} then
return name of item 1 of matchtrack
else
return "no track found"
end if
end tell

即根据持久ID查找轨道。我正在尝试使用Swift Automation让它在MacOS Cocoa Swift应用程序中工作作为 Apple 事件桥。我可以通过以下方式检索该值:

let trackID = try iTunes.tracks[1].persistentID.get()

我尝试过各种说法。这似乎最有希望:

let trackRow = try iTunes.tracks[ITUItem.persistentID == "C70EA9CDC276CB6D"].get() as ITUItem

当我运行它时,我收到错误:

Instance member 'persistentID' cannot be used on type 'ITUItem'

该框架充其量还处于测试阶段,因此可能是一个错误。还有什么建议可以尝试吗?我想问作者,但找不到联系方式。

这是应用程序生成的 sdef 文件的一部分。

item n : an item
properties
class_ (type, r/o) : the class of the item
container (specifier, r/o) : the container of the item
id (integer, r/o) : the id of the item
index (integer, r/o) : The index of the item in internal application order.
name (text) : the name of the item
persistentID (text, r/o) : the id of the item as a hexadecimal string. This id does not change over time.
properties (record) : every property of the item

Track 是 item 的子类。

修复

当我第一次尝试 @matt 的 ITUIts.persistentID 建议时,它无法编译。我收到错误:

Binary operator '==' cannot be applied to operands of type 'ITUItem' and 'String'

经过一番反复之后,我意识到问题是我缺少导入:

import SwiftAutomation

我最初有它,但不确定我是否需要它,所以我将其注释掉。在我调用这个之前,在没有它的情况下,其他十几个对它的调用都可以正常工作。

最佳答案

使用ITUIts并摆脱as ITUItem。所以:

let trackRow = try itunes.tracks[ITUIts.persistentID == "C70EA9CDC276CB6D"].get()
// result will be something along these lines:
// [ITunes().sources.ID(66).libraryPlaylists.ID(93639).fileTracks.ID(95018)]

这里是一个完整的工作测试以及我机器上的结果(当然你的数字会有所不同):

let itunes = ITunes()
let trackID = try itunes.tracks[1].persistentID.get() as String
print("track ID is", trackID)
// track ID is 689006177BB39343
let trackRows = try itunes.tracks[ITUIts.persistentID == trackID].get() as [ITUItem]
if let trackRow = trackRows.first {
print(trackRow)
// ITunes().sources.ID(66).libraryPlaylists.ID(93639).fileTracks.ID(95018)
}

关于macos - 将 AppleScript 转换为 SwiftAutomation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42724421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com