Here’s the macro I use for pausing/resuming a variety of media players. I have used it with Apple Music, TV, QuickTime Player, VLC, Spotify and Podcasts.
Right now, the AppleScript portions that control Spotify, VLC and Podcast apps are commented out because I do not have those applications installed currently. You will need to uncomment them if you want to use those apps, and comment out any portions for the apps you don’t use. You must comment out (or delete) portions that refer to apps you do not have installed, otherwise the AppleScript will fail. If that doesn’t make sense, just let me know which apps you do and do not use, and I’ll adjust it for you.
DISCLAIMER: This macro will set and use a global variable called DND__Current Media
, so if you see that variable in your preferences window (or elsewhere), know that it is used by this macro.
Download Macro(s): 01)[MG-SUBRT] Current media player pause-resume (for export).kmmacros (18 KB)
Macro-Notes
- Macros are always disabled when imported into the Keyboard Maestro Editor.
- The user must ensure the macro is enabled.
- The user must also ensure the macro's parent macro-group is enabled.
System Information
- macOS 13.6.3
- Keyboard Maestro v11.0.2
AppleScript to pause media (click to expand/collapse)
------------------------------------------------------------
# Author : Chris Thomerson (@cdthomer)
#
# Version : 3.2.0 (Now retrieves KM variable before execution)
# History : 3.1.0 (Determines if apps are running without activating them)
# 3.0.0 (Added loop and error handling)
# 2.0.0 (Set Keyboard Maestro variable to indicate finished [DEPRECATED])
# 1.3.0 (Added hiding TV app)
# 1.2.0 (Added support for Podcasts app)
# 1.1.0 (Updated variable handling to set KM variable only if variable has content)
# 1.0.0 (Initial script)
#
# Created : Sunday, February 27, 2022
# Modified : Thursday, July 27, 2023
# macOS : 12.6.3 (Monterey) - macOS 13.4.1 (Ventura)
# Tags : Music, TV, QuickTime, Spotify, VLC, Podcasts
#
# PURPOSE
# Determine which media player is currently playing, pause
# it, and set a Keyboard Maestro variable for later use.
#
# DISCLAIMER
# Permission to use, copy, modify, and/or distribute this
# software for any purpose with or without fee is hereby
# granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
# ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
# USE OR PERFORMANCE OF THIS SOFTWARE.
------------------------------------------------------------
# setup variables for loop and error handling
set apscDone to false
set loopCount to 0
# repeat script until successful or timesout
repeat until apscDone is true or loopCount is 15
try
# get current media variable from Keyboard Maestro
tell application "Keyboard Maestro Engine"
set currMedia to getvariable "DND__Current Media" --retrieves Keyboard Maestro variable
end tell
# Apple Music
set appName to "Music"
if application appName is running then
tell application "Music"
if it is running and (player state is playing) then
set currMedia to "Music"
pause
end if
end tell
end if
# Apple TV
set appName to "TV"
if application appName is running then
tell application "TV"
if it is running and (player state is playing) then
set currMedia to "TV"
# Hide TV
tell application "System Events"
tell application process "TV"
set visible to false
end tell
end tell
pause
end if
end tell
end if
# QuickTime Player
set appName to "QuickTime Player"
if application appName is running then
tell application "QuickTime Player"
if it is running and (playing of documents contains true) then
set currMedia to "QuickTime Player"
pause front document
end if
end tell
end if
(*
# Podcasts
set appName to "Podcasts"
if application appName is running then
if application "Podcasts" is running then
tell application "System Events"
tell application process "Podcasts"
if menu item "Pause" of menu "Controls" of menu bar 1 exists then
set currMedia to "Podcasts"
click menu item "Pause" of menu "Controls" of menu bar 1
end if
end tell
end tell
end if
end if
*)
(*
# Spotify
set appName to "Spotify"
if application appName is running then
tell application "Spotify"
if it is running and player state is playing then
set currMedia to "Spotify"
pause
end if
end tell
end if
*)
(*
# VLC
set appName to "VLC"
if application appName is running then
tell application "VLC"
if it is running and playing then
set currMedia to "VLC"
play # don't let this fool you, it's the same command for play and pause
end if
end tell
end if
*)
# set media variable to blank if nothing is playing
if currMedia is {} then
set currMedia to ""
end if
# set Keyboard Maestro current media variable
tell application "Keyboard Maestro Engine"
setvariable "DND__Current Media" to currMedia # sets Keyboard Maestro variable
end tell
set apscDone to true
on error
# set variables to continue or exit loop
set apscDone to false
set loopCount to loopCount + 1
end try
end repeat
return currMedia
AppleScript to resume media (click to expand/collapse)
------------------------------------------------------------
# Author : Chris Thomerson (@cdthomer)
#
# Version : 4.0.0 (Now hides TV controls)
# History : 3.1.0 (Will resume only if app is running upon resuming)
# 3.0.0 (Added loop and error handling)
# 2.0.0 (Set Keyboard Maestro variable to indicate finished)
# 1.2.0 (Added support for Podcasts app)
# 1.1.0 (Updated variable handling to set KM variable only if variable has content)
# 1.0.0 (Initial script)
#
# Created : Sunday, February 27, 2022
# Modified : Thursday, May 18, 2023
# macOS : 12.6.3 (Monterey)
# Tags : Music, TV, QuickTime, Spotify, VLC, Podcasts
#
# PURPOSE
# Determine from a Keyboard Maestro variable which media
# player was previously paused and resume it’s playback.
#
# DISCLAIMER
# Permission to use, copy, modify, and/or distribute this
# software for any purpose with or without fee is hereby
# granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS
# ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
# USE OR PERFORMANCE OF THIS SOFTWARE.
------------------------------------------------------------
use framework "Foundation"
use framework "CoreGraphics"
# setup variables for loop and error handling
set apscDone to false
set loopCount to 0
# repeat script until successful or timesout
repeat until apscDone is true or loopCount is 15
try
tell application "Keyboard Maestro Engine"
set currMedia to getvariable "DND__Current Media" # retrieves Keyboard Maestro variable
end tell
if currMedia is "Music" or currMedia is "" then
if application currMedia is running then
tell application "Music"
play
end tell
end if
end if
if currMedia is "TV" then
if application currMedia is running then
tell application "TV"
# play TV
play
# Enable TV track clock macro
tell application "Keyboard Maestro"
set enabled of macro id "0ADCE6C1-8FE5-42A1-BC0E-1AD063944C2D" to true
end tell
# get mouse coordinates via Keyboard Maestro
tell application "Keyboard Maestro Engine" to set mousePosStr to process tokens "%CurrentMouse%"
# extract individual integers from string
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {","}
set mouseCoor to text items of mousePosStr
set AppleScript's text item delimiters to saveTID
# set individual variables
set {mouseX, mouseY} to mouseCoor
# get coordinates for front TV window
tell application "System Events"
tell application process "TV"
if front window exists then
tell front window
set {xSize, ySize} to its size
set {xPos, yPos} to its position
end tell
end if
end tell
end tell
# set coordinates for center of front TV window
set xCur to xPos + (xSize / 2)
set yCur to yPos + (ySize / 2)
# move mouse to center of front TV window
set cursorPoint to current application's NSMakePoint(xCur, yCur)
set theError to current application's CGWarpMouseCursorPosition(cursorPoint)
# set mouse back to original position
set cursorPoint to current application's NSMakePoint(mouseX, mouseY)
set theError to current application's CGWarpMouseCursorPosition(cursorPoint)
end tell
end if
end if
if currMedia is "QuickTime Player" then
if application currMedia is running then
tell application "QuickTime Player"
play front document
end tell
end if
end if
(*
if currMedia is "Podcasts" then
if application currMedia is running then
tell application "System Events"
tell application process currMedia
click menu item "Play" of menu "Controls" of menu bar 1
end tell
end tell
end if
end if
*)
(*
if currMedia is "Spotify" then
tell application "Spotify"
play
end tell
end if
*)
(*
if currMedia is "VLC" then
tell application "VLC"
play
end tell
end if
*)
tell application "Keyboard Maestro Engine"
setvariable "DND__Current Media" to "" --sets Keyboard Maestro variable
end tell
set apscDone to true
on error
# set variables to continue or exit loop
set apscDone to false
set loopCount to loopCount + 1
end try
end repeat