The jonki

呼ばれて飛び出てじょじょじょじょーんき

【Mac】MacでiTunesで再生中の曲をツイートする。

AppleScriptPythonで無理やり実行して、その結果を取得してtweepyでツイートしてます。tweepyはこちらの記事を参考に入れてみてください。

以下適当スクリプト

tweet.scpt
tell application "iTunes"
        set m_album to album of current track
        set m_artist to artist of current track
        set m_song to name of current track
        set m_info to m_song & " " & m_artist & " " & m_album
end tell
tweetSong.py
# /usr/bin/python
# -*- coding: utf-8 -*-

import tweepy
import os
import subprocess

consumer_key = 'xxx'
consumer_secret = 'xxx'

access_key = 'xxx'
access_secret = 'xxx'

def post(message):
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_key, access_secret)
        api = tweepy.API(auth_handler=auth)
        api.update_status(message)

def getCurrentSong():
        info = subprocess.check_output(['osascript ' + os.getcwd()+'/tweet.scpt'], shell=True)
        return info

def main():
        info = getCurrentSong()
        print info
        post(info)

main()