Getting AppleScript to insert a text block
August 1st, 2008Since trying out MacSpeak Dictate, I found a useful feature called “text macros” which allows you to link a block of text to a voice command. Say the command, and the text is magically inserted. One problem: MacSpeak applies it’s own rules to the formatting of the text, so words may not be capitalized the way you originally entered them. Solution: use an AppleScript instead, which can also be linked to a voice command.
I’ve been searching high and low for how to get AppleScript to type a block of text on command. There’s the “keystroke” command but this is extremely slow for entering a lot of text. The solution I came up with involves saving the current contents of the clip board, inserting the text you want to insert on the clipboard, and getting AppleScript to paste it. Finally AppleScript restores the original contents of the clipboard. I had to use a delay before restoring the clipboard because sometimes the restore goes through before the text is pasted.
tell application "System Events"
set currentClipboard to the clipboard
set the clipboard to "Ishir Bhan, M.D., M.P.H."
keystroke "v" using {command down}
delay 1
set the clipboard to currentClipboard
end tell
To include line breaks in your text, just use \n.
Insert TExt Block applescript:
I’m very interested in this script. If I have any app forward and the cursor is where I want Ishir inserted. What do I do from here? How do I work it?