LoginSignup
0
0

Spotifyをcrontabからxdotoolで制御

Posted at

朝のアラーム用にspotifyを使いたかったので、crontabから再生・停止を制御できるようにしたメモ
xdotoolを使って制御する
spotifyは起動してある前提

xdotool search --name spotifyだと見えないウィンドウしか取得できなかったのでPIDからWindowを取得している

spotifyのホットキーがPlay/Stopのトグルキーなので、スクリプトを実行したときに再生されていれば止まるし、止まっていれば再生される

DISPLAY変数は環境に合わせて変更する
自分の環境ではGUIを切ってXRDP経由でSpotifyを起動している

以下スクリプト

#!/bin/bash
export DISPLAY=:10
pid=`ps -ef | grep /snap/spotify/75/usr/share/spotify/spotify | head -1 | awk '{print $2}'`
window_id=`xdotool search --onlyvisible --pid $pid`
xdotool windowactivate $window_id
xdotool key space
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0