Quicktime Player recording with Applescript broken on Mavericks? - macos

I have this simple code:
tell application "QuickTime Player"
activate
new screen recording
document "Screen Recording" start
delay 10
document "Screen Recording" stop
end tell
This records a 10-second movie on my 10.8 machine with no problem.
But on 10.9 mac-mini, QT hangs at the stop action above.
It hangs at the window with message "Finishing Recording".
I have to force quit it and still same thing. If I do these steps manually, they work. But with AppleScript or even same steps with Automator have same issue.
I upgraded to 10.9.2, but still same issue.
Is this a known bug? Any suggestions for a work around?
Thanks.

I do something like this to record the screen from CI and it always works for me:
set filePath to (path to desktop as string) & "ScreenRecordingFile.mov"
tell application "System Events" to tell process "Simulator"
set frontmost to true
end tell
tell application "QuickTime Player"
activate
set newScreenRecording to new screen recording
delay 1
tell application "System Events"
tell process "QuickTime Player"
set frontmost to true
key code 49
end tell
end tell
tell newScreenRecording
start
delay 15
stop
end tell
export document 1 in (file filePath) using settings preset "720p"
close document 1 saving no
end tell

I've ran into the same problem and found out, that the problem is not in the stop, but in the non-interactive start, so I've tried to start recording (pseudo)interactively and it worked out perfectly.
tell application "QuickTime Player"
activate
new screen recording
activate
tell application "System Events" to tell process "QuickTime Player"
key code 49
end tell
document "Screen Recording" start
delay 10
document "Screen Recording" stop
end tell
It still calls start instead of emulating click, because I haven't found a way to make it work.

