Two-part AppleScript key sequence

applescript

I am trying to assign a keyboard shortcut to the screenshot app window feature (if you didn't know, it is ⌘⇧+4, then Space, which allows you to take a screenshot of a single app window).

I have been trying to code it as an AppleScript for almost an hour, but there is something I am missing; I am only getting the standard screenshot selection feature.

The code I have so far is:

tell application "System Events"
    key code 21 using {shift down, command down}
    key code 49
end tell

Does anyone know how to code the key sequencing that I am trying to do above? Or have a resource that describes how to do it?

Best Answer

Found it! Was missing a delay.

tell application "System Events"
    key code 21 using {shift down, command down}
    delay 0.1
    key code 49
end tell

Then assign to a keyboard shortcut using Alfred, Quicksilver or other similar application.