Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to end an AppleScript using a keystroke?- please

I currently have the below code, I am trying to make it that if the keystroke "p" is clicked the apple script or repeat ends (this is made to repetitively click the space bar very fast for a long time) and I cant seem to get it to stop spamming without having to restart my computer (hence why I want it to be p that stops the command)


Thank you!




tell application "System Events"


display dialog "Enter message" default answer "example text"


set textToSay to the text returned of the result


display dialog "Interval " default answer ".1"


set int to the text returned of the result


set stopspam to the keystroke "p"


display dialog "Click to launch in 5 Seconds"


delay 5


repeat until stopspam


delay int


keystroke textToSay



end repeat


end tell

Posted on Mar 9, 2022 10:17 PM

Reply
Question marked as Best reply

Posted on Mar 10, 2022 9:10 AM

You are misunderstanding the use of the keystroke command.


Keystroke does not indicate that a given key is pressed. There is actually nothing in AppleScript itself that can do this.


Instead, keystroke simulates the action of pressing the specified key on the keyboard - in other words, keystroke "p" effectively presses the 'p' key and sends that to the front app.


From reading between the lines as to what you're trying to do, you're out of luck. There is no way to capture keystrokes within the 'display dialog' command without resorting to AppleScriptObjC which adds additional UI controls.

Similar questions

1 reply
Question marked as Best reply

Mar 10, 2022 9:10 AM in response to Morsormedarers

You are misunderstanding the use of the keystroke command.


Keystroke does not indicate that a given key is pressed. There is actually nothing in AppleScript itself that can do this.


Instead, keystroke simulates the action of pressing the specified key on the keyboard - in other words, keystroke "p" effectively presses the 'p' key and sends that to the front app.


From reading between the lines as to what you're trying to do, you're out of luck. There is no way to capture keystrokes within the 'display dialog' command without resorting to AppleScriptObjC which adds additional UI controls.

How to end an AppleScript using a keystroke?- please

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.