A Shortcut To Enclose Text In Quotes Using Automator

You can create a very short Automator action to take text as input, manipulate it, and replace the original text. In this example, we'll create a Javascript JXA action that encloses the text in quotes. You can use this in conjunction with a keyboard shortcut to quickly apply quotes in Pages and other apps.

Video Transcript
So I fielded an interesting question a few weeks ago and it turned into an Automator script, a really simple Automator script, and I thought it was very useful. The idea is to enclose selected text into some sort of characters like, for instance, quotes. So I could select, say, this sentence here and say I wanted to enclose it in quotes. Well I could go and add, of course, a quote here and a quote there and do it that way. But what if this is a task that you do pretty often. I would love to be able to select text and then hit a shortcut and quotes go around it. So how would you do this?

Well, you can do it in Automator. So let's launch Automator and let's create a new script. So I'm going to go New and I'm going to do a Quick Action. This will allow me to add something to the Services Menu of Applications. So I'll Choose that and I get some options up here. I could say I want it to receive something Automatic or in this case I'm going to say I want it to receive Rich Text because this is really only going to be something that I only use in text. I'm going to use it in Pages for instance. In Any Application so that way in Pages but also in other apps that I use for text editing. Then I'll just stick with the default Image and Color.

The only thing I need to put in here is a JavaScript piece of code. Now JavaScript, when I add the Run JavaScript automator action, it gives me a function which accepts input. The input is going to be what comes in from the action. So in this case it's going to be the text. All I need to do is manipulate that text. This is so easy to do in JavaScript. So I'm going to do input equals and then I'm going to put quotes. Then I'm going to create the quotes. I'm going to do Option and then the left bracket for the curly quote on the one side. Put that in quote. Plus the original input plus and in quotes Shift and Option and the left bracket to get the other side of the curly quotes.

So that changes the input to be the input plus a quote on either side. Then it's going to return it. Now there's a checkbox up here that says Output replaces selected text. That exactly what we want. Great. So I'll select that and it looks like we're ready to go. Do Command S for save and I'll save it as Enclose in Quotes. There we go. So I can now quit Automator and go back to Pages.

Now in Pages, if I go to Pages, Services this is where I will find it. But before I actually even use it I'm going to go to Services, Preferences. This will take me to System Preferences. Here I can close most of the options here and look for Text. Sure enough there is Enclose in Quotes. This is where I get to add a keyboard shortcut. So I'm going to Command and then the just the single quote, which is also the double quote there, so that's a really easy one to do as a shortcut. I don't think it's being used for much else. That should take care of it.

So now when I look under Services and look for Enclose in Quotes I'll see that that's the shortcut. I could just use the menu item here but instead I'll use the keyboard shortcut. I'll do that and you can see it encloses that in quotes. So I can select anything I want and enclose it in quotes.

You can use this for a variety of different things. So, for instance, I could instead of using quotes I could have use parentheses. Or something that's a little harder to type. You know there's all sorts of odd characters that you can do. There's square brackets. There's curly brackets. There are, you know, brackets or quotes for other languages. So you can use those. There's all sorts of different things you may want to enclose text in for different reasons. You can create scripts for these.

