2016-06-06 13 views
0

Ich erstelle eine neue Kakao-App mit dem Cocoa Applescript in Xcode und möchte die Trackdetails von spotify abrufen, um das Bild in meiner App zu rendern. Ich habe ein einfaches Fenster mit zwei Schaltflächen next und prev mit 2 Textbeschriftungen für Künstlername und Titelname erstellt. Ich habe es geschafft, die Track-Details und die Bild-URL aus dem Skript-Editor zu bekommen, aber ich kann es irgendwie nicht in Xcode speichern.Wie man Image und Trackname der Spotify App mit AppleScriptObjC erhält

Die nächste und vorherige Schaltfläche funktionieren, aber die Beschriftungen und das Bild werden nicht angezeigt (ImageView).

p/s: Ich habe alle Ausgänge an xib angeschlossen.

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 

Ich hoffe, dass mir jemand helfen kann. Dies ist eine Test-App zum Erlernen von AppleScript mit xcode. Danke im Voraus!

Antwort

1

Ändern tell artist1 to get artist... zu set artist1 to get artist...

Nun, da artist1 einen gültigen String-Wert hat, und WENN es richtig mit dem NSTextField verkabelt ist, sollte es im Textfeld erscheinen. Sie können auch Ihren Spotify Code wie so vereinfachen:

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) 
+0

danke Ron Reuter. Jetzt muss ich herausfinden, wie man das Bild setzt. Danke noch einmal! –

0

Hier ist ein Weg, um die Grafik-URL-Zeichenfolge in eine NSImage für die Anzeige in der Benutzeroberfläche, mit Applescript-Objective-Syntax zu transformieren. imgAlbumCover ist die NSImageView, die das Bild anzeigen wird.

use framework "Foundation" 
use framework "AppKit" 

tell application "spotify" 
    tell current track 
     set tArtworkURL to artwork url 
    end 
end tell 

set coverURL to current application's NSURL's URLWithString:tArtworkURL 
set imageData to current application's NSData's dataWithContentsOfURL:coverURL 
set coverImage to current application's NSImage's alloc()'s initWithData:imageData 

set imgAlbumCover's image to coverImage 

Denken Sie daran, App Transportsicherheit in Info.plist zu aktivieren, damit Sie die http machen können: // Anfrage. (NSAppTransportSecurity -> NSAllowsArbitraryLoads)

+0

Danke Ron für den Code! Ich habe dies verwendet, 'setURL auf NSURLs URLWithString_ der aktuellen Anwendung (url1) setzen theImage auf die NSImage's alloc initWithContentsOfURL_ (theURL)' –

+0

Danke für Ihre Hilfe Ron! –