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.

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

    Douglas Brace
    6 years 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.
    .

    6 years 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
    6 years ago

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

    Jim
    6 years 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!

    6 years 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,
    6 years 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.

    6 years 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
    6 years 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?

    6 years 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
    6 years 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
    6 years 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.

    6 years 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
    6 years 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.

    6 years 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
    6 years 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?

    6 years 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
    6 years 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 years 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.

Comments Closed.