【问题标题】:AppleScript list of records to Swift Array (or better)AppleScript 记录到 Swift 数组(或更好)的列表
【发布时间】:2019-06-14 02:31:09
【问题描述】:

我测试了一个基于this example 的小型 OSX 应用程序,它使用 AppleScript 的桥接从 iTunes 获取简单信息。

就像我在 ComboDrums 的评论中所说的(是我),我有一个脚本可以将我所有的 iTunes 播放列表放在树中,但是一旦返回比简单的字符串更复杂,它就会失败。

所以我正在寻找将返回的 AppleScript 列表转换为 Swift 友好对象的方法。

有什么想法吗?

谢谢。

脚本:

to getStaticPlaylistsTree()
    tell application "iTunes"
        set theList to {{theName:"Bibliothèque", theID:"66270731FDBE2C50", isFolder:false, theClass:library playlist, isSmart:false, theCount:37581}, {theName:"Clips vidéo", theID:"07D5032B96891D67", isFolder:false, theClass:user playlist, isSmart:true, theCount:283}}
    end tell
    return theList
end getStaticPlaylistsTree

【问题讨论】:

  • 请发布您的代码。
  • 代码在项目示例中(github.com/hhas/Swift-AppleScriptObjC)。我刚刚添加了一个方法来获取一些播放列表的静态树,例如: getStaticPlaylistsTree() 告诉应用程序“iTunes”将 theList 设置为 {{theName:"Bibliothèque", theID:"66270731FDBE2C50", isFolder:false, theClass:library playlist, isSmart:false, theCount:37581}, {theName:"Clips vidéo", theID:"07D5032B96891D67", isFolder:false, theClass:user 播放列表, isSmart:true, theCount:283}} end tell 返回 theList。谢谢。
  • 请在问题中发布格式化代码。
  • 我无法格式化代码,因为我无法使用我的帐户发布答案... ????很抱歉,但我不能。您可以在简单的 AppleScript 脚本中复制/粘贴代码吗?是一样的... getStaticPlaylistsTree() 告诉应用程序“iTunes”将 theList 设置为 {{theName:"Bibliothèque", theID:"66270731FDBE2C50", isFolder:false, theClass:library playlist, isSmart:false, theCount:37581} , {theName:"Clips vidéo", theID:"07D5032B96891D67", isFolder:false, theClass:user 播放列表, isSmart:true, theCount:283}} end tell return theList end getStaticPlaylistsTree

标签: arrays swift macos applescript itunes


【解决方案1】:

首先,使用以 NSDictionary 作为参数的构造函数创建 swift 类或结构

struct SwiftModel {

    // Declare attributes

    init(dictionary: NSDictionary) {
        self.isFolder = dictionary.value(forKey: "isFolder") as! Bool
        self.isSmart = dictionary.value(forKey: "isSmart") as! Bool
        self.theCount = dictionary.value(forKey: "theCount") as? Int
        self.theID = dictionary.value(forKey: "theID") as? String
        self.theName = dictionary.value(forKey: "theName") as? String
        self.theClass = (dictionary.value(forKey: "theClass") as? NSAppleEventDescriptor)
    }
}

然后,使用 flatMap 或 compactMap 将苹果脚本列表转换为 Swift Array。

let listFromAppleScript = // List returned from apple script i.e self.iTunesBridge.getStaticPlaylistsTree
let staticPlayListTree = listFromAppleScript?.compactMap({SwiftModel(dictionary: $0 as! NSDictionary)})
print(staticPlayListTree![0].theName)

输出:可选(“Bibliothèque”)

【讨论】:

  • 好的,谢谢。 CompactMap 返回 AppleScript 列表的树,而 flatMap 返回列表的平面数组,对吗?
  • 另外,在 iTunesBridge.swift 协议中声明的 getStaticPlaylistsTree 需要返回一个 [NSString:AnyObject]?就像在 trackInfo 中一样?谢谢。
  • getStaticPlaylists 的类型可以是 [NSDictionary] 或 [[String:Any]]
  • 在这种情况下,您可以使用 compactMap 或 flatMap 但要了解更多说明,您可以参考stackoverflow.com/questions/49291057/…
  • 好的,谢谢。但是地图如何获得孩子们的树呢?在我的示例中,没有孩子,但在最终的树中,theChildren 属性中有很多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
相关资源
最近更新 更多