automator – AppleScript (to start screen recording) takes 9 to 15s to even start running. Why?


I use the following AppleScript (assigned to keystroke Cmd+Ctrl+5) to start screen recordings (adapted from here). If I create an Automator Quick Action and assign the keystroke to it, it takes 9s until the screen recording starts. If I call the script via osascript ./screenrecord_starter.scpt from the command line, it takes about 15s. Why does it take so long (and how can this be improved (either by how the script is called or by the script itself))?

#!/usr/bin/osascript

--  Setup screen recording (mimics 'Cmd-S-5')
tell application "System Events" to keystroke "%" using {shift down, command down}

--  Start the screen recording
tell application "System Events" to tell process "Screen Shot"
    repeat until exists its front window
        delay 0.1
    end repeat
    if not (exists button "Record" of its front window) then
        click checkbox "Record Entire Screen" of its front window
    end if
    click button "Record" of its front window
end tell