mycheal

Q: how to end a loop using keystroke/keypress in 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 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 repeat


Sample applescript with a loop in screencapturing.

Problem: How to end the loop using keystroke or keycode?

Mac mini, OS X El Capitan (10.11.5)

Posted on Jun 23, 2016 7:50 AM

Close

Q: how to end a loop using keystroke/keypress in applescript?

  • All replies
  • Helpful answers

  • by Camelot,Helpful

    Camelot Camelot Jun 24, 2016 3:52 AM in response to mycheal
    Level 8 (47,285 points)
    Mac OS X
    Jun 24, 2016 3:52 AM in response to mycheal

    Problem: How to end the loop using keystroke or keycode?


     

    Short answer: Other than the standard Command-period, you can't.

     

    Standard AppleScript doesn't have the ability to detect arbitrary keyboard events midstream. You can do something tacky like post a dialog and capture the response (which will slow down your script, but maybe you could replace the delay 3 with a display dialog... with timeout 3).

     

    Anything more involved means going outside of AppleScript, maybe to ASObjC

  • by Roote,

    Roote Roote Jun 24, 2016 5:27 PM in response to mycheal
    Level 2 (417 points)
    Jun 24, 2016 5:27 PM in response to mycheal

    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.