[英]Python osascript returning 0 it seems
我正在嘗試使用applescript來檢索歌曲的BPM值。 最終,我想用一個游戲來實現它。 這是我的代碼:
import os
import time
import sys
def getBPM():
iTunesInstruct = """'
tell application "iTunes"
set k to get bpm of current track
end tell
return k
'"""
bpm = os.system('arch -i386 osascript -e ' + iTunesInstruct )
#bpm =90
bpm = int(bpm)
bpm = round(bpm)
if bpm > 250:
bpm = 200
return bpm
def getBeatSecond(bpm):
bps = float(bpm) / 60
#raw_input(bps)
return float(bps)
i = 0
beatMatch = True
while True:
beat = 1 / getBeatSecond(getBPM()) # BPS Beat divided by a second.
if beatMatch:
time.sleep(beat)
print beat
else:
raw_input('Go??')
i += 1
if i > 50:
break
但這似乎只起作用一次...它得到了我正在聽的歌曲的BPM,看到是94,然后在第二次迭代中似乎以為是0,然后除以0並消失了。 這是怎么回事?
os.system不等待命令完成。
90 =是osascript的結果, 0 =沒有錯誤,是退出狀態( os.system )。
使用subprocess.Popen
from subprocess import Popen, PIPE
def getBPM():
cmd = "arch -i386 osascript -e " + """'tell application "iTunes" to return bpm of current track'"""
bpm, tError = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE).communicate()
if bpm > 250:
return = 200
return int(bpm)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.