Skip to content

Instantly share code, notes, and snippets.

@fuji246
Created March 19, 2020 18:39
Show Gist options
  • Save fuji246/da1a1549fb357a8468ac3f769e3f223e to your computer and use it in GitHub Desktop.
Save fuji246/da1a1549fb357a8468ac3f769e3f223e to your computer and use it in GitHub Desktop.
screen capture script using QuickTime Player
#!/usr/bin/osascript
# helper function for command parameters
on stringSplit(str, delimiters)
# save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
# set delimiters to delimiter to be used
set AppleScript's text item delimiters to delimiters
# create the array
set splitedArray to every text item of str
# restore the old setting
set AppleScript's text item delimiters to oldDelimiters
# return the result
return splitedArray
end stringSplit
on lastOffset(the_text, char)
try
set i to 1
set last_occurrence to 0
repeat count of the_text times
if item i of the_text as string = char then
set last_occurrence to i
end if
set i to i + 1
end repeat
on error
return 0
end try
return last_occurrence
end lastOffset
on basedir(the_path)
set last_occurrence to lastOffset(the_path, "/")
if last_occurrence is equal to 0 then
return "."
end if
if last_occurrence is equal to 1 then
return "/"
end if
return items 1 thru (last_occurrence) of the_path as string
end basedir
on basename(the_path)
set last_occurrence to lastOffset(the_path, "/")
if last_occurrence is equal to 0 then
return the_path
end if
if last_occurrence is equal to 1 then
return "/"
end if
if last_occurrence is equal to (count of the_path) then
set the_path to items 1 thru (last_occurrence - 1) of the_path as string
return basename(the_path)
end if
return items (last_occurrence + 1) thru -1 of the_path as string
end basename
# Open Sound Pannel
on openSoundPannel()
tell application "System Preferences"
activate
set the current pane to pane id "com.apple.preference.sound"
delay 1
end tell
end openSoundPannel
# Close Sound Pannel
on closeSoundPannel()
tell application "System Preferences" to quit
delay 1
end closeSoundPannel
# select Audio output device, return the name selected
on selectAudioOutput(audiodevice)
if audiodevice is equal to ""
log "skip selectAudioOutput"
return ""
end if
openSoundPannel()
set device to ""
tell application "System Events"
activate
tell process "System Preferences"
tell tab group 1 of window "Sound"
click radio button "Output"
if audiodevice is equal to "default"
set selected of row 1 of table 1 of scroll area 1 to true
set device to value of text field 1 of row 1 of table 1 of scroll area 1
else
set AudioOutputs to table 1 of scroll area 1
repeat with Counter from 1 to (number of rows of AudioOutputs as integer)
if exists text field 1 of row Counter of AudioOutputs then
set currentdevice to value of text field 1 of row Counter of AudioOutputs as string
if currentdevice is equal to audiodevice then
set selected of row Counter of AudioOutputs to true
set device to currentdevice
exit repeat
end if
end if
end repeat
end if
end tell
delay 1
end tell
end tell
closeSoundPannel()
log "selectAudioOutput: " & device
if device is equal to ""
display dialog "can't set audio device for recording, not found: " & audiodevice buttons {"OK"}
end if
return device
end selectAudioOutput
# start recording
on startRecording(device)
tell application "QuickTime Player"
activate
set doc to new screen recording
repeat with audiodevice in audio recording devices
if name of audiodevice is equal to device or device is equal to "default" then
set current microphone of doc to audiodevice
exit repeat
end if
end repeat
do shell script "/usr/sbin/system_profiler SPDisplaysDataType | grep 'Resolution'"
tell the result to set {DisplayWidth, DisplayHeight} to {word 2, word 4}
activate
tell application "System Events" to tell process "QuickTime Player"
# delay 5
key code 49
click at {DisplayWidth div 2, DisplayHeight div 2}
end tell
# recording begins here
start doc
end tell
end startRecording
on fileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists file theFile then
return true
else
return false
end if
end tell
end fileExists
# stop recording
on stopRecording(savepath)
tell application "QuickTime Player"
activate
delay 1
set doc to document 1
stop doc
# save recording
tell application "System Events"
tell process "QuickTime Player"
tell window 1
set savebasedir to my basedir(savepath)
set savebasename to my basename(savepath)
keystroke "s" using {command down}
delay 2
keystroke "G" using {command down} -- go to
delay 2
keystroke savebasedir
delay 1
key code 36 # enter
delay 2
keystroke "a" using {command down}
delay 2
keystroke savebasename
delay 2
key code 36
#wait for export, "my" is needed to call subroutine in tell block
log "export recording to " & savepath
repeat until my fileExists(savepath)
-- do nothing
end repeat
end tell
end tell
end tell
quit
end tell
end stopRecording
on todayISOformat()
set dateFormat to "%T"
set theDate to current date
set y to text -4 thru -1 of ("0000" & (year of theDate))
set mon to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
set H to text -2 thru -1 of ("00" & (hours of theDate))
set M to text -2 thru -1 of ("00" & (minutes of theDate))
set S to text -2 thru -1 of ("00" & (seconds of theDate))
return y & "_" & mon & "_" & d & "-" & H & "_" & M & "_" & S
end todayISOformat
on parseSettings(settings)
set savepathv to "/tmp/" & todayISOformat() & ".mov"
set audiodevicev to ""
repeat with s in settings
set params to stringSplit(s, "=")
if length of params is equal to 2 then
set k to item 1 of params
set v to item 2 of params
if k is equal to "savepath" then
set savepathv to v
else if k is equal to "audiodevice" then
set audiodevicev to v
end if
end if
end repeat
return {savepath:savepathv, audiodevice:audiodevicev}
end parseSettings
on run argv
set helpStr to "Usage: screencapture.applescript[(start|stop|select) params]
params like:
savepath=[path_to_save] - valid for stop, default to \"/tmp/[datetime].mov\"
audiodevice=[audio device name] - valid for start, select, device name like \"Soundflower (2ch)\" or \"default\" which means system default
"
set action to "none"
if length of argv is not less than 1 then
set params to parseSettings(argv)
log params
set action to item 1 of argv
set savepath to params's savepath
set audiodevice to params's audiodevice
end if
if action is equal to "start" then
startRecording(audiodevice)
else if action is equal to "stop" then
stopRecording(savepath)
else if action is equal to "select" then
selectAudioOutput(audiodevice)
else
return helpStr
end if
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment