Creating hotkey or keyboard shortcut for pasting specific text phrase in Mac OSX
The easiest way to type text with an AppleScript is to run:
tell application "System Events" to keystroke "foo bar"
You can trigger this with a keyboard shortcut by creating a new Quick Action (previously called Service) in Automator. Set it to receive No Input, and then drag Run AppleScript from the left pane to the right.
It should look like this:
Save it, and then assign a keyboard shortcut to this Service under System Preferences » Keyboard » Keyboard Shortcuts.
The keystroke
command can only be used to insert characters that are found on the current keyboard layout, and there is a visible delay for long strings of text. You can also insert text directly:
That method isn't really usable either. There is a bug on 10.7 and 10.8 where the shortcuts for Automator services don't always work until you hover over the services menu.
I have used FastScripts to assign this script to F3:
try
set old to the clipboard as record
end try
try
tell application "System Events"
key code 123 using {option down, shift down}
keystroke "c" using command down
end tell
delay 0.05
set input to the clipboard
read POSIX file "/Users/lauri/Notes/snippets.txt" as «class utf8» using delimiter linefeed
repeat with p in result
considering case
if p starts with (input & " ") then
set the clipboard to text ((offset of space in p) + 1) thru -1 of p
tell application "System Events" to keystroke "v" using command down
delay 0.05
exit repeat
end if
end considering
end repeat
end try
try
set the clipboard to old
end try
snippets.txt look like this:
m [email protected]
cmd ⌘
Another option is to use DefaultKeyBinding.dict. This would make ⌥M or ⌥X and m insert an email address:
{
"~m" = (insertText:, "[email protected]");
"~x" = {
"m" = ("insertText:", "[email protected]");
};
}
You can also add custom text to the popovers shown when holding keys. See this question.
Just click on: System Preferences, Keyboard, Text, and enter your shortcut and phrase