Skip to content

Instantly share code, notes, and snippets.

@whacked
Created November 28, 2016 05:19
Show Gist options
  • Save whacked/c638acc1faf17ebb6de5523d48646c77 to your computer and use it in GitHub Desktop.
Save whacked/c638acc1faf17ebb6de5523d48646c77 to your computer and use it in GitHub Desktop.
-- ref https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
set recordingTime to 10
set dateString to replace_chars((date string of (current date) as string) & " " & (time string of (current date) as string), ":", ".")
set filePath to (path to desktop as string) & "my recording at " & dateString & ".mov"
display notification filePath
-- get window size
tell application "Finder"
set _b to bounds of window of desktop
set _width to item 3 of _b
set _height to item 4 of _b
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
delay 1
-- for whatever reason, CGDisplayMoveCursorToPoint will hang if called immediately after import Quartz.
-- through trial and error, querying mouseLocation() makes it not hang
do shell script "python -c 'import time,Quartz; time.sleep(1); Quartz.NSEvent.mouseLocation(); Quartz.CGDisplayMoveCursorToPoint(0, (" & (_width / 2 as string) & "," & (_height / 2 as string) & "));mouse_location = Quartz.NSEvent.mouseLocation(); [ time.sleep(0.05) or Quartz.CGEventPost(Quartz.kCGHIDEventTap, Quartz.CGEventCreateMouseEvent(None, mouse_action, mouse_location, Quartz.kCGMouseButtonLeft)) for mouse_action in [ Quartz.kCGEventLeftMouseDown, Quartz.kCGEventLeftMouseUp, Quartz.kCGEventLeftMouseDown, Quartz.kCGEventLeftMouseUp ] ]'"
start
delay recordingTime
stop
end tell
export document 1 in (file filePath) using settings preset "720p"
close document 1 saving no
-- this will not work without enabling Assistive Access. Commenting out since not vanilla-runnable
--tell application "System Events" to click menu item "Hide Export Progress" of menu "Window" of menu bar 1 of process "QuickTime Player"
set miniaturized of window 1 to true
end tell
-- tell application "System Events" to tell process "QuickTime Player" to set visible to false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment