37

I have a text phrase (actually, several phases) that I use frequently in multiple applications. I'd like to create a keyboard shortcut or hotkey that will paste this text phrase wherever I have placed my cursor in a text input field (Apple Mail, Microsoft Word, TextEdit, TextWrangler, browser windows, etc.) rather than constantly copying and pasting from a sticky note.

I'd prefer not to have to buy any apps (seems like Keyboard Maestro has this functionality but seems silly to pay $36 just for this), though free apps would be fine. Tried looking under System Preferences > Keyboard > Keyboard Shortcuts but no obvious solution, and all AppleScripts I found seem to not translate properly for me. I've found other solutions for this problem on this forum for Windows users (like AutoHotKey) but none for Mac users.

9 Answers 9

26

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.

11
  • Wow, thanks so much @slhck! I tried this, but am having problems in the last step. It's not showing up in the "Services" menu when I go to "Keyboard Shortcuts" in System Preferences. I know it runs because when I open Automator and hit Play, the text is typed in the previous application, but it won't show up in service so I can add a keyboard shortcut even though I have saved it in my Library > Services folder. On the advice of other forums I Googled, I double-checked that the service input was set to "Service receives: no input" (it was) and tried relaunching the Finder. Any thoughts?
    – Ruthie
    Jan 30, 2013 at 0:56
  • So you saved it from Automator? Normally it should appear in the Keyboard Shortcuts options, under Services and General at the bottom. Have you tried logging out and back in, or even restarting?
    – slhck
    Jan 30, 2013 at 8:06
  • @slhck this sounds like a great way to do this, but could you detail your answer a bit more? I'm not fluent with the way Automator works, when I launch it it asks me "chose a type for your document" and then I'm kind of lost. I'll go with Jonathan Huie's answer for now but it still implies typing something... Thanks! Oct 17, 2015 at 23:38
  • 2
    I find that this works in a text editor, but not in a text field on a website. Anyone else?
    – Eliot
    Oct 23, 2015 at 18:51
  • 1
    I get the following error when I try it: The action “Run AppleScript” encountered an error: “The operation couldn’t be completed. (com.apple.Automator error -212.)” Jun 13, 2022 at 17:30
12

In 2022, the simple way to do this without any scripting or third-party products is to use the Shortcuts app.

  1. In the Shortcuts app, click + to create a new shortcut.
  2. Double-click or drag the Text action from the Action Library panel to the editor.
  3. Enter your text phrase in the text field.
  4. Double-click or drag the Stop and Output action to the editor.
  5. In the Shortcut Details panel, tick the Use as Quick Action option.
  6. Click the Add Keyboard Shortcut button, and enter your desired key combination.
  7. Give your shortcut a name (at the top of the window) and close the window.

Your new shortcut should now be available in a number of ways. In a text document or text field:

  1. Right click and select it from the contextual menu.
  2. Select it from an app's Services menu.
  3. Press the key combination you set up. (The system may take a few minutes to register this, so be patient. If it's still not working, make sure the key combination isn't conflicting with one of the system's own shortcuts.)
1
  • 1
    The "system may take a few minutes to register this" advice was extra handy! Thank you. Apr 24 at 8:13
9

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.

6

Just click on: System Preferences, Keyboard, Text, and enter your shortcut and phrase

5
  • 1
    I like this answer, but it does't really allow "keyboard shortcut" or "hotkey", one still has to input a text (and not a combination of keys) that gets replaced by the machine. Oct 17, 2015 at 23:29
  • 2
    This only seems to work in a few select apps. It does not work in most of the places I'd like it, eg browser input fields, my mail client (Thunderbird), Terminal, etc. Sep 3, 2017 at 10:14
  • Does this work? I have tried typing the "replace" string in the browser, text editor, and terminal, which are the only three places I'm ever typing in: none of them worked!
    – PatrickT
    May 23, 2020 at 23:53
  • This is exactly what I was looking for. Mar 31, 2021 at 4:49
  • Oh that's awesome, it allows to type a shorthand and that replaces it with the text you want. That was a great and clean solution for me. Oct 28, 2023 at 23:14
3

Alfred app lets you do this—plus a great deal of other things—using the Workflow and Snippets features. I use Alfred all the time and highly recommend it. To unlock Workflows you have to pay for a Powerpack license, but you won't regret it. 😉

1
  • I agree, this is one of the most useful apps anyone owning a Mac should have. I fully migrated from custom Automator services to Alfred.
    – slhck
    Jul 12, 2019 at 18:39
2

Have a look for an App called "Textexpander" which gives you the abillity to define textphrases like @dd which - when typed - will be expandet to the defined content - in that case the actual date.

AFAIK it's Shareware. So it's not actually free but the demo mode is not timelimited.

1
  • Just a quick note in case it's useful for others -- TextExpander doesn't work on X11 terminal windows.
    – ggkmath
    Jul 8, 2013 at 15:21
1

I use "Text Replacement" function which is a native macOS feature. It lets you enter a keyword you define and then it converts into the pre-defined phrase. It is very easy and doesn't require any extra apps.

System Preferences > Keyboard > Text Replacements...

1
  • 1
    How is this a shortcut key ? Apr 24, 2023 at 13:59
0

A separate keyboard called the Martian Keyboard will hold text strings such as sentences and even short paragraphs. It will save you from typing annoying nuisance keystrokes you type over and over, day after day. The keys are work like "paste" keys. It has the ability to store all the shortcuts in Word on a single keyboard, using shift layer keys. It's mainly for professional use, so it is a bit expensive.

0

I achieved this with BetterTouchTool.

Its advantage over Automator and scripts is that it can paste Unicode contents.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 22, 2022 at 22:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .