Blog over OpenSource,Web en ICT in het algemeen

Build your own screenshot upload tool on osx

This tutorial shows you how to build your own screenshot upload tool like the excellent TinyGrab. The advantages are clear building your own. It’s fun to do and you keep control of all your images.

What you need

- Apple's OSX
- Access to an FTP server
- Automator
- PHP knowledge

The reason I use an PHP script is simple. I know how to use PHP and after the upload has succeeded I use the API from TinyUrl to return an short url which is being copied to the clipboard. There are many different ways you could use the automator, this is just an fairly easy way to accomplish the objective, your own upload tool for screenshots.

Let’s get started!

Screenshot and location
If you want to make a screenshot of a portion on your screen you use the keyboard keys: CMD-SHIFT-4. Your cursor will change to an circle with an plus sign. Drag use the mouse the part of the screen you want to snatch and after releasing the mouse an snapshot sound marks the screen capture as complete. On your desktop an PNG image appears. We want to change the location were the images are stored.

Create an folder in your home folder an call it scripts. Create another folder inside scripts called screenshots. The path is used in the next step.
If you created it in your homefolder the path will be: /Users/yourname/scripts/screenshots

Now open your terminal through Applications/Utilities/Terminal. We change the default location by typing in the following command:

defaults write com.apple.screencapture location /Users/yourname/scripts/screenshots/

In order for the screencapture location to take affect we need to logout and login again.

The scripts
Create an new PHP file in the sripts folder you created an name it what ever you want. I called it files.php. Open it in an editor an add the following code. You will need to change the FTP user account and address ofcourse.

$ftp_server = 'ftp.servername.com';
$ftp_user_name = 'username';
$ftp_user_pass = 'password';
if ($handle = opendir('/Users/yourname/scripts/screenshots')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".."){
$file = 'screenshots/'.$file;
$path = '/remote/server/path/to/uploadfolder/';
$date = date("YmdHis");
$rfile = 'screenshot'.$date.'.png';
$remote_file = $path.$rfile;
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
} else {
echo "There was a problem while uploading $file\n";
}
ftp_close($conn_id);
}
}
echo(createTinyUrl('http://www.yourserves.com/remote/path/screenshots/'.$rfile));
closedir($handle);
}
function createTinyUrl($strURL) {
$tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$strURL);
return $tinyurl;
}

You have to make sure that the weburl you send to TinyUrl is accordingly with the path were you uploaded the screenshot ofcourse. Otherwise you cannot access the image from the internet.

Save the file and exit.

To make the file accessible later on by Automator the best thing you can do is create an shell script and set it so that it can be executed.
Create an empty file in the scripts folder called start.sh and open it in an editor.

#!/bin/bash
php files.php

Save it an exit. Now set it executable by running the following command:

chmod +x start.sh

Fun with automator
Now open automator from the applications folder and choose workflow template.

Now we can drag actions from the left to the workflow designer. I have used the following steps:

  • Run shell script
  • Find Finder Items
  • Run shell script
  • Copy to clipboard
  • Find Finder items
  • Move Finder items to trash
  • Show Growl Notification

These are the steps and their settings in Automator.

shell script 1

finder items

copy to clipboard

finder items

move 2 trash

growl message

The last Growl message will appear when you successfully uploaded the image.

My growl message, in Dutch ;-)
growl

Conclusion:
Using the OSX’s possibilities for automating processes is unlimitied. I would like to know what you do using Automator or tell me what you think of this simple tutorial and how to get it better and faster. Maybe you have tips for adding error control for instance.

Lees ook deze:

Leave a Reply

Comment moderation is enabled. Your comment may take some time to appear.

Spam protection by WP Captcha-Free