How to detect if media is playing?

Howdy all. I would like to figure out a way to detect if media is playing within an app. For instance, I listen to music in iTunes in between phone calls. But once a call comes through I run a macro that does a variety of things including pausing that music. Once I'm done, I run another macro that basically lets me pick up where I left off including resuming the music.

However, sometimes I am NOT listening to music and don't want it to start playing after a phone call. Lately I watch tv episodes in Quicktime in between calls, and therefore would like to resume that episode after the call.

So is there a way to detect which media player is currently playing media and go back to that one with a macro? I thought of using an If-this-then command and having it set a variable if an app is running, but both QT and iTunes are generally running (albeit in the background) so that variable would be set for both. I don't see a way to have it set a variable only if one of those apps is currently playing media.

Any ideas for me? Thanks in advance!

A bit of AppleScript will help you out here. Thankfully, iTunes and QuickTime Player are scriptable and having properties that report their player state. Spotify and VLC also have similar properties:

tell application "QuickTime Player" to get playing of documents contains true
tell application "iTunes" to get player state is playing
tell application "Spotify" to get player state is playing
tell application "VLC" to get playing

Each of those lines will return true if the application in question is currently playing. You can use these in isolation or combination as part of your macro workflow, once you retrieve and store the values in a KM variable.

Cool thanks for the script. I had found another AS that would find the currently playing media player, set it, then resume it but 1) it was way more complicated than your script and 2) I couldn't get it to work haha.

One thing, when I run this script while iTunes is playing and QT is paused the only result it returns is false even though iTunes is playing. Is there more script than what is in your example above? I thought every tell had to end with an "end tell"?

Also, I'm new at AS (KM too but I'm getting the hang of it now), how would I then use this script to set a variable? For example, suppose iTunes returned "true" and QT "false"; what's the best/easiest way to use those to set my "Current Media" variable?

Sorry for all the questions! I google and search through forums but sometimes I can't find anything relevant and can't figure it out on my own haha.

Hi @cdthomer,

I'll copy-n-paste from the AppleScript Language Guide, which explains it better than I can:


A statement is a series of lexical elements that follows a particular AppleScript syntax. Statements can include keywords, variables, operators, constants, expressions, and so on.

Every script consists of statements. When AppleScript executes a script, it reads the statements in order and carries out their instructions.

A control statement is a statement that determines when and how other statements are executed. AppleScript defines standard control statements such as if, repeat, and while statements, which are described in detail in Control Statements Reference.

A simple statement is one that can be written on a single line:

set averageTemp to 63 as degrees Fahrenheit

Note: You can use a continuation character (¬) to extend a simple statement onto a second line.

A compound statement is written on more than one line, can contain other statements, and has the word end (followed, optionally, by the first word of the statement) in its last line. For example the following is a compound tell statement:

tell application "Finder"
   set savedName to name of front window
   close window savedName
end tell

A compound statement can contain other compound statements.

So, I've used the tell application command in the form of a simple statement, by including the word to.

My apologies, I didn't realise you were relatively new to AppleScript.

If you ran those four lines exactly as I wrote them out, then they each get executed in turn, but only the very last command returns the result that you see, i.e. false, since VLC presumably was not playing.

Use the lines in isolation, according to your needs, such as:

tell application "iTunes" to get player state is playing

Alternatively, store the results from each in an AppleScript variable, and refer to them appropriately to get the result you require:

tell application "QuickTime Player" to set isQTplaying to (playing of documents contains true)
tell application "iTunes" to set isiTunesPlaying to (player state is playing)
tell application "Spotify" to set isSpotifyPlaying to (player state is playing)
tell application "VLC" to set isVLCplaying to playing

return [isQTplaying, isiTunesPlaying]

which will have returned {false, true} in your example.

As an example, this identifies which applications are playing, and stores the result in the KM variable called Result, which is then tested to determine whether it contains "iTunes" or "QuickTime Player", in which case the playing tracks are paused.

set A to {}

tell application "QuickTime Player" to if it is running and (playing of documents contains true) then set end of A to "QuickTime Player"

tell application "iTunes" to if it is running and (player state is playing) then set end of A to "iTunes"

tell application "Spotify" to if it is running and (player state is playing) then set end of A to "Spotify"

tell application "VLC" to if it is running and playing then set end of A to "VLC"


You could, of course, do the whole thing in AppleScript, but I thought I'd mix it up a bit.


My apologies, I didn't mention I'm a rookie when it comes to AS.

Your script works PERFECT! It's so simple too. Thanks so much for taking the time to break it down and write a detailed explanation!