监控窗口标题变化的脚本

Thi*_*lem 3 notify-osd music-player

事情是这样的:我使用lastfm包来听我的Last.FM(废话)...并且我想在新音乐开始时显示一些notify-osd通知。

只要程序不支持它,我想创建一个shell脚本来监视lastfm播放器的窗口标题并在其更改时发送通知。

你怎么认为?我该如何开始?

ænd*_*rük 5

您可以使用wmctrl检查打开窗口的标题:

\n\n
$ wmctrl -lx | grep "last.fm.Last.fm" | grep -Po "(?<=$HOSTNAME ).*$"\nInsomnium \xe2\x80\x93 Death Walked the Earth\n
Run Code Online (Sandbox Code Playgroud)\n\n

不过,使用此方法观看标题需要脚本定期轮询更改,这会很混乱且效率低下。

\n\n

更聪明的方法是等待 Last.fm 播放器生成的某种事件。D-Bus 接口或类似的 API 是理想的选择,但我没有看到任何文档表明存在这样的接口。作为次优选择,您可以尝试观察终端输出以获取有关玩家正在做什么的信息:

\n\n
#!/bin/bash\n\ngrabnextline=0\nalbumart="$(mktemp)"\n\nwhile read line; do\n    if [ $grabnextline = 1 ]; then\n        title="$(echo "$line" | cut -d\\" -f2)"\n        wget -O "$albumart" "$(grep -A 10 "<title>$(echo "$title" | awk -F \' \xe2\x80\x93 \' \'{print $2}\')" ~/.local/share/Last.fm/Last.fm.log | grep -Eo "http://.*\\.(jpg|png)" | sed \'s/174s/64s/\' | sed \'s/jpg/png/\')"\n        notify-send "$title" --icon "$albumart"\n        grabnextline=0\n    fi\n    if [[ "$line" = *ScrobblerManager::nowPlaying* ]]; then\n        grabnextline=1\n    fi\ndone < <( /usr/bin/lastfm 2>&1 )\n\nrm "$albumart"\n\nexit\n
Run Code Online (Sandbox Code Playgroud)\n\n

这仍然是一个丑陋的黑客,但它完成了工作。

\n