3

I want to set the rating of a track in Swift 2.3/3 using the iTunes ScriptingBridge framework. I have the Swift iTunes header with the protocol definition as this (full header here):

@objc protocol iTunesTrack: iTunesItem {
    @objc optional var album: NSString {get set}
    @objc optional var albumArtist: NSString {get set}
    @objc optional var rating: Int {get set}
}

extension SBObject: iTunesTrack {}

But when I try to set the rating:

var iTunesApp = SBApplication(bundleIdentifier: "com.apple.iTunes") as! iTunesApplication
var currentTrack = iTunesApp.currentTrack
currentTrack.rating = 10

Xcode compiler gives me this error:

Cannot assign to property: 'currentTrack' is inmutable

How I make it mutable? I know this code works in Objective-C but I cannot make it work in Swift and it doesn't work using the Objective-C header and a Objective-C Bridging Header.

pvieito
  • 117
  • 2
  • 8

1 Answers1

2

I use the setRating method in the Objective-C code, I can't use the rating property to set the rating.

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];
[[iTunes currentTrack] setRating:60];

Same in Swift with the iTunes.h file.


This works on Swift v2.2:

  • In your "iTunes.swift" file, you must add the setRating function
  • @objc optional var rating: Int {get}    
    @objc optional func setRating(_: Int)
    

  • The Swift code:

  •    let iTunesApp = SBApplication(bundleIdentifier: "com.apple.iTunes")  as! iTunesApplication
       let currentTrack = iTunesApp.currentTrack
       currentTrack!.setRating!(10)
    
jackjr300
  • 7,111
  • 2
  • 15
  • 25