AppleScript список записей в Swift Array (или лучше) - PullRequest
0 голосов
/ 20 января 2019

Я тестирую небольшое приложение OSX на основе этого примера , которое использует мост к AppleScript для получения простой информации из iTunes.

Как я уже сказал в комментарии ComboDrums (это я),У меня есть скрипт, чтобы получить все мои списки воспроизведения iTunes в дереве, но как только возвращение становится более сложным, чем простая строка, происходит сбой.

Поэтому я ищу способ преобразования возвращаемого списка AppleScriptк дружественному объекту Swift.

Есть идеи?

Thx.

Сценарий:

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

1 Ответ

0 голосов
/ 22 января 2019

Во-первых, создайте класс или структуру swift с помощью конструктора, имеющего NSDictionary в качестве параметра

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, преобразуйте список сценариев Apple в 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)

Вывод: опционально («Библиотека»)

...