Comments: 18 Responses to “A Shortcut To Enclose Text In Quotes Using Automator”

    Douglas Brace
    3 weeks ago

    For MacMost episodes like this, would it be possible for you to either (a) provide a downloadable ZIP file or (b) provide a text file of the text that you entered into Automator? Thank you for considering this.
    .

    3 weeks ago

    Douglas: I usually do (include the code). But since this is just one line, I didn’t do it this time. The line is just:

    input = "“" + input + "’";

    Be careful when copying and pasting as the outer quotes need to be straight quotes, and the characters in the middle need to be the curly ones.

    Nick Greaves
    2 weeks ago

    Thanks very much for the video – educational, elegant and informative. I particularly enjoy your Automator tutorials.

    Jim
    2 weeks ago

    I created this script and would like to revise it. How do I find it? I thought it would be in the Automator library. Also, will this work in Word or Mail? I’ve tried with no success. So far, I can only get it to work in Pages. Thanks always for your great tips!

    2 weeks ago

    Jim: Launch Automator. File, Open Recent. Then, while you have it open, Command+click the filename in the title bar to see the path for next time.

    Tim A,
    2 weeks ago

    Was a wonderful taste of what can be accomplished with Automator.
    Here is a future challenge: capture text that has a date and time and convert it to a Calendar Event.

    Thanks again for these stellar videos.

    2 weeks ago

    Tim: That would be tricky since there are so many ways one can format a date. But you can often already do this with Data Detectors. I should be a new video on those soon.

    Don
    2 weeks ago

    Thanks for the useful tip.

    I understand a Javascript function key=key.replace(/ /g,”_”); searches for spaces in a selection and will replace them with underscores.

    But I don’t know Javascript. How do you implement this in your Automator example?

    2 weeks ago

    Don: Pretty much just as you put it. The variable is input, not key, so just change that.
    input = input.replace(/ /g,"_");
    (make sure you use straight quotes, not curly ones).

    jasper robinson
    2 weeks ago

    Thanks – I’ve adapted this trick to help create local file links.

    Step1: Service receives FILES OR FOLDERS in FINDER.APP.

    Step2: Gary’s Run JavaScript but with “input = “file://” + input;”. Now I can select a file/folder, run the Service, which creates text of the form like “file:///Users/WXYZ/Documents/chart.pdf” which can be used as clickable link to the file. You can embed them in Note/Evernote etc to help organise project that have files/folders in different places.

    Bob
    2 weeks ago

    Wen I use ⌘’ in Pages, TextEdit and Notes apps it works fine. When I try it in Mail in either a new message or a reply message it changes the text color to blue but doesn’t add any quote marks.

    2 weeks ago

    Bob: That keyboard shortcut is already in use in Mail, so pick another one. I was just using it as an example in the tutorial.

    Bob
    2 weeks ago

    Doesn’t seem to matter. I disabled all shortcuts except for that one and I also changed the shortcut for Enclose in Quotes to multiple different key combinations not already used and none of them work when pressing the set keyboard combination. But, any key combination I set for Enclose in Quotes works when I click on Mail, Services, Enclose in Quotes.

    2 weeks ago

    Bob: I tried Shift+Command+’ (quote) and it worked in Mail for me. Of course, I have to be composing a message for it to work, as you can’t add text to a message you are reading, as opposed to composing.

    Don
    1 week ago

    Gary: Thanks again for the help, but another dummie question after trying your suggestion…

    function run(input, parameters) {
    input = input.replace(/ /g,”_”);
    return input;
    }

    This returned a “Error on line 3: TypeError: input.replace is not a function. (In ‘input.replace(/ /g,”_”)’, ‘input.replace’ is undefined)”.

    What’s needed?

    1 week ago

    Maybe JavaScript isn’t happy that the input variable may or may not be a string. Try this to force it to be a string:

    function run(input, parameters) {
    str = ""+input;
    str = str.replace(/ /g,"_");
    return str;
    }

    Don
    1 week ago

    Gary: Outstanding tip! Works like a charm.

    I now have a useful group of Services to manipulate filenames by simply substituting one line in your suggested script.

    Replace Spaces with Hyphens: str = str.replace(/ /g,”-“);
    Replace Spaces with Underscores: str = str.replace(/ /g,”_”);
    Replace Hyphens with Spaces: str = str.replace(/-/g,” “);
    Replace Underscores with Spaces: str = str.replace(/_/g,” “);

    Plus this global search and replace technique could be expanded for other applications.

    Tim A
    6 days ago

    Don – ” Services to manipulate filenames…” I find this only seems to work on text, say within in TextEdit but not on Finder names. Am I missing something?? Changing filenames is exactly what I would like to be able to do.

Leave a New Comment Related to "A Shortcut To Enclose Text In Quotes Using Automator"

:
:
:
0/500 (500 character limit -- please state your comment succinctly and do not try to get around this limit by posting two comments)