问题
I'm making an application, in which I need to run a code that will tell system events to keystroke a certain phrase. Like in an AppleScript, I would do:
Tell Application "System Events" to keystroke "This is a test"
I don't know how to do this from Xcode, and I would really appreciate any help. Thank you!
回答1:
You can run an AppleScript from code, e.g., using OSACompileExecute
. Also see QA1026.
Alternately, you can post keyboard events using the CGEvent APIs. With recent OSes, you may be able to avoid posting each letter individually using CGEventKeyboardSetUnicodeString
.
来源:https://stackoverflow.com/questions/3030070/tell-system-events-to-keystroke-in-xcode