How do I write Applescript to use hotkeys to control VLC Media Player in the background while typing on Word or Pages? - applescript

I would like to control VLC Media Player using hotkeys while typing in Word or Pages (without having to have VLC Media Player be the active window).
Can I use AppleScript to do this? I am editing video transcripts and, at the moment, have to toggle between programs to control the active window. I would love to be able to press hotkeys to:
rewind
jump back 5 seconds
play/pause
fast forward
jump forward 5 seconds
Thank you so much for any help you can offer. I've never used AppleScript and appreciate any help.
I used a free hotkey assigner called Spark and inserted Applescript for these functions, following a tutorial on how to do this for transcription, but it didn't work. (I did it for Quicktime but would actually prefer to use VLC Media Player. Either would be great, though).
https://www.engadget.com/2008-06-26-simplify-media-transcription-with-hotkeys-and-applescript.html
Jump back:
tell document 1 of application "QuickTime Player" to set current time to (current time - 2500)
Play/pause:
tell document 1 of application "QuickTime Player"
if playing is true then
pause
else
start
end if
end tell
Jump forward:
tell document 1 of application "QuickTime Player" to set current time to (current time + 2500)

Related

Start and Save QuickTime screen recording with applescript

I have a very basic applescript that starts a QuickTime screen recording (or rather opens up the screen recording dialog box in QuickTime):
tell application "QuickTime Player"
new screen recording
end tell
How do I make a second applescript that stops the screen recording and opens up a Save As... dialog box in QuickTime?
This is a simple example of how to accomplish stopping a recording already underway and present the Save... dialog box (note that most if not all versions of QuickTime Player have a Save... option and not Save As...)
tell application "QuickTime Player"
new screen recording
delay 5
-- press record button
end tell
tell application "System Events"
-- stop the recording
key code 53 using {command down, control down}
-- wait for recorded window to appear
delay 5
keystroke "s" using command down
end tell
The answer to your question is the second tell block but I included the first tell block so that you can run this script as is and see the entire process in action.
The delay in the second tell block is so that the Save... command is not fired before QuickTime Player finishes rendering the screen recording and opening the video in a window, otherwise the Save... dialog box won't appear in the foreground or possibly won't appear at all.
Ideally this script should be developed further to wait until the screen recording has been rendered and opened in a window, rather than using a delay, but it answers your question in the most basic form.

Is there a way to pause a YouTube video in Google Chrome using AppleScript?

What I'm trying is to type tell application "Google Chrome" to pause and make audio or video in Google Chrome pause. It wouldn't work, since it thinks pause is a variable.
Is there any way to either pause audio or video in all applications or just Google Chrome?
From comments:
The following example AppleScript code will play/pause a YouTube video in the active tab of the front window of Google Chrome: tell application "Google Chrome" to tell active tab of front window to execute javascript "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
#user3439894 this one works perfectly, since I only want to pause the video in the active tab. Is there any way to check if the video is paused so it doesn't unpause a video? Also could you write this comment as an answer so I can accept it as the correct answer?
There may be an easier way using JavaScript to ascertain if the active tab of the front window in Google Chrome has a YouTube video playing, however, as I do not know the code for it, here is a way that works for me.
The following example AppleScript code, shown below, was tested in Script Editor under macOS Catalina 10.15.7 and macOS Big Sur 11.2.3 using Google Chrome (Version 90.0.4430.93 (Official Build) (x86_64)) with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Example AppleScript code:
if not running of ¬
application "Google Chrome" then return
tell application "Google Chrome"
if (window count) = 0 then return
set atx to the active tab index of its front window
end tell
tell application "System Events"
-- # macOS Big Sur System Events bug issue.
-- # If running macOS Big Sur, uncomment the next
-- # two lines if System Events reports an error.
-- run
-- delay 0.5
tell application process "Chrome"
if (the value of ¬
attribute "AXTitle" of ¬
radio button atx of ¬
tab group 1 of ¬
group 1 of ¬
the front window) ¬
does not end with "Audio playing" then return
end tell
end tell
tell application "Google Chrome" to ¬
tell the active tab of its front window to ¬
execute javascript ¬
"document.getElementsByClassName('ytp-play-button ytp-button')[0].click();"
Notes:
As coded, the execute javascript only happens if there is a YouTube video playing in the active tab of the front window of Google Chrome, thus pausing it. The code, as coded, cannot cause a paused video to play, regardless of the active tab or any tab of any window.
The -- # macOS Big Sur System Events bug issue. section in the example AppleScript code is an attempted workaround to a System Events issue in macOS Big Sur, in that the exact same code without it runs without issue in macOS Catalina and may not work or needs adjusting. I am still trying to troubleshoot the issue in general. In my experience the issue has not been reproducible at will and as such alternate methods may have to be used if issue continues and cannot be diagnosed.
Use of other than exact versions tested under may be partially to blame for some errors, especially any that involve a change in the hierarchical UI element structure in any UI Scripting scenario using System Events.
Is there any way to either pause audio or video in all applications or just Google Chrome?
Typically pressing the Play/Pause key on the keyboard should pause a video in Google Chrome, and most other apps as well.
It wouldn't work, since it thinks pause is a variable.
pause is not part of the AppleScript dictionary in Google Chrome and why it shows as a variable.
In Google Chrome, running AppleScript code containing JavaScript needs the Allow JavaScript from Apple Events menu item checked under: View > Developer -- Note that this used to be the default, however, at some time is was changed. For more information: https://support.google.com/chrome/?p=applescript
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.
This following AppleScript code will pause or play any instance of a YouTube video in any tab of any window in Google Chrome (whether visible or not).
pauseOrPlayYoutubeInChrome("ytp-play-button ytp-button", 0)
to pauseOrPlayYoutubeInChrome(theClassName, elementnum)
tell application "Google Chrome"
set youtubeTabsRef to a reference to (tabs of windows whose URL contains "youtube")
repeat with youtubeTabs in youtubeTabsRef
execute youtubeTabs javascript "document.getElementsByClassName('" & ¬
theClassName & "')[" & elementnum & "].click();"
end repeat
end tell
end pauseOrPlayYoutubeInChrome
My solution consists of running 2 AppleScript in a specific environment.
It works for me using 1 window of Brave Browser(I think it is the same for google chrome) with 2 tabs on youtube.
First script force opening Brave browser, second run keystroke "K"
tell application "Brave Browser"
if it is not running then launch
activate
end tell
tell application "System Events"
keystroke "k"
end tell
I have created a Quick Action on Automator and then I have followed this answer: https://apple.stackexchange.com/a/401262
And when trying to test it as a Keyboard Shortcut when using another Application, I was receiving an error like this "com.automator.runner.xpc is not allowed to send keystrokes", so I realised that I need to give accessibility privileges(on Security and Privacy) to all applications I want the script to work.
And After that, it works, but sometimes it gives the same error and it works again after some minutes(and never come back again) so I think this can help someone too.
This will pause or play the youtube video on the tab of the Brave Browser that is active.

