Determining when QuickTime stops recording using AppleScript - applescript

I'm using the following AppleScript to determine when the user clicks the stop recording button in the task bar:
tell application "QuickTime Player"
tell document 1
activate
new screen recording
delay 1
tell application "System Events" to key code 49
delay 2
repeat until (new screen recording) is false
end repeat
end tell
end tell
Though instead the script keeps relaunching QuickTime.

You can put the result of the new screen recording command into a variable.
Use the exists command to check this document
tell application "QuickTime Player"
activate
set tdoc to new screen recording --> document "Screen Recording"
delay 1
tell application "System Events" to key code 49
delay 2
repeat while exists tdoc
delay 1
end repeat
-- the recording is stopped
tell front document
-- do something with the front document ("Untitled")
end tell
end tell

Related

How do I double click on an image/or link to initiate installation? APPLESCRIPT [Please view image]

I am trying to automate the installation of slack. I run the .dmg then must double click the "Slack" icon to start the install. using the '''click''' function on images is not really doable im guessing.. I see that the URL this image calls on is file:///Volumes/Slack.app/Slack.app/ Do I need to call on this link perhaps ? How can I get the same results of double clicking on the image.
Accessibility Inspector Screenshot
tell application "Finder"
set myFolder to container of (path to me) as alias
end tell
tell application "Finder"
open document file "Slack-4.3.3-macOS.dmg" of myFolder
end tell
tell application "System Events"
tell process "Finder"
click image "Slack" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack"
end tell
end tell
Accessibility Inspector Screenshot
If you are still interested in doing this job by clicking the image in AppleScript, you can try code below:
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
ignoring application responses
perform action "AXShowMenu"
end ignoring
end tell
end tell
end tell
delay 0.3
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
--key code 125 --also can try use arrow key to select item in context menu
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell
I test this code on my MacOS 10.12.6, it works OK. Even though it's easy to use terminal command to do this job, this method involves some very useful AS knowledge point.
1st : perform action "AXOpen" and "AXShowMenu"
2nd : show context menu and select menu item
3rd : deal with system delay
If it doesn't have delay issue, you can try the code below, which is more simpler,(also works on my machine):
tell application "System Events"
tell its application process "Finder"
tell image "Slack.app" of group 1 of list 1 of list 1 of scroll area 1 of splitter group 1 of splitter group 1 of window "Slack.app"
perform action "AXShowMenu"
delay 0.3
keystroke "Open"
delay 0.2
keystroke return
end tell
end tell
end tell

Getting osascript to work with iPhone and QuickTime Player

I run this script and applescript can detect the devices just fine
tell application "QuickTime Player"
video recording devices
end tell
It gives a the reply below
{screen compression preset "FaceTime HD Camera" of application "QuickTime Player", screen compression preset "Gangzheng's iPhone" of application "QuickTime Player"}
But when I run my actual script
tell application "QuickTime Player"
video recording devices
set newMovieRecording to new movie recording
tell newMovieRecording
set current camera of newMovieRecording to "Gangzheng's iPhone"
set current microphone of newMovieRecording to "Gangzheng's iPhone"
start
end tell
end tell
it says QuickTime Player got an error: Can’t make "Gangzheng's iPhone" into type video recording device.
Is there a way to make applescript record anything from iPhone?
I had accomplished what you want to do, but in a different way because I also tried with
set current camera of newMovieRecording to "Gangzheng's iPhone"
but had no luck.... So here is my script for setting a camera
tell application "System Events" to tell process "QuickTime Player"
#Set volume to 100%
click button 1 of window 1
#To open dialog to show available cameras
click button 3 of window 1
#To select our device
click menu item "Gangzheng's iPhone" of menu 1 of button 3 of window 1
end tell
My script was a bit longer because I wanted to record the screen of my iphone at my MAC... So I had to set QuickTime Player to full screen, move mouse pointer outside the screen and so on...
tell application "Finder"
set bigBounds to (get bounds of window of desktop)
end tell
tell application "QuickTime Player"
activate
start
#Start new recording cmd + option + N
tell application "System Events" to key code 45 using {option down, command down}
set iBounds to (get bounds of front window)
if bigBounds = iBounds then
delay 1
else
#Full screen only if is not already at full screen control + cmd + F
tell application "System Events" to key code 3 using {control down, command down}
delay 1
end if
tell application "System Events" to tell process "QuickTime Player"
#Set volume to 100%
click button 1 of window 1
#To open dialog to show available cameras
click button 3 of window 1
#To select our device
click menu item "iPhone de User" of menu 1 of button 3 of window 1
end tell
#To move mouse to the right side of the screen
set mouseToolsPath to "/Users/Shared/MouseTools"
set x to (item 3 of bigBounds) - 1
set y to (item 4 of bigBounds) / 2
#Por si el archivo no existe...
try
do shell script quoted form of POSIX path of mouseToolsPath & " -x " & (x as text) & " -y " & (y as text)
on error errMsg number errNum
display dialog "An unknown error occurred: " & errNum as text
end try
end tell
Hope it is helpful to someone.
barbudito, can you try this?
tell application "QuickTime Player"
get video recording device "Gangzheng's iPhone"
set newMovieRecording to new movie recording
tell newMovieRecording
start
delay 10
do shell script "env"
pause
save newMovieRecording in "/Users/pmlg673f/Movies/test.mov"
stop
close newMovieRecording
end tell
end tell
I had encountered this exact problem and made the following blog post which covers how I solved it https://kevinjalbert.com/applescript-that-mirrors-iphone-to-quicktime/. I had used some inspiration from this post and others as I looked around for a way to set the video/audio inputs via AppleScript.
The source for the AppleScript is here: https://gist.github.com/kevinjalbert/d7661a6a34c1d66dccf701f64eb09be4#file-mirror-scpt.

