How to autoload screenshots on FTP in OS X or a story about how we are not looking for easy ways

Hello!

As you know, programmers are people who are not looking for easy ways. Recently, I had a need to automatically upload freshly-made screenshots to my ftp (my own and no other). A long search for software that solves the problem for OS X over the Internet led me to a slight depression. I didn’t even look for paid software, because I don’t want to pay for functions that I myself can write, and free programs hit me to the bone, with their crookedness: screenshots were downloaded not the first time, sometimes only half of the screen was downloaded, and generally, from mine the tendency to perfectionism, to take a screenshot of a window and not see on it a beautiful, beautiful shadow, which leaves the standard OS X utility - this is hell. Therefore, it was decided to take Saturday night by solving this problem with one's own hand.

Without hesitation, I remembered about the amazing feature of Apple's OS - Automator and decided to automate uploading files to FTP and talk about the automaton to the habrasociety and apple users who are not aware of it, so there will be a finished file that you can just start and everything will not happen. and an explanation of what's what, in order to teach how to use the utility and inspire others to automate actions in their system.

Under the cutter theory, practice and many pictures.

Automator- A kind of wrapper for shell script in * nix from Apple, allows you to automate everything in the world, absolutely any movement of the user. Once upon a time I used it to build my graduation project on Node-Webkit, the automator allowed me to recompile the project by pressing the hotkey combination, pack it into the * .nw archive and start it right there.

Bit of theory

There are several types of scripts in Automator that you can create:

1. Workflow - A simple source of the action that we will program.
2. Application - The same workflow, only it can be launched and it will work independently in your OS X
3. Service - Everything is the same, only the script will be available in the “Service” tab of the programs that you select when creating the script (for this thing you can then hang up the hot keys)
4. Printing Plugin - Workflow, which will be available in the print window
5. Folder Action - a hook that will start immediately when the folder is updated (add, delete the file)
6. Celendar Alarm - there's nothing to say, so it’s clear: we create a workflow that works on an event in the calendar (actually the cron of tasks a)
7. Image Capture Plugin - workflow, which is available in the window for receiving images from the camera (accepts the captured image for input)

Now a little practice

It is clear that we need to catch the action of creating a screenshot and immediately after it - upload the screenshot to the server, so our choice of script type is Folder Action.
By default, all screenshots are created in the desktop folder and it is clear that you can’t hang a hook to upload a file to ftp to the desktop so as not to trash your server, so we will have to reconfigure the standard screenshot creation utility a little:

  1. Open Terminal
  2. Create a screenshots folder in the ~ / Pictures folder and forward all the screenshots that the utility will create there:
    mkdir ~/Pictures/screenshots && defaults write com.apple.screencapture location ~/Pictures/screenshots



Now our screenshots will be created in this folder.

Go to Automator itself and do the following:

  1. Create Finder Action
  2. In an open window, we see a menu with a collection of actions and a workspace. We need only 2 actions: “Run Shell Script” and “Display Notification”, they are located in the “Utilites” section. Add them with a simple drag and drop.
  3. In the selection at the very top of the workspace, select the folder that we will monitor
  4. In the “Run Shell Script” block, set “Pass input: as argument
  5. In the “Run Shell Script” block itself, you need to write a sh script that will upload files to the server, I will not list it here, because the topic of the article is different. Here is a link to github where you can get it, read the comments, it says how to configure it for your ftp server. The script will upload a new screenshot and copy the URL to it to the clipboard.
  6. You can write anything in the “Display Notification” block. This function will give you a standard notification with your text.
  7. Save


That's not all. Now we need to tie the hook to the folder, for this we need to right-click on the folder, select Service-> Folder Actions Setup ... and select the script that we just created.



In order for everything to work, you need to restart the computer.
And that’s all, we take a screenshot as we did before and it will automatically be downloaded to your ftp server.

PS I want to know if there is still need for articles on what and how to do using Apple Automator and other features of the OS X system that are talked about a little?

Also popular now: