While you can use text replacements to insert reusable blocks of text, it is sometimes necessary to customize the text for various situations. You can build an Automator service to trigger a series of prompts and then create a block of text based on your responses. In this example, we'll build a way to quickly respond to support email messages.
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos).
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos).
Video Transcript
Hi, this is Gary with MacMost.com. Today let me show you how to build an automated Quick Action that allows you to create a text response based on a set to inputs.
MacMost is brought to you thanks to a great group of more than 600 supporters. Go to MacMost.com/patreon. There you could read more about the Patreon Campaign. Join us and exclusive content and course discounts.
So you probably already know how to create text replacement where you type a few characters and then it replaces it with a whole paragraph of text. This is handy for responding to somebody with a standard message, email, or text. But what if you need to actually have some different information every time you respond. Like you want to respond including that person's name or other bits of data. You could use Automator to build a system where it prompts you for things, like the person's name, and then gives you text in return that includes that bit of information inside the text.
So let's start off here in Automator and I'm going to create a QuickAction. That way we'll be able to access it in different apps and later assign a keyboard shortcut to it. Now the first thing we want to do is setup how it works. So what we want is Automator to not have any input. So we should set it to No Input, be we're not going to do the right away. We're going to leave it at automatic for now. Then we're going to check the box saying Output Replaces Selected Text. Later on we'll change this to No Input. If we do it now it's going to uncheck this box. So the first thing we want to have happen is it will ask us for a piece of text. Automator has a handy action for that. We'll search for Ask and there's Ask For Text and we'll say OK. It will ask us for a name and it will require an answer and if I run this you could see it asks for a name and we can type it in and that's it. Now we want to store that in a variable because we're not done yet. We're going to ask for more pieces of information. So let's set variable, so Set Value of Variable, let's create a new variable name and we'll call this Name, since it's the person's name. Great. So that will ask for the text and we'll set this variable to it.
Now let's ask for some more text. What we're going to do with this is have kind of an email response. We'll address the person by name and tell them we'll get back to them in after a certain number of days. What number of days? Well, let's make that something that we can change. So sometimes it's 3 days and sometimes it's 7 days, whatever you want. So we'll Ask for Text but this time we're going to ask for Days? and we'll require an answer here. Now we don't want the result of the last action passed into this. We want to break that. So I want to Control click here and Ignore Input.
So these are separate things. I'll ask for the Name, set it in a variable, and then start fresh Ask for the Number of Days and then set this as a variable and we'll do New Variable name and we'll call that Days. Notice here I can see the variables at the bottom. If you don't you probably have this set to Log. Click here to set it to Variables and now you can see those variables.
Now we'd like the next section to actually prompt us with some choices. So not something we type in but choices. Okay if you don't hear from us then call us or email us or try again or something else. We want to have a choice. We're going to use a JAVA Script. We're going to run a piece of JAVA script. Again I'm going to break the input here by Control clicking on that saying Ignore Input. Now we have JAVA script. JAVA script is both going to setup a list of things that can be the result of this prompt and do the prompt.
So here's the script. We start off with the default function declaration at the top. We don't need to change that. Then these two lines which are pretty standard at the top of any JAVA script. First we ask for the current app. You know what app is running. It's probably going to be Mail for doing an email response thing here. Then we do Includes StandardAdditions which will allow us to actually include some of the commands we need. Now we get to work.
First we have a variable declaration here. We declare choices to be an array. An array has got square brackets around it and item separated by commas. So we have three items here, they are strings in quotes, pieces of text in other words, continue to wait, try contacting us again, and try calling. You can have four items here. Five, two, how many you want. Just change this array.
Now we're going to have another variable, selection. That's going to be the result of a chooseFromList call which will throw up a prompt and allow us to choose from a list. The list will be from the array choices. Then we include the parameters withPrompt and the prompt it Otherwise? and then multipleSelectionsAllowed: false. Only allow one selection.
Then we'll return the result. Let's set a variable to this and we'll call this variable Otherwise. Now we can look at the results here to test this out. So I can click on results and we get this area here where we're going to see the results. So what we want to test it does this JAVA script actually work and after it works does it actually set the value of this variable because the result here is going to be the value of this variable. So let's hit Run to test it.
First it's going to ask for the name. Then the number of days, and here's the prompt for Otherwise. So we'll select this one here and hit OK. We can see here in the result that is indeed what the variable is set to. So now the next thing we want to do is build the text based on those three variables. So we want to use another script for that. But first we want to pass in all three variables into that script. How to do that in Automator with JAVA script is pretty simple. Use Get Value of Variable like that and we'll break the input here, so Ignore Input like that, and this will get us the first variable.
We do it again but now we're going to do the second variable. Actually we want to put these in order. So the first variable should be Name, the second should be Days, and we'll do it one more time and the third one should be Otherwise. So we have a series here. It starts off fresh to get the value for this variable pass it into here. Get the value of the second variable and pass it into here and get the value of the third variable. Now we're going to pass that into a script. So all three of those values are passed in as input as an array. You can see here the JAVA script function asks for input. Input will now be an array with those three values in it.
So here's the script for this. It's a pretty simple one. You can see here we keep the first line. This is going to bring up the variables in as input. Then we setup a new string. Response equals and we set it to some text. But notice I'm not starting a new command here. I didn't put a semi-colon. Instead I just write on the next line, I do a Plus which will append text together and I bring Input [0] the first item in the array as the next part. Then I continue Plus and some text here. Then Input [1] the second item more text. Input [2] the third item and then a little more text just a period there at the end.
Then I finally put a semi-colon to end this statement. This could all be done on one line but it will look neater if I break it up on several lines like this. JAVA script still considers this to be just one command. Then I return this response. Let's look at the results for this when we run this. So I'll hit Run. I'll do a name, I'll do a number of days, I'll just use the down arrow there to easily select one of these and hit Return. Now I can see the response here includes the name, includes the days, and includes the bit of text here at the end and puts it all together. This is perfect.
So now, because I said at the very top here Output Replaces Selected Text, I should get this as output from the script. Let's Save this now, do File, Save, and you can call this anything you want. I'm going to call this Support Response, and hit Save. Now one last thing I want to do is change Workflow receives current and instead of Automatic Text I'm going to say No Input. Now notice since I've added all this stuff here Automator recognizes that I am outputting something so it's going to allow me to keep this checkbox, Output Replaces Selected Text, even though I'm receiving no input because we're never going to select text and have it replace something with this. We're just going to have to insert it where the cursor is.
So let me Save it again and now let's go and switch to Mail. So now here I am in Mail and I'm going to type something into the body but I'm want to use my script instead. I can go to Mail and then Services and I should see it here at the top under Text. If you don't see it here at the top it could be because you forgot to have it select No Input. Because there is no input here. I haven't selected anything. So if you have it selected to Automatic or Receives Text Input it's going to say Oh, I can't put this here because nothing is selected.
Now I can select this here. It's going to give me my prompts. The first time you do this it may actually ask you for permission to run the script in Mail. Now we do those. We select one of these. Hit OK and you can see it inserts the text right where I want it. Now all I need to do is set a keyboard shortcut. Go to System Preferences to do that. I can also access that by going to Mail, Services, and see at the bottom where it says Services Preferences. If I do that it jumps right to where I need to go. I can look for the script here. So it's going to be all the way here under Text. I'll see Support Response and I can add a shortcut. So I can do, like, Control Option Command R and you can see that there's the shortcut.
So now when I'm in here I notice that I would go to Mail, Services it will actually show Control Option Command R as a Support Response. I'll try that here. Hit that and you can see it's going to prompt me for the things and I can easily insert that there.
You can create several different versions of these. Or if you're good with coding you can use more prompts to actually customize this further. So you can have a whole tree of inputs that asks for different pieces of information. Create this string in JAVA script and return it here as text to make responding to emails or filling out a form on a webpage very easy.
Here are the JavaScript elements to save you from typing them from scratch:
function run(input, parameters) { app = Application.currentApplication(); app.includeStandardAdditions = true; var choices = ["continue to wait", "try contacting us again", "try calling"]; var selection = app.chooseFromList(choices, {withPrompt: 'Otherwise?', multipleSelectionsAllowed:false}); return selection; }
function run(input, parameters) { var response = "Thanks for your inquiry " + input[0] + ". We will respond within " + input[1] + " days. If you do not hear from us by then, please " + input[2] + "."; return response; }
Is there a shortcut that returns date ?
Sony: A simple script like this would do it:
Hi,
I followed this script & it works until the last "Get Value of Variable". It appends each get value of variable in the results, then, once getting to the last get value of variable, it removes all of the other get value variables & sets the result to only the last get value of variable.