« All posts

Mac Automator App to Take a Screenshot, Instantly Upload it to Your Server, and Copy its URL to your Clipboard

Anyone can Google the Mac keyboard shortcuts for taking screenshots: Shift-⌘-3 for the entire screen or Shift-⌘-4 to select a region (or Shift-⌘-4-Spacebar to select a specific application window). You can even throw a Control in there to copy the image file to your clipboard. By default, the image will save to your desktop as “Screen Shot date at time.png.” If you want, you can change the filetype and location (see this TekRevue guide). Or, instead of all that, you could use the “Grab” app found in Applications > Utilities.

But what if you want to quickly share a public URL to that screenshot in an email or text or Slack message? If you use Dropbox, this feature is built in. But, if you’re like me, perhaps you got rid of Dropbox. So now what?

I built this handy service in Mac Automator that will:

  1. Take your screenshot either as a selectable region or a specific application window, triggered by your ol’ familiar keyboard shortcuts;
  2. Instantly save locally and upload your screenshot image file to a server of your choosing (via SFTP, though FTP could be used as well); and
  3. Copy the public URL to that image to your clipboard.

So first, you want to open up Automator on your Mac. This default system app can be found under Applications. Once opened, create a new Service.

Near the top of your workflow you’ll see “Service receives selected…” and a dropdown.  Here you’ll want to select “no input“.

Next, add “Run Shell Script” from the list of actions on the left to your workflow on the right.

Within this new action, select “/bin/bash” for Shell and paste the below code into the code editor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
path=~/screenshots/
filename=screenshot_`date '+%Y-%m-%d_%H-%M-%S'`.png
localfile=$path$filename
remotefile=/$filename
url=https://screenshots.example.com/$filename

screencapture -i $localfile

sftp your_username@123.456.789.01:/path/on/server/to/screenshots/directory/ <<EOF
put $localfile
exit
EOF

printf $url | pbcopy
/usr/bin/osascript -e "display notification "$url copied to clipboard." with title"Grabbed!""

In order for the SFTP connection to work, you must first generate an SSH key pair and add the public key to your server.

At the very least, you’ll want to change “https://screenshots.example.com/” to your site, “your_username” to your SFTP user name, “123.456.789.01” to your server IP, and “/path/on/server/to/screenshots/directory/” to the remote (on your server) path where these image files will reside. (You’ll want to ensure “screenshots.example.com” properly points to “/path/on/server/to/screenshots/directory/” on your server.)

Then, save your Service, naming it whatever you want. Now listen up because this is important as it’s the step that allows you to assign a keyboard shortcut: as of writing this post, Automator doesn’t allow you to save files to the location you need it in, so initially you can save it anywhere, such as on your desktop. Then, in Finder, navigate to your system user’s home directory, then to Library (prereq: you’ll need hidden files and folders made visible on your machine) and then Services.  Drag your newly-saved Service from your desktop (or wherever you saved it) to this Services directory.

Now you can open System Preferences, choose Keyboard, then the “Shortcuts” horizontal tab, and finally, the “Services” vertical tab. Scroll down and expand “General,” and boom! You should see your custom Service listed there. Check it, then click “Add Shortcut” to define whatever combination of keyboard keys you’d like to use to trigger the taking of a screenshot. I chose Shift-⌘-4 since that was familiar.

Leave a Reply

Your email address will not be published. Required fields are marked *