AppleScripts is a super useful tool to create time-saving shortcuts and improve your workflow. If you're looking for a great way to streamline your process on Mac, whether it be through application usage or mouse+keyboard automation, AppleScripts is the way to go.
In this video, I'll show you how to:
1. Create two microautomations in AppleScript to save out multiple Google Slides presentations as PDFs.
2. Take a full-page screenshot with Google Chrome.
I also provide a step-by-step guide on how to code the microautomations and set up helper applications and scripts. If you'd like to see the script in action jump to 14:05.
This tutorial is available as written instructions within my e-guide at:
http://prodshop.co/automate
Helper scripts and text expansion shortcuts needed for this lesson can be downloaded at:
http://prodshop.co/automate-with-applescript
or https://prodshop.co/a2mate
(Try right clicking and selecting Copy Link Address, and pasting into a new window if the link from YouTube doesn't work directly).
If you need additional instruction to set up the helper tools, visit the following links:
SizeUp: http://prodshop.co/sizeup-setup
Alfred: http://prodshop.co/alfred-setup
Brew: http://prodshop.co/brew-setup
Transcript
Hey I'm Luke and this is How I Automate my job well, some of it anyway, using AppleScript
In this video I'm going to show you
two examples of how I automate my job using
AppleScript
coding with AppleScript can be quite complex,
but fortunately
automating the mouse and keyboard is very
easy.
That is after all how us humans use the computer.
One quick thing, I made it an e-guide with
all of the steps
in this video so if you'd like to follow along
with written instructions you can download
that.
There are a couple of helper tools that I
use to simplify the AppleScripting workflow
The first is Typinator which is a text expansion
program that allows us to code with keywords
instead of having to write out the entire
code.
The next is SizeUp which is a window management
program. Whenever we are doing clicks with
AppleScript
it's important that the clicks always land
in the same place and SizeUp will help move
the window into the proper position.
Next is cliclick or command line interface
click
AppleScript doesn't do a good job by default
when clicking on things and this will help
with that
Finally is Alfred App, it's an application
launcher and one of my most favorite programs.
This will help us run AppleScripts a little
bit easier.
Let's get started
Go ahead and open up your browser, I'm going
to use Google Chrome
and go to prodshop.co/automate-with-applescript
This will download some helper scripts that
I've created to click on things and also write
new AppleScripts
along with the text expansion shortcuts that
I've created for you.
Go to your downloads folder and go ahead and
open up the zip
And you can see the scripts and text expansion
shortcuts there.
Let's start by installing Typinator
So just Google Typinator
and it will be the first thing that pops up
The trial version is fine. If you end up liking
text expansion I definitely recommend getting
this program.
I've also included for Text Expander if that's
your preferred text expansion program.
Once Typinator is downloaded and installed,
and open
go ahead and just open up the lukes-applescript-shortcuts
file in the package you downloaded
and it will automatically import.
I'll go ahead and delete it so that way I
don't have two copies.
Next, let's download SizeUp
Google SizeUp and it will be by Irradiated
Software.
Once you have it downloaded and installed,
go ahead and open it
and you'll see the SizeUp icon which looks
like a little window with four arrows around
it
Go to preferences
and then go to the General tab, and make sure
that the Show Visual Action Overlay is deselected
By default, SizeUp will show an overlay every
time that it's activated so I recommend disabling
it.
Let's go ahead and install cliclick. But to
do that we're going to need to install Homebrew
which is a package manager.
If you don't know what that is, don't worry.
Go to brew.sh
Triple click on the line below install Homebrew
Open up Terminal, command space, Terminal,
enter. If you're using Spotlight that's fine.
Paste the line of code, push enter, and it's
going to ask you for your administrator password.
The cursor won't move when your typing but
it is working.
It's going to take a minute or two to install
and once it is installed go ahead and do command
Q to quit Terminal
and then open up Terminal again.
For some reason it needs a refresh after installing
brew.
Once you have that installed type in brew
install cliclick
push enter, and it will do its' magic.
Once cliclick is done installing
Let's go to System Preferences because we
need to grant some security privileges to
the applications that we're gonna use.
To do that, go to the apple at the top left
click on System Preferences
Go to the Security and Privacy tab
Go to the Privacy tab, the last option
Go down to Accessibility
Click on the lock
Enter your administrator password.
Click on the plus sign.
And then we are going to add a couple applications.
It's easiest just to search for them.
Type in Typinator.
And do open.
We'll do Terminal
so we can do a test.
SizeUp
and then Script Editor.
Once that's done you can close System Preferences,
command Q.
And let's go ahead and run a test with cliclick
in Terminal.
Type cliclick c: (which stands for click),
zero comma zero.
And then as we push enter it's going to click
on the xy coordinate zero zero.
Boom.
Pretty cool huh?
The next step is to setup Alfred.
You can go ahead and go to alfredapp.com.
Download the free trial which will be fine
for the purposes of these examples.
And once Alfred is open, go ahead and go to
the menu bar at the top, click on the hat
icon, and click on Preferences
Go to the general tab.
And I like to change the default keyboard
shortcut to be command space.
Alfred does the same thing Spotlight does
but it's much more powerful.
So I'll show you how to change the shortcut.
Go to System Preferences. Command space System
Preferences, enter.
Go to the keyboard panel.
Shortcuts
Spotlight
and make sure that the two selections there
are deselected
Once that's done, you can do command Q to
quit System Preferences
click in the Alfred Hotkey field, and do command
space to change the keyboard shortcut.
Next go to Features
and then in the extras area make sure that
AppleScripts is selected.
Once that's done you can close Alfred Preferences.
So let's start to make our first AppleScript
What we'll do is we'll save out a Google Slides
presentation as a PDF
and then we'll create a loop to multiple Google
Slides presentations
Once time my manager asked me to save over
50 Google Slides presentations
and after doing a couple I created this microautomation
to make it much easier.
Go to Google Chrome
and type in slides.new
That's the Google shortcut url for new Google
Slides presentations.
I'm going to go ahead and to command T to
open a new tab and do that five times
or, four more additional times
Once you have them all open, let's go to the
first one
and we'll review the steps that we're gonna
do.
We'll click on File, Download As, PDF Document
then we'll use the keyboard shortcut to go
to the next tab
which is option command right key
If you're ever looking for a keyboard shortcut
in a program
you can do shift command question mark
and then start typing what you want inside
of the search within the Help menu
and you can see if the keyboard shortcut you
need is there
Otherwise a quick Google search will do it.
Go back to the first presentation
and let's use the helper script that you downloaded
to create a new AppleScript
so once Alfred is open and ready to command
space
new applescript se
SE stands for script editor
push enter
A dialog window will open asking what the
file name should be
I like to always preface it with the name
of the program that I'm using
so chrome dot, and then a description
slides as pdf
push enter
and script editor will open our script with
the proper code that we need.
Go ahead and do shift command S, remove the
word copy
push enter
and then replace, and then you'll be good
to go
Going forward you can do just command S
The first step will be to click on file
but before we click on anything let's make
sure that the window is perfectly centered
that way the clicks will land in the same
spot each time
Go to SizeUp
and with Google Chrome selected
you can do option command control C
or you can use the selection inside of the
menu
There we go.
Now that it's centered
Hover your mouse over the word file
and then launch Alfred without moving the
mouse, command space
and type in mousecode
push enter
a dialog window will come up asking for a
coordinate marker name
This is just a variable to keep track of each
click
So I'll just call this one "first"
What you should see is Script Editor activate
and the code that we need be pasted inside
of the window.
Go ahead and do command S to save
and let's go for our second click
I'll go back to Google Chrome, using command
tab to kick over
I'll do File
hover over Download As
command space to launch Alfred
I'll type mousecode again
push enter
I'll type the coordinate marker as "second"
and then the code for the second script will
appear in Script Editor
one more time
File, Download As,
and I'll hover over PDF document
command space, mousecode, enter
"Third"
There we go.
Now the next thing we need to do is tell the
script
"Hey, I want Google Chrome to show up before
we click in these areas"
If you would run the script as is, it will
just click on whatever program is in the front
So go to line right below "start coding below"
and we're going to activate Google Chrome
The full code is
tell application "Google Chrome" to activate
but the text expansion shortcut that I created
just takes the word "tell" plus the first
letter of the word application, making the
shortcut tella.
So type, t e l l a, and then a dialog box
will appear asking which program do you want
to activate
Type Google Chrome
then push enter
next we want to make sure that the window
is centered
the text expansion shortcut for this is centerwindow
Awesome.
Now we can test it out
Go ahead and do command R to run the script
Awesome
If it didn't work, go back to Script Editor,
go to the very top of the script and change
the delayOne variable to .4
Now the reason that there's a delay in between
each step is if you send a bunch of AppleScript
commands to the computer
it's kind of like pushing a bunch of keys
on a typewriter, it's gonna get stuck.
So, coding it as a variable, and then putting
it after each step
will help AppleScript to do its' thing.
And if you want to increase the delays you
can just change it at the top line and you'll
be good to go
rather than having to change it throughout
the code.
The next step will be to perform the keyboard
shortcut to go to the next tab
which will be option command right key
Now a little bit about typing with AppleScript
there are two methods, there's keystroke
which allows you to type letters, numbers
and punctuation.
And then there's key code, which will allow
you to push page up, page down,
down, left, right, things like that.
In order to look up the key codes I've programmed
a text expansion shortcut called keycodes
So go ahead and launch Alfred and type keycodes,
push enter
and you can see a nice diagram that you can
reference the key codes that you need.
So right key is 124
but to make things easier I have other text
expansion shortcuts for commonly used keys,
and we'll go through those now.
So go back to Script Editor
but before we push a key with AppleScript
we need to activate the System Events application
Basically saying, "Hey, we're going to use
the keyboard."
So to do that, just type s y s e.
The cursor should move automatically inside
of the clause.
And once it's there, go ahead and type rightkey
all one word
Perfect, the code we need is there
Now we need to add the modifier keys to the
right key
So after 124, type using
and since we're using multiple modifier keys
we'll need a curly bracket, so
open curly bracket, command down, option down.
So it's just saying push the right key, with
command down and option down at the same time.
Let's go to our first presentation in Google
Chrome
go back to Script Editor and do command R
to run it.
And we should see a PDF download and then
it jump to the next tab.
Boom, boom, boom. Yep it made it to the second
tab, you can see here.
Awesome.
The last step is to put it in a loop.
So basically we'll have a dialog appear and
say how many times do you want to do this,
and then we'll execute the code.
Let's go back to Script Editor, I'm going
to maximize the window a bit, option command
C, or control option command C, the three
to the left of the space bar.
Select everything below the phrase start coding
below, do command X, HA!
And then type loop setup.
The cursor should move below the line repeat
numberOfTimes times
and do command V, command S to save, and it
should compile correctly.
Now here's what's going on in the code.
There's going to be a question, how many times?
And then, that number is going to be saved
as a variable, numberOfTimes.
Then this code, will repeat
numberOfTimes times.
Let's try it out.
Let's go to Google Chrome,
make sure that the first presentation is selected.
We'll go back to Script Editor and do command
R
A dialog will pop up, I'll push 5
Boom.
Yes, 2.
Boom, boom boom.
Yes, yes, yes.
Awesome, awesome, awesome.
Kewl, Kewl, Kewl.
Pretty cool huh?
So if you had 50 of these to do, you would
save a lot of time.
The next example we're going to do is to take
a full page screenshot in Google Chrome.
Let's get to it.
First let's review the steps.
We'll do shift command C to open the Inspect
Element panel.
Then we'll do shift command P to open the
command menu.
We'll type capture full.
Then we'll push enter.
Then once it's done we'll have a conditional
statement that'll say, has this downloaded
yet?
And then click on the X to close the Inspect
Element panel.
Let's get started on the script.
So command space, new applescript se.
I'll call it chrome.screenshot
push enter
I'll do shift command S, take out copy, replace
it,
now we're good to go.
So the first step will actually be to activate
Google Chrome
so, tella, Google Chrome
next we will say, "Hey, we need the keyboard"
because we're gonna do shift command C.
So s y s e, put the cursor inside of the clause
if it's not there already.
Using a modifier key along with a letter is
common, so I have the text expansion shortcut
for that.
It's k s s.
And then I'll just type the letter C.
I'll go ahead and add shift to the modifier
keys, so I'll put a curly bracket in front
of command
comma after down, and then write shift down.
The next keyboard shortcut is shift command
P
so I'll just copy this code and change the
C to a P.
Next we'll type capture full
Now in the same way that we can't send a bunch
of commands to the computer with AppleScript
without it freaking out
This happens for phrases and longer words
using the keystroke method
So the text expansion shortcut to get around
this is text loop.
The first field will ask you what you want
to type
So lets' type capture full.
The next field is just asking for a variable
name to differentiate between this and any
future text loops that you might use in your
script.
So I'll just call this firsttypevar, push
enter
command S, and what this does is it sets up
a loop and it will put a small delay after
each key is pushed.
Finally, we'll push the enter key, or the
return key.
The text expansion shortcut for that is enterkey,
or returnkey, I put both just in case.
Wink.
And then the final step will be the conditional
statement saying has this downloaded yet?
And then clicking on the X.
Go ahead and type continue loop.
It'll ask you what the question should be,
and I'll just put has it downloaded?
push enter.
Command S
and then put your cursor inside of the clause
where it says, "If the answer to the question
is yes, do the following things"
What we'll want to do first is activate Google
Chrome again
because sometimes when a prompt comes up,
it'll change the focus from your previous
program
so type tella
Google Chrome
Then we'll click on the X.
So I'll go ahead and go to Chrome
Make sure that my window is centered
Option Command Control C
I'll do shift command C, and then wherever
the X is for yours', hover over it
command space to launch Alfred
type mousecode, and then name of the coordinate
marker
I'll call it clickX
and the Script Editor will jump over and put
the code where we need it
Before we run the script I'm going to go to
the top and increase the delay to .4
Opening the Inspect Element panel sometimes
takes a little bit of time, so we want to
make sure that it opens then we'll start typing.
So I'll do command R, shift command C
typing
wait for it to download
There it goes, once it's all downloaded, I'll
just push enter
Boom, the panel closes
Magic!
If you have any questions or need any help,
feel free to write a comment
send me an email
or join the productivity shop slack channel.
Thanks for watching!
[jingle]