Reputation: 53
I am still learning the basics of AppleScript. When I try to run a simple "Hello World" script like:
tell application "TextEdit"
activate
end tell
tell application "System Events"
keystroke "Hello World!"
key code 36
end tell
The first time I run it, it writes "Hello World!" in TextEdit as it should. But the second time, it writes "Hello World!" in Script Editor. From then on, it will only write in Script Editor.
Am I missing something obvious in the script? Or is there something about OS X that I should be looking at?
Thanks in advance! Feel free to respond with long winded answers, Kbase articles, or other discussion threads that I may have missed.
Upvotes: 0
Views: 182
Reputation: 903
Try this group of code. I'll explain how it works below, using comments (--).
tell application "TextEdit"
-- Activate TextEdit and bring it to the foreground
activate
-- Create a new document and assign it to a variable called 'myDocument'
set myDocument to make new document
-- Activate the above mentioned new document
tell myDocument
-- Add text to the above mentioned new document
set its text to its text & "Hello, world!"
end tell
end tell
Upvotes: 1
Reputation: 383
tell application "TextEdit" to activate
tell application "System Events"
tell application process "TextEdit" to set frontmost to true
keystroke "Hello World!" & return
end tell
Upvotes: 1