Automatic screenshots upload to SeaFile

As you probably know, I am a big fan of SeaFile. I like to have my data in cloud. It is very convenient. What I don't like is to share my private data with other people. Or companies. I just like to have a control over my data. So some time ago I was researching some alternative to Dropbox and after few tries I found SeaFile to be the most suitable for all my needs.
There are hoverer two task I was still using Dropbox for. One of these were the nice ability to automatically upload recently taken screenshot to the Dropbox and quickly share it. This is really nice feature.
Recently Brian Teeman wrote an article about taking screenshots to share online.
This article is very interesting and if you are looking for an easy to deploy solution for it you should definitely read it. But Brian's solution just wouldn't fit exactly to my workflow so I created my own.

So here is my "how to automatically upload a taken screenshot to your own cloud (SeaFile), and create a short URL using YOURLS URL Shortener on Mac OS X"

Changing the Screenshots' location

The first step is to "convince" OS X to store created screenshots in other than Desktop location. I have a folder called "Synced" in the default "Documents" folder which contains all my with SeaFile synced files and directories. In this directory I created a subdirectory called "Screenshots". Now we just need to tell OS X to store these screenshots in this directory.

  • Open a terminal
  • Execute this command defaults write com.apple.screencapture location ~/Documents/Synced/Screenshots/
  • Then restart the UI server with this command: killall SystemUIServer

From now on, every time we are going to take a new screenshot it will be copied to our synced folder and as this folder is being controlled by the SeaFile, automatically uploaded to our SeaFile server

Follow up

This part was fairly easy I think. We changed the location of the screenshots and as we are now putting it in the by SeaFile controlled folder, it is being automatically uploaded to our cloud server. But now we need take some actions to:

  • Create a public share on our SeaFile server
  • Create a short URL with our YOURLS script
  • Get the created short URL to the clipboard

Obtain API keys

To perform any operation on our SeaFile server we need to get an API key. Also for the YOURLS instance it is highly recommended to use the API key instead of sending the user name and password every time we want to create new short URL.

SeaFile API key

Open terminal again (why have you closed it at all?) and execute following code:

curl -d "username=user[at]email.org&password=YOURPASSWORD" https://your-cloud-url.net/api2/auth-token/

You need of course adjust some parameters of this call:

  • user[at]email.org - replace with the email address you are using in your SeaFile instance
  • YOURPASSWORD - replace with your password you are using in your SeaFile
  • https://your-cloud-url.net - replace with the URL of your SeaFile instance.

You will get as response a JSON encoded which contains the security token

{"token": "cc3cccccccccccccwqqedfdwddb40c"}

YOURLS API key

This is even easier. Simply go to your YOURLS instance under https://yourl.org/admin/tools.php and you will see the token under: "Secure passwordless API call -> Your secret signature token"

Create a bash script

I usually keep all my shell scripts synced too so I created a script called "screenshots.sh" in " ~/Documents/Synced/Scripts/". The location and name of the script is of course up to you but in the further I am going to use this location as default. Here is the content of my script:

  1. #!/bin/sh
  2. fileBase=`basename $1`
  3. file=`echo $fileBase | sed 's/ /%20/g'`
  4. share=`curl -vs  -X PUT -d "type=f&p=/Screenshots/"$file -H 'Authorization: Token cc3cccccccccccccwqqedfdwddb40c' -H 'Accept: application/json; indent=4' https://your-cloud-url.net/api2/repos/2908e2cc-ffa0-4be9-a824-cbd68069b336/file/shared-link/ 2>&1 | grep Location | awk -F ": " '{print $2}'`
  5. surl=`curl -s -d "signature=0abcdefg1334d&action=shorturl&format=simple&url="$share https://yourl.org/yourls-api.php`
  6. echo $surl | pbcopy
  7. osascript -e 'display notification "File has been uploaded to SobiCloud. The (s)URL has been added to your clipboard." with title "Screenshot uploaded"'

Some explanations:

  • Lines 2 and 3: creating an URL conform file name. Unix has no problems with white spaces in the name but for URL protocol we have to replace it with %20
  • Line 4: This is the part which is creating the "share" using the SeaFile API. After the share has been created the URL to our screenshot is being stored in a variable called "share"
  • Line 5: This is the call to our YOURLS shortener via API. After it the short URL is being stored in a variable called "surl"
  • Line 6: copy the short URL to the clipboard
  • Line 7: Show a message using the OS X notifications system

Now we can take a screenshot, copy it to the desired location, upload it to our SeaFile server, create a share and shorten the URL.
To do all those things we simply have call our script and pass the location of recently take screenshot. Something like this:

~/Documents/Synced/Scripts/screenshots.sh "~/Documents/Synced/Screenshots/Screen Shot 2014-11-25 at 21.4"

The only issue is that someone have to actually execute our script with proper parameter automatically.

Automate the whole thing

Fortunately OS X provides a very useful tool called, you would never guess, Automator. This App allow us to add kind of observer to a particular folder which observes changes in this folder and under defined rules can execute some actions.
So here we go. Step by step.

  • Launch the Automator app - I am usually using spotlight for launching applications. You can of course also use the applications launcher or finder
  • Create a new project/file. Select "Folder Action" as document type.

  • Select the right folder (1) - at the top of the project, select the folder you need to watch. In our case it would be the "~/Documents/Synced/Screenshots/"
  • Select an action - From the library on the left site select "Run Shell Script" and drag it into the action area
  • Set the right filter (2) - Set the filter that way that it triggers the action only if the file name contains the string "Screen Shot". I am not sure but it may vary if you are using OS X in other than English language. Additionally set the kind of file to "image"
  • Pass arguments (3) - In the section "Run Shell Script" on the right site, select "Pass Input", "as arguments"
  • Add the command (4) - finally, add the command to execute. In our case it will be our previously created script sh ~/Documents/Synced/Scripts/screenshots.sh "$1"
  • Start it - press the button "Run" in the right top corner.

That's pretty everything. If you have any questions, don't hesitate to ask. I hope you'll enjoy it


Comments powered by Disqus
Located in: Dev
Powered by SobiPro

Joomla! Cycling World Challenge 2015

Community members only. For more information, please have a look at http://jcycle.org/

Distance Participants Goal Prize Start End
38639 56 Most km What? Nothing! Apr 1, 2015 1:00 AM Oct 31, 2015 2:00 AM
# Name Distance
1 Christian Hent 7263
2 Gerald Martin 5427
3 Randy Carey 3248
4 Pisan Chueachatchai 2665
5 Radek Suski 2661
6 Piotr Szarmach 2333
7 Sebastian Boncol 1424
8 Slawek Sikora 1403
9 Stefanie Thielmann 1291
10 Danuta Zarzeczna 1221