2017-01-09 12 views

答えて

6

を、私は、Pythonを使用して、現在再生中のSpotifyは曲を取得する方法する必要がありますが、MacOSのシエラに、私はWindows上でそれを行う方法を知っているが、私はMacOSの

のためのそれのためのモジュールを見つけることができない私は、あなたのMacに通知として表示する現在の再生曲名とアーティストを取得するためのPythonコードを書きました。また、さまざまな変数にアクセスし、次の曲の機能を再生/一時停止することもできます。

この変数は、アプリケーション/ Spotify.app/Contents/Resources/Spotify.sdefファイルで検索できます。したがって、Pythonアプリケーションでそれらを使用することができます。

編集:Pythonや他の言語を使用してSpotifyを制御するには、あなたのPythonアプリケーションでAppleScriptを使用する必要があります。ここで

Spotify Developer page for AppleScript API

私のコードです:

#! /usr/bin/python 
from Foundation import * 

apple_script_code = """ 
set currentlyPlayingTrack to getCurrentlyPlayingTrack() 
displayTrackName(currentlyPlayingTrack) 

on getCurrentlyPlayingTrack() 
    tell application "Spotify" 
     set currentArtist to artist of current track as string 
     set currentTrack to name of current track as string 

     return currentArtist & " - " & currentTrack 
    end tell 
end getCurrentlyPlayingTrack 

on displayTrackName(trackName) 
    display notification trackName 

    delay 1 

end displayTrackName 
""" 

s = NSAppleScript.alloc().initWithSource_(apple_script_code) 
s.executeAndReturnError_(None) 
関連する問題