Search code examples
xcodecocoaapplescriptapplescript-objc

How to get image and track name of Spotify app using AppleScriptObjC


I'm creating a new cocoa app using the Cocoa Applescript in Xcode and i want to get the track details from spotify as to render the image in my app. I created a simple window with two button next and prev with 2 text labels for artist name and track name. I managed to get the track details and the image url from Script Editor, however i cannot somehow store it in Xcode.

The next and prev button work but nothing is showing at the labels and the image (ImageView).

p/s : I linked all the outlet to xib.

script AppDelegate
property parent : class "NSObject"

-- IBOutlets
property theWindow : missing value
property labelOne : missing value
property labelTwo : missing value
property buttonNext : missing value
property buttonPrev : missing value
property imageOne : missing value
property ourWeb : missing value

property artist1 : "default1"
property artist2 : "default2"
property url1 : "defaulturl"

tell application "spotify"
    set url1 to get artwork url of current track as string
    tell artist1 to get artist of current track as string
    set track1 to get name of current track
    labelOne's setStringValue_(artist1)
end tell

on nextClicked_(sender)
    tell application "spotify" to next track
end nextClicked_

on prevClicked_(sender)
    tell application "spotify" to previous track
end prevClicked_
end script

I hope someone can help me. This is actually a test app for learning applescript with xcode. Thanks in advance!


Solution

  • Change tell artist1 to get artist... to set artist1 to get artist...

    Now that artist1 has a valid string value, and IF it is wired-up to the NSTextField properly, it should show up in the text field. You can also simplify your Spotify code like so:

    tell application "Spotify"
        tell current track
            set name1 to name
            set artist1 to artist
            set url1 to artwork url
        end tell
    end tell
    
    labelOne's setStringValue_(artist1)