Apple script find and replace text

So, right now my code clicks on a textarea in safari then it adds a word using keystroke, however I was wondering if there is a way to find and replace a word in that area and also is there a way to replace a highlighted word without keystroke?
Current code:
Sorry its very very poorly formatted since I am pretty bad at applescript
to clickClassName(theClassName, elementnum)
tell application "Safari"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName
to clickID(theId)
tell application "Safari"
do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell
end clickID -- All the code up to this point did is allow me to click on IDs and classes in safari.
tell application "Safari" to activate
tell application "System Events"
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit"})
end tell
end tell
end tell
delay 2 -- All the code up to this point from the last comment did is open a new tab in safari.
repeat 5 times
set randomlist to {"1","2","3","4"} -- List of numbers that will be randomly selected and put in the texarea
set randomword to some item of randomlist
clickClassName("tool-button wikiEditor-toolbar-spritedButton", 0) -- This clicks the bold button on the sandbox page
tell application "System Events"
tell process "Safari"
keystroke randomword -- Uses keystroke to put replace the text inside the bold formatting area
end tell
end tell
delay 1
clickID("wpSave") -- Clicks save
delay 2
tell application "Safari" to set the URL of the front document to "https://en.wikipedia.org/w/index.php? title=User:USERNAMEHERE/sandbox&action=edit" -- Goes back to sandbox and repeats the process
delay 2
end repeat
If you want to test this then put in your wikipedia username in the link and login into your account. Pretty much its a wikipedia bot that will just put stuff in my wikipedia sandbox

AppleScript to Save Preview Document

I'm using a third party application to copy an image to the clipboard. I would then like to perform an AppleScript that opens Preview, creates a New Document which will then use the clipboard as the content, then save this to a specified location.
I'm 99% of the way there - just can't get the document to save. I'm on OS X 10.9.5.
Here's my script so far:
set the save_location to the quoted form of "/Users/freddy/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
activate
save front document as "PNG" in POSIX file save_location
end tell
I can't find the correct syntax for saving a Preview document. It will be the only document open at the time.
Try the following - note the comments:
# Do not use `quoted form of` - only needed and useful with `do shell script`
set the save_location to "/Users/jdoe/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
# Wait until the new window appears.
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
activate
# Save the document; `as "<format>"` doesn't seem to work, but the
# format is *implied* by the filename suffix (extension).
save front document in POSIX file save_location
end tell
This script works to save an image in the clipboard to disk, using Preview in OS 10.11.6 on a 2010 Mac Mini:
--CONDITIONS: Finder's clipboard has an image in it
tell application "Preview"
launch
activate
end tell
delay 2 --tweak the delays to what works
tell application "System Events" --not as elegant as "tell application...to tell process", but clearer, chronologically
keystroke "n" using command down --Preview creates a new window.
delay 1
keystroke "v" using command down --Finder's clipboard image is pasted into Preview window.
end tell
delay 1
tell application "System Events"
set the clipboard to "Joe Blow" --for name of forthcoming Preview file
delay 1
keystroke "w" using command down --Forces prompt of Preview to save image.
delay 1
keystroke "v" using command down --Pastes new filename into Preview's save window.
delay 1
key code 36 --"Return" key saves the new file.
end tell
--RESULT: A new image exists in your default folder, named "Joe Blow.png" (or whatever extension)

Closing QuickTime by Applescript

I am trying to play a movie fullscreen one time, then close the player programmably.
I have tried using QTMovieView, command line and AppleScript and found the Applescript is the most simple way.
BUT, as I really don't know Applescript, I can not make the QuickTime auto close after movie playing.
Everything works fine but the "done" was unrecognized in the repeat line.
Here is the script with this error:
error "QuickTime Player got an error: Can't make done of document 1 into type specifier." number -1700 from done of document 1 to specifier
tell application "QuickTime Player"
activate
open "/Users/...real path of the movie.mov"
present document 1
play document 1
repeat until (get done of document 1)
end repeat
delay 2
close document 1
end tell
Finally, I changed to this, is this ok?
tell application "QuickTime Player"
quit
end tell
tell application "QuickTime Player"
activate
open "/Users/.../...mov"
tell document 1
present
play
repeat until playing is false
end repeat
delay 2
close
end tell
quit
end tell
New problem: app hang before video finish.
This works for me, however it doesn't seem very robust. Is it guaranteed that the current time will always end up being equal to the duration, given that they're both reals? You may want to put some "within epsilon" logic into the repeat condition.
tell application "QuickTime Player"
play document 1
repeat until (current time of document 1 = duration of document 1)
end repeat
delay 2
close document 1
end tell
Try:
tell application "QuickTime Player"
tell document 1
present
play
repeat until playing is false
delay 1
end repeat
end tell
quit
end tell

Resources