0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AppleScriptでメディアの再生状態を取得する

0
Last updated at Posted at 2022-05-05

AppleScriptの資料少ない気がする(のは気のせい?)

個人的な目的:AmazonMusicを夜は再生状態なら停止させたい。

Amazon MusicをAppleScript(Automator)で停止させる。(22.08.02時点)

AppleScript初心者の私が分かった事は、AppleScriptを使えば、個別のアプリケーションのGUI情報の取得、アプリケーションの制御できそうな事。・・・セキュリティ的に危なくないか?と思いました。(当たり前の事を言ってる?)
「セキュリティとプライバシー」の設定は慎重に。

tell application "System Events"
	tell process "Amazon Music"
		set amazonMusicState to name of menu item 1 of menu 1 of menu bar item 5 of menu bar 1
			
		if amazonMusicState is "一時停止" then
				
			tell application "Amazon Music"
				activate
				tell application "System Events"
					key code 49
				end tell
			end tell
		end if
	end tell
end tell

メニューバーの再生状態の表示が切り替わるので、それを利用して停止させてます。

参考資料

コントロールセンターで取得する方法

tell application "System Events"
	tell its application process "ControlCenter"
		click menu bar item "コントロールセンター" of menu bar 1
		try
			set btns to button "一時停止" of window "コントロールセンター"
			set isPlaying to true
		on error
			set isPlaying to false
		end try
		click menu bar item "コントロールセンター" of menu bar 1
	end tell
end tell

何か他の方法がある気もします。ご指摘があれば修正します。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?