Hello. First post. First AppleScript. New Mac user.
I want to open an ‘existing’ TextEdit file and move the cursor to the end of the file. Did a search and found a script that should work. It opens to the front now. Well it does when I don’t add the keystroke stuff that I found in the example script.
Here’s what I have so far:
tell application "System Events"
end tell
tell application "TextEdit"
launch
activate
open file "Macintosh HD:Users:username:Desktop:myFile.txt"
tell process "TextEdit"
keystroke (ASCII character 31) using command down
end tell
end tell
That won’t even compile. “Expected Expression but found command name.” I appreciate any help given.
I bought a book last night called “AppleScript: The Definitive Guide”. Is that a good book? Other recommendation? How about that Missing Manual one? At the store I went to it was, missing.
Hi;
I realize it’s a bit confusing, but AppleScript | Mac OS refers to OS 9 questions, so I’ve moved this to AppleScript | OS X where X-users will see it.
This will do it:
tell application "TextEdit"
open file ((path to desktop as text) & "myFile.txt")
activate
end tell
tell application "System Events" to tell process "TextEdit" to keystroke (ASCII character 31) using command down
You can input text this way too, even into an rtf file:
set NewLine to "Here is a new line. Cursor waiting for another..." & return
tell application "TextEdit"
open file ((path to desktop as text) & "myFile.rtf")
activate
end tell
tell application "System Events" to tell process "TextEdit"
keystroke (ASCII character 31) using command down
keystroke return & NewLine
end tell
That worked like a charm! Thank you very much.
Also, sorry about posting in the wrong forum. I tried really hard not to make those kinds of mistakes since it was my first post and all. I’ll be even more careful next time.
On a related question, I want to place the cursor @ the end of the line, not create a newline,
tell application “System Events” to tell process “TextEdit” to keystroke (ASCII character 31) using command down
gives me a newline.
Is there a keyword for the keystroke ‘end’?
The cursor stays at the end anyway…
set myNums to {"one", "two", "three", "four"}
repeat with k from 1 to 4
set MoreText to "Here is text string " & item k of myNums & ". Cursor waiting for another... "
tell application "TextEdit"
open file ((path to desktop as text) & "myFile.rtf")
activate
end tell
tell application "System Events" to tell process "TextEdit"
keystroke MoreText
end tell
end repeat