Two tricks are needed:
Send a space key to the QuickTime Player before start to avoid the Finishing... dialog being stuck. (credits to #outring)
We need to simulate a mouse click. This is not easy, and we need to do it via CoreGraphics (aka Quartz). We can either write in Objective-C (need compilation), in Python (via PyObjc bridge) or Swift.
Somehow recording starts roughly 2 seconds after the mouse click, so we need to sleep 12 seconds for a 10-second video.
A working script will look like this:
tell application "QuickTime Player"
activate
new screen recording
tell application "System Events" to tell process "QuickTime Player" to key code 49
document "Screen Recording" start
do shell script "python -c 'import Quartz; mouse_location = Quartz.NSEvent.mouseLocation(); [ Quartz.CGEventPost(Quartz.kCGHIDEventTap, Quartz.CGEventCreateMouseEvent(None, mouse_action, mouse_location, Quartz.kCGMouseButtonLeft)) for mouse_action in [ Quartz.kCGEventLeftMouseDown, Quartz.kCGEventLeftMouseUp ] ]'"
delay 12
document "Screen Recording" stop
end tell

Related

AppleScript plays playlist in Music.app on macOS Monterey

I'm working on this Alfred Workflow: https://github.com/gustavosaez/Alfred-Workflow-Apple-Music-Playlist
And today I'm looking for a day to "automate" the play music in background or hidden (to avoid open the application and click PLAY).
I found a way to set the mouse click on a screen position (specific on the play button), but it works only if the app is visible and if the resolution of screen is the same of mine.
I search about UI Elements for Music.app and didn't find anything.
The problem is:
When Alfred opens the chosen link, Music.app take a few seconds to open and load the playlist selected;
After this, space/play command is inactive, so I think to create a script that gets the current page and play the playlist in shuffle mode.
Any idea?
some ideas:
tell application "Music"
-- tell application "System Events" to tell process "Music" to click at {620, 374}
play current playlist
end tell
|
tell application "Music"
-- tell application "System Events" to tell process "Music" to click at {620, 374}
play {workflowPlaylistName}
end tell
tell application "System Events"
keystroke "h" using command down
end tell
I'm confused why you are trying to use mouse clicks or keystrokes, instead of just using
tell application "Music"
play
end tell
or
tell application "Music"
play the playlist named "{query}"
end tell
and pass the name of the playlist as query

AppleScript works in Script Editor but not as application

I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.
I am running Yosemite, and am fully updated.
Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.
Thanks for any advice! Here is the code:
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
close every window
end tell
set volume output volume 75
tell application "iTunes"
set currentVolume to sound volume
if player state is playing then
stop
back track
end if
play track "The Promise"
set player position to 6
end tell
delay 4
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
end tell
I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.
Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.
NOTE: I can't get Chrome to open a file either.
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell
tell application "Safari" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.
You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”
You may need to use the “+” button to add your app to the list.
I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.

Delay in an Alfred 2, using Automator and Apple Script to open "Stickies" and create a new note

Basically my goal is to code a key command (option-s) to activate Stickies and create a new note. Right now I have an Alfred 2 generated Automation which links the hot key to the following script:
on alfred_script(q)
tell application "Stickies" to activate
delay .2
tell application "Stickies" to activate
delay .01
tell application "System Events"
keystroke "n" using command down
end tell
end alfred_script
The two activate commands are my attempt to deal with a bug where it opens the application, but doesn't bring it to front. It works seamlessly when the application is open in the background, but it's slow and creates a screen flash when the application isn't already running. The delay is not coming from the application itself because I can open the application and hit command-n as fast as possible, and it always works.
(By the way if you have an idea for how I could hide all other notes and just show the new one, that would be awesome!)
Try this:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
set frontmost to true
end tell
If you run the script by pressing option-s, there might not be enough time to release option before keystroke "n" using command down.
Or this doesn't raise the windows for other notes:
launch application "Stickies"
tell application "System Events" to tell process "Stickies"
click menu item "New Note" of menu "File" of menu bar 1
end tell
do shell script "open -a Stickies"
activate app "Appname" and set frontmost of "Appname" to true raise all windows, but do shell script "open -a Appname" raises only one window.
Hotkeys also have a short delay by default in Alfred, but you can reduce it by changing the trigger behavior:
You could try this alternate way, might have a different effect.
tell application "System Events"
tell process "Stickies"
set frontmost to true
keystroke "n" using command down
keystroke "Hello World" & linefeed & "I'm a new note!"
end tell
end tell
Hiding all other notes, i'd say start a new question for that.

Tell PDF Viewer/Adobe Reader to go to page on OS X

I am trying to make an integrated presentation for my students.
System: OS X Lion 10.7
For that I need to show a foreground window running a 3D presentation which is remotely controlled by a command line on another screen.
Besides it I would like to show a small set of PDF slides behind it.
I need some way of "remote controlling" the backgrounded PDF Viewer (Adobe Reader or PDFView) to accept simple commands like
Go to Page x (ideally)
Go to first page (home button)
Go to next page (arrow down, arrow right, page down or scroll down will all do)
I prefer to run the PDF in Adobe Reader fullscreen mode (allows me to background it and overlay another window on top, but PDFView could do too.
I have tried various Applescripts (run with osascript), but with no success:
tell application "System Events"
tell process "Adobe Reader"
tell window "starkdemo.pdf" to key code 125
end tell
end tell
sends key down to my terminal
tell application "/Applications/Adobe Reader.app"
tell active pane of active window
large scroll down 1
end tell
end tell
gives me an error 63:69: script error: Expected end of line but found class name. (-2741)
I have found that the free Viewer Skim has extensive AppleScript scripting http://sourceforge.net/apps/mediawiki/skim-app/index.php?title=AppleScript
It also have the fullscreen view that I like. So I will be using this in the future. Also the applescript command is just one line:
tell document "starkdemo.pdf" of application "Skim" to go to page 7
Try:
activate application "Adobe Reader"
tell application "System Events"
tell process "Adobe Reader"
click menu item "Full Screen Mode" of menu 1 of menu bar item "View" of menu bar 1
delay 2
key code 125
delay 2
keystroke space
delay 2
end tell
end tell
Or try this with Acrobat Pro
tell application "Adobe Acrobat Pro"
tell PDF Window 1
read page down
end tell
end tell
Or
tell application "Adobe Acrobat Pro"
tell PDF Window 1
goto page 3
end tell
end tell

quicktime mute all files droplet

Want an applescript droplet that opens files in QuickTime and mutes them all. The script only mutes the front most opened file.
on open the_Droppings
tell application "QuickTime Player 7" to activate
tell application "QuickTime Player 7" to open the_Droppings
tell application "System Events" to tell process "QuickTime Player 7"
keystroke (ASCII character 31) using {command down, option down}
end tell
end open
You need to tell each window open in Quicktime in turn to perform the action. An action has to have a specific target in Applescript; they way you have it written now, you are tell the Quicktime application, not a window in Quicktime.
Untested:
on open the_Droppings
tell application "QuickTime Player 7" to activate
tell application "QuickTime Player 7"
open the_Droppings
set documentCount to (count documents)
end tell
repeat with thisDocument from 1 to documentCount
tell application "System Events"
tell process "QuickTime Player 7"
tell document thisDocument
keystroke (ASCII character 31) using {command down, option down}
end tell
end tell
end tell
end repeat
end open
But I believe there is also a preference to not have movies auto-play upon opening as well.
Here is the way I got it to work.
on open the_Droppings
activate application "QuickTime Player 7"
repeat with oneDrop in the_Droppings
tell application "QuickTime Player 7"
open oneDrop
set sound volume of document 1 to 0
end tell
end repeat
end open

Resources