1

I test a little OSX app based on this example which use a bridge to AppleScript to get simple infos from iTunes.

Like I said in the ComboDrums's comment (it's me), I have a script to get all my iTunes playlists in a tree but as soon as the return is more complex than a simple string, it fails.

So I'm looking for the way to convert the AppleScript's list returned to a Swift friendly object.

Any idea ?

Thx.

Script:

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
4
  • Post your code please. Commented Jan 20, 2019 at 15:10
  • The code is in the project example (github.com/hhas/Swift-AppleScriptObjC). I just added a method to get a static tree of some playlists like : 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. Thx. Commented Jan 20, 2019 at 15:16
  • Please post the formatted code in the question. Commented Jan 20, 2019 at 16:20
  • I can't format the code because I can't post answer with my account... 😂 So sorry but I can't. Can you copy/paste the code in a simple AppleScript script? It is the same... 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 Commented Jan 21, 2019 at 14:14

1 Answer 1

1

Firstly, Create the swift class or struct with a constructor having NSDictionary as parameter

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)
    }
}

Then, Using flatMap or compactMap convert apple script list to 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)

Output: Optional("Bibliothèque")

Sign up to request clarification or add additional context in comments.

8 Comments

Ok thank you. CompactMap return a tree of the AppleScript list and flatMap an flat array of the list, right ?
And also, the getStaticPlaylistsTree to declare in the iTunesBridge.swift protocol needs to return a [NSString:AnyObject]? like in the trackInfo ? Thx.
getStaticPlaylists can be of type [NSDictionary] or [[String:Any]]
in this scenario you can use either compactMap or flatMap but for more clarification you can refer stackoverflow.com/questions/49291057/…
Ok thank you. But how the map do to get the children’s tree ? In my example there is no children but in the final tree there are a lot in the theChildren property.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.