如何使用AppleScriptObjC获取Spotify应用程序的图像和跟踪名称

时间:2016-06-06 15:05:19

标签: xcode cocoa applescript applescript-objc

我正在使用Xcode中的Cocoa Applescript创建一个新的cocoa应用程序,我想从spotify获取跟踪详细信息,以便在我的应用中呈现图像。我创建了一个简单的窗口,旁边有两个按钮,上面有2个文本标签,用于艺术家姓名和曲目名称。我设法从脚本编辑器中获取轨道详细信息和图像URL,但是我无法以某种方式将其存储在Xcode中。

下一个和上一个按钮有效,但标签和图像(ImageView)上没有显示任何内容。

p / s:我将所有插座连接到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

我希望有人可以帮助我。这实际上是一个用xcode学习applescript的测试应用程序。提前谢谢!

2 个答案:

答案 0 :(得分:1)

tell artist1 to get artist...更改为set artist1 to get artist..

现在,artist1具有有效的字符串值,并且如果它正确连接到NSTextField,它应该显示在文本字段中。您还可以像这样简化Spotify代码:

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)

答案 1 :(得分:0)

这是一种使用AppleScript-ObjectiveC语法将图稿url字符串转换为NSImage以在UI中显示的方法。 imgAlbumCover是将显示图像的NSImageView。

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

请记住在Info.plist中启用App Transport Security,以便您可以发出http://请求。 (NSAppTransportSecurity - > NSAllowsArbitraryLoads)