Move all open applications to current desktop "space" with applescript

I have been searching endlessly with no avail on how to move a window between spaces on macos in applescript. The seemingly basic thing I want to do is move all open application windows from any space to the first space. If anyone out there can help, please reach out. There seems to be no straight forward way in doing this in the latest version of macos (Mojave 10.14.4). I am also open to doing this in any another language that will interact with macos if it is possible/simpler.
tell application "System Events"
repeat with proc in application processes where background only is false
tell proc
log "found app: " & name
set processName to name
log count of windows
repeat with win in windows
-- move window to first "space"
end repeat
end tell
end repeat
end tell
I think the only way to do this is just simulating moving spaces using
tell application "System Events" to key code 19 using {control down}
but then your desktop will bounce for each app like crazy.
There's a window manager for macOS called Phoenix that's scriptable using JavaScript.
I wrote a Phoenix script, phoenix-move-windows that moves windows based on the current monitor and screen layout. It is configurable so that it will move windows to a specified position, and you can have different arrangements based on the current number of screens, and number of spaces on each screen.
It does support moving all windows to a specified screen and space, using the "defaultBinding" in an arrangement.

OSX Photos video play/pause Applescript shortcut

There is currently no keyboard shortcut to play videos in the new Mac Photos application.https://discussions.apple.com/thread/6996731?start=0&tstart=0
Rather than be frustrated in manually clicking the "play" button hundreds of times, I would like to run an Applescript to play/pause with a keyboard shortcut. I tried with no success:
tell application "photos"
"play" video
end tell
Any help advice on this would be appreciated.
Thanks
Just to update Paul's response, in Photos 2.0 (3150.4.120), you need to hit TAB twice, then spacebar will start playback. Similarly you need to hit TAB twice once you are done playing it, and then (and only then) spacebar will take you back to the grid view of your thumbnails.
If you do not mind a slightly time-consuming workaround, you can enslave Automator to create a Service that will subsequently respond to a keyboard shortcut. You can find the instructions here.
Since your goal is to use the keyboard, there's a better option for you. After selecting a video with cursor keys or mouse, press space to maximize the video, and then press TAB. Then space will start the video playing, left and right arrows will move frame-by-frame. Hitting TAB again will allow the space bar to return the video to its normal size.
Apparently this changes the focus to the pane with the movie so that space can start it, or perhaps it changes focus to the movie controls? IMHO, whatever it does, it's not very intuitive and badly needs to documented and preferably fixed.
More info at:
https://discussions.apple.com/thread/6996731?start=0&
2020 / Photos 6.0:
You can now toggle playback with Option + Space
or do it manually via "Image" -> "Start Playback".
Autoplay Script
repeat while application "Photos" is running
delay 1
try
tell application "System Events"
tell process "Photos"
click menu item "Start Playback" of menu "Image" of menu bar 1
end tell
end tell
end try
end repeat
Save it as autoplay.scpt and run it with osascript autoplay.scpt.
The script exits when you close the Photos application.

Open a moviefile with a keypress in Applescript

I am currently working on a school project in which I need to open a video file while using a key press in applescript. I want the program to open the file fullscreen in quicktime player when I press "y" while the program is running.
This is my current code, it works but I don't see how I can include keypresses.
tell application "QuickTime Player"
set testmovie to "Users:Nan:movies:atime.mp4"
activate
open alias testmovie
play document 1
set presenting of document 1 to true
end tell
I hope you guys can help!
You can create a Service in Automator and paste your code within a run applescript action. You can then assign a keystroke to the service in keyboard preferences.

Resources