Hi mycheal. One option is to create a small utility to detect a modifier key press for use in an AppleScript. Be aware when pasting code to maintain straight quotes. Copy and paste the following Swift code into a plain text editor and save it to your Desktop with a .swift file extension using a name such as modkeys.swift:
import Foundation
import AppKit
func TranslateModifierFlags(modifierFlags: NSEventModifierFlags) -> String {
let rawModifierFlags = modifierFlags.rawValue
var pressedButtons = [String]()
if ((rawModifierFlags & NSEventModifierFlags.ControlKeyMask.rawValue) != 0) { pressedButtons.append("Control") }
if ((rawModifierFlags & NSEventModifierFlags.AlternateKeyMask.rawValue) != 0) { pressedButtons.append("Option") }
if ((rawModifierFlags & NSEventModifierFlags.ShiftKeyMask.rawValue) != 0) { pressedButtons.append("Shift") }
if ((rawModifierFlags & NSEventModifierFlags.CommandKeyMask.rawValue) != 0) { pressedButtons.append("Command") }
if (pressedButtons.count > 0) {
return pressedButtons.joinWithSeparator(" ")
}
return "none"
}
var modifierFlags = NSEvent.modifierFlags()
print(TranslateModifierFlags(modifierFlags))
With Xcode installed, compile the above code with the Swift compiler in Terminal. The following command compiles the source file and outputs the binary to /usr/local/bin:
xcrun -sdk macosx swiftc ~/Desktop/modkeys.swift -o /usr/local/bin/modkeys
You can test the utility in Terminal by preceding the command with a delay and depressing any of the four modifier keys, Control, Option, Shift, and Command, either individually or in combination:
sleep 1; modkeys
Output returned is one or more of “Control”, “Option”, “Shift”, and “Command”. An example of the utility usage in your Applescript:
set dFolder to "~/Desktop/screencapture/"
do shell script ("mkdir -p " & dFolder)
set i to 0
set x to do shell script "date +%Y%m%d-%H%M%S" as string
repeat 10 times
set key_down to (do shell script "/usr/local/bin/modkeys")
if key_down = "Shift" then
beep
display notification "User cancelled"
return
else
set a to do shell script ("screencapture -x -t gif " & dFolder & i & x & ".gif") as string
try
tell application "Image Events"
set this_image to open a
scale this_image to size 10
save this_image
end tell
end try
delay 3
set i to i + 1
end if
end repeat
The beep and display notification in the script provide aural and visual feedback so you know when to release the held Shift key.