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.