I would like to play and pause VLC with Keyboard Maestro and Stream Deck.
When I use the script in Keyboard Maestro, I want to change the icon on the Stream Deck.
For that I should know if VLC is playing or not.
For now I just found this:
tell application VLC
play
end tell
But with that the script doesn’t know if it’s playing or paused.
I just put my toe in this scripting world, but I love it.
Set a variable “VLCIsPlaying”
Then ask VLC if it is playing or not
Store it in the variable
then:
if VLCIsPlaying is true then
pause VLC
change icon to play
else
play VLC
change icon to pause
end if
isPlaying is false when it is paused, and true when it is playing.
The command to pause or play is the same command “play”
So no need to check is playing, the play command will pause an already playing video, and will play a paused video.
Based on the bit, containing “running and playing”, I assume the context allows you to merge the two actions by going the shell way with the simple one-liner below wrapped in a handler. It checks CPU usage to the first decimal digit. When VLC is playing media, the usage spikes to significant numbers, usually in tenths of units, and falls to single digits, which are distinctly low values, or none otherwise.
on player_cpu(_process)
return (do shell script "ps -acx -o %cpu,comm | grep -i " & quoted form of _process & " | awk -F'[. ]' '{if ( $2 ) print $2\",\"$3 ; else print $3\",\"$4;}'") as number
end player_cpu
where _process is the application or the process you’re gauging.
A caveat is the decimal separator, as it depends on the default number format setting in the System Preferences. If it’s a dot, you must change it to a comma before passing it to AppleScript, hence the ‘F[. ]’ option after awk followed by the comma escapes.
If the separator is a comma, you should omit that option, leaving the remainder of the command alone.
The advantage of this approach is that you avoid calls to the application from within AppleScript.
I see I should’ve expanded. I presumed the OP knew the basics of the scripting logic and main concepts.
The handler is a collection of statements that aren’t doing anything by themselves. To activate those, you need to run the handler (or call the handler in the developer parlance). Also, since it’s AppleScript, you should’ve put it in the Execute AppleScript macro, not Execute Shell Script.
To do this, you just include the segment «my player_cpu(“VLC”)» (without the enclosing quotes) in the AppleScript script by placing it anywhere inside the Execute AppleScript KM action.
Since we want the value (a real number) returned by this handler, we either leave the segment as is or assign it to a variable, as in «set _cpu to my player_cpu(“VLC”)». We then manipulate that value, massaging our flow as we see fit.
In our case, that value gets assigned to the variable _cpu, which then goes to the output of the Keyboard Maestro action that’s set to store the action’s return, a string, in the variable VLCplaying. This reduces the KM script actions in your example to the single Execute AppleScript.
The CPU architecture has nothing to do with that. The problem may lie in the first action that fails to acquire values.
When you said “does nothing”, what did you mean? Was there no result or an error?
To check for irregularities, highlight the code to include “on player_cpu…end player_cpu” and “set _cpu to my player_cpu”, and choose one of “Get AppleScript Result”, “Run as AppleScript” or “Make new AppleScript” from the right-click menu. VLC should be running. Test this snippet with playback started and paused.
Also, what do you see in the box “Decimal” when you go to System Settings->Language & Region->Advanced->General? Is it a dot or a comma?
Another possible reason could be the shell flavour. I use bash, however, all the newest macOS versions default to zsh where things may go differently. Naturally, I haven’t tested with other shells because I don’t use them.