Automating Complicated Text Inserts

If you need to insert similar text into documents often, you may want to create a JavaScript (JXA) task in Automator that makes it easier. In this example, we'll look at inserting a piece of text that contains two variables, such as: (Refer to page X or report Y). The script will prompt you for the page and report, and then insert the test into the document you are working on.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: Automating Complicated Text Inserts.

Let's say that you have to do a lot of typing where you repeat the same phrase often. For instance, in this example document you can see I've got this phrase refer to page number X of report Y. You can see I've repeated here a few times. Maybe my work requires that I type this kind of thing over and over and over again but with slightly different reference points here. So in this case the page number and the report ID. So you want to be able to make this a little simpler. There's an app that you can purchase that would do that.

But you can do it without those. One option is you can use System Preferences and under Keyboard, Text you can create a shortcut. You probably already know this. I can repref is going to be this text which has blank lines here for refer to page number....of report... like that. If I do this and I want to insert another reference I do repref and I hit space, I get the text there and I can type in something like that.

But what if I wanted to make it a little easier. If I wanted it to have it actually prompt me for the missing pieces of information and then insert them so it automatically did it.
You can do this using a little Automator script. So I'm going to run Automator and I'm going to create a New Service. Then I'm going to set the service so that it receives no input and I'm going to just put one thing in here which is going to be a script. Now it used to use AppleScript for this kind of thing. But Java Script has kind of taken it over and it's more modern language. So I'm going to use Java Script instead.

So I'm going to run Java Script and put that in there and I can type in my Java Script right there. Java Script is also called JXA. Java Script for automation. JXA kind of abbreviates that. The script that I'm going to put in here is pretty straight forward but I'll explain it line by line as I paste it in.

So the first part that I'm going to paste in here is just some Declarations. I'm going to declare a variable called app and set it to the current application. You pretty much need this for almost any Java Script for automation script that you create.

Then I'm going to create another variable called SE which stands for System Events. It's basically going to be a way for me to trigger system events. I'm going to use that to paste in the result.

Then I'm going to, for the current app, include standard additions, set that equal to true. That is something that enables a lot of the automation stuff for whatever app you're running.

Now I'm going to have it prompt me for the two pieces of information it needs. So the first one is it is going to set a variable called Page Number and it's going to display a dialogue and it's going to ask for page number question mark with the default answer blank. Now you may want to put something in here as a default answer. Like if most of the time it's page number one you may want to have it prefilled in with one so you can just hit return.

Then for Report ID it would do the same thing. Ask for Report ID question mark with the default answer a blank. So it's going to actually come up with two prompts. I can test this at this point by hitting Run and you can see it's going to ask for page number, I'll say 4, for Report ID, 78A, and it's done. So we know that part works.

Now we want to assemble the text that we're going to paste in. So we're going to create a new variable called Output and we're going to put a bunch of strings together. So the first part is something defined by single quotes. This is going to be parenthesis Refer to page number with a space, then the plus symbol to append strings to each other, then page number dot textReturned. Page number is actually going to get the results of display dialogue which includes a lot of information. We just want the text part of that, so textReturned. Then we're going to add space of report space and then Report ID textReturned. So the return value of this. Then close parentheses and a period.

Now all that is left is to paste that into place. So we will use Set Clipboard to set the clipboard just like if we used Command C to copy to the value from here. Then we're going to use the System Events that we defined up here to do a system event, the keystroke, and press the V key. Using Command down. So Command V, in other words paste. The will paste the text where the cursor is in whatever app we're using.

So let's Save. It's automatically going to save the service in a place where it could be accessed throughout the system. So we just have to come up with a name. So we'll call the Report Reference. We'll save it. We can see where it is here now. It is under Library, Services, Report Reference. That will mean that it can be accessed in any application by looking in the Services menu for Report Reference.

So let's give it a try. So in Pages here I've got my cursor ready to insert that text. I go to Pages, Services, Report Reference. It prompts me and it inserts the text right there. So it's real easy to do. I can go to System Preferences and make a Keyboard shortcut for this. So go to Shortcuts, Add Shortcuts, and I will add an app shortcut for Report Reference which is the exact name in the menu and I'll do like Command Shift F1. Now I can use that, Command Shift F1 and it prompts me.

Now note that there's a tricky part here. If I go into Automator you can see that sometimes I need to add these two lines to get it to work. After the clipboard is set that works fine. But I noticed that sometimes, perhaps because of something else I've got running on my Mac, this pasting doesn't work unless I tell it to specifically activate the application even though it should already be running and put a slight delay in there. So you may want to add these two if it's not working. Of course this ties the script to a specific application, in this case Pages, which is fine because that's what I'm using here.

So keep that in mind that you may need to add those two to get it to work on your Mac.

Here is the text of the JXA script:

Comments: 9 Responses to “Automating Complicated Text Inserts”

    Jeff White
    2 years ago

    var pageNumber = app.displayDialog(‘Page Number?’, {defaultAnswer:”});
    when I compile your script I get this error:
    Error on line 8: SyntaxError: Invalid character ‘\u8216’
    The first line of this message is line 8
    Do you know what error is?

    2 years ago

    Jeff: Make sure you don’t have an extra invisible character somewhere. Not just on that line, but the next one (which is line 8 if you copied and pasted).

    2 years ago

    What is the application you mentioned in your video?

    2 years ago

    Jean-Claude: Not sure what you mean. I am using Automator in the video. I am demonstrating the script by inserting text into Pages. If you mean something else, let me know the time in the video where I mention it and I will try to clarify.

    2 years ago

    At approx 34 seconds in the video, you say “there are some apps you can purchase…”

    2 years ago

    Jean-Claude: Apps like TypeIt4Me and TextExpander.

    2 years ago

    OK, thanks!

    Russell D
    2 years ago

    That is the coolest thing I have ever seen. I have been using Mac for six years and am stil learning things it can do.

    Ulrich Maasmeier
    2 years ago

    There is another app for text automations: Typinator.
    I use it since textexpander went on to monthly subscriptions.
    Works great.

Comments Closed.