Re: Keystroking a Text Field
Re: Keystroking a Text Field
- Subject: Re: Keystroking a Text Field
- From: Bill Cheeseman <email@hidden>
- Date: Wed, 01 Jul 2015 11:38:41 -0400
on run tell application "TextEdit" activate set myWindow to the front window set myContent to "Hello Cruel World!" tell application "System Events" to keystroke myContent end tell end run
When I run the above from the Script Editor with a Blank window open in Text Edit, the “Hello Cruel World!” string gets written to the Script Window, not the TextEdit Window, how to I get it to into the TextEdit Window?
Dave, there are a couple of problems with your script. Before I explain them, here's a version of your script that works:
activate application "TextEdit" delay 0.1 tell application "System Events" to keystroke "Hello Cruel World!"
The key to the solution is the realization that AppleScript is too fast for TextEdit. You have to give TextEdit a little time to get itself activated. 0.1 seconds is enough on my very fast MacBook Pro late-2013, but you might need a little more time on a slower machine.
Other issues include the fact that you really shouldn't put the 'tell application "System Events"' statement inside a tell block addressed to 'application "textEdit"'.
Also, in case this is what you were thinking when you did that, the System Events 'keystroke' command does not need an inner tell block -- or any tell block -- that is addressed to 'application "TextEdit"'. You might almost say that the 'keystroke' command is not really part of GUI Scripting. The 'keystroke' command always gets sent to the current frontmost application. That's why your script inserted the phrase into Scfript Editor -- Script Editor was still the frontmost application when the 'keystroke' command was executed. You just have to give TextEdit time to become the frontmost application. |
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden