2011-03-17 28 views
5

我想要執行與here所述相同的操作,但是使用shell腳本(最好在bash中)而不是python。似乎這樣的事情應該可以使用dbus-monitor,但我對dbus並不是很熟悉,我不清楚如何將解決方案中描述的概念應用到python問題並將它們應用到dbus-monitor工具。如何使用bash連續監視rhythmbox以便跟蹤更改

回答

12

這是我能找到的最簡單的方法:

#!/bin/bash 

interface=org.gnome.Rhythmbox.Player 
member=playingUriChanged 

# listen for playingUriChanged DBus events, 
# each time we enter the loop, we just got an event 
# so handle the event, e.g. by printing the artist and title 
# see rhythmbox-client --print-playing-format for more output options 

dbus-monitor --profile "interface='$interface',member='$member'" | 
while read -r line; do 
    printf "Now playing: " 
    rhythmbox-client --print-playing 
done 

它產生的輸出是這樣的:

Now playing: Daft Punk - Overture 
Now playing: Daft Punk - The Grid 

時啓動它還會打印當前播放的歌曲。如果這不是您想要的,請查看$line的內容並查看它是否包含NameAcquiredplayingUriChanged。如果它包含NameAcquired,請跳過它。

Python版本和這個bash版本的主要區別在於Python版本使用DBus來獲取播放的歌曲信息。我找不到使用bash做這件事的好方法,但rhythmbox-client --print-playing似乎很好。

+0

好的解決方法,謝謝! – jbeard4 2011-03-18 14:12:10

+1

這不起作用,rythmbox客戶端被丟棄... – 2012-04-24 03:00:17

+0

如果我不想連續監視;如果我想在關閉Rhythmbox後退出/關閉腳本,我該怎麼做?是否可以在腳本中使用,還是需要單獨的腳本? – 2016-08-15 18:59:48