Hi @cdthomer,
I'll copy-n-paste from the AppleScript Language Guide, which explains it better than I can:
Statements
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.
AppleScript
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"
A
You could, of course, do the whole thing in AppleScript, but I thought I'd mix it up a bit.