<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Voodoo Programming &#187; CC Licence</title>
	<atom:link href="http://www.cneophytou.com/tag/cc-licence/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cneophytou.com</link>
	<description>Things programmers do that they know shouldn&#039;t work but they try anyway, and which sometimes actually work, such as recompiling everything.</description>
	<lastBuildDate>Fri, 12 Nov 2010 17:07:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Photo feed as Desktop Background</title>
		<link>http://www.cneophytou.com/2008/04/17/photo-feed-as-desktop-background/</link>
		<comments>http://www.cneophytou.com/2008/04/17/photo-feed-as-desktop-background/#comments</comments>
		<pubDate>Thu, 17 Apr 2008 15:14:57 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[Chromasia]]></category>
		<category><![CDATA[ddoi]]></category>
		<category><![CDATA[Feed]]></category>
		<category><![CDATA[Google Apps]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[RSS]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/?p=110</guid>
		<description><![CDATA[I first came up with this idea after reading a post on HackerMoJo.com about getting an RSS feed of images as a Screen Saver using a python script. The idea is simple: Take a feed, parse it, and save the photos it links to as numbered images in a folder on the disk, then use [...]]]></description>
			<content:encoded><![CDATA[I first came up with this idea after reading a post on <a href="http://hackermojo.com/mt-static/archives/2008/04/metropulse-launched.html">HackerMoJo.com</a> about getting an RSS feed of images as a Screen Saver using a python script. The idea is simple: Take a feed, parse it, and save the photos it links to as numbered images in a folder on the disk, then use that folder as a Screen Saver. Well, as it turns out you can also use that folder as a desktop background, but there are a few improvements that can be made.<br />
<br />
First, the script only worked with that particular feed. If you wanted to use a different feed, you would have to change the code. Second, the script would always download all the photos linked to from the feed, thus increasing the load placed on the hosting server. I wanted to use the script to download the photos from <a href="http://www.chromasia.com/iblog">Chromasia</a>, which brings me to point number three: some feeds don't link to the large photo, but instead only include a thumbnail. To solve these issues, I created a small application using Google's wonderful <a href="http://code.google.com/appengine/">App Engine</a>, which takes care of everything. More on how to use this after the jump.<br />
<span id="more-110"></span>
<br />
This application takes care of keeping a database of the most recent photos linked to from the provided feeds, thus balancing the load placed on the hosting servers, and also allowing me to serve the feeds in a single standardised format. So far, the script supports the feeds of David Nightingale's <a href="http://www.chromasia.com/iblog">Chromasia</a> who kindly agreed to let me download web-scaled versions of his photos for use with this script and also helped me test early versions of the app, and Sam Javanrouh's <a href="http://wvs.topleftpixel.com/">daily dose of imagery</a>.<br />
<br />
There are several ways to use these feeds. The easiest way is through iPhoto 6 or 7. Just subscribe to the iPhoto photo feed for either chromasia (<a href="http://deskfeed.neophytou.net/feed/chromasia">http://deskfeed.neophytou.net/feed/chromasia</a>) or daily dose of imagery (<a href="http://deskfeed.neophytou.net/feed/ddoi">http://deskfeed.neophytou.net/feed/ddoi</a>). There are three down sides to using this method: You have to open iPhoto every day to refresh the feeds manually, every time iPhoto refreshes it has to download all the photos over again, and you can only use the photos from a single feed, not multiple ones. Also in Tiger, I believe you can only use an iPhoto photo feed as a Screen Saver, not as a Desktop Background.<br />
<br />
The other way to use this application, is through a small Python script which I wrote. To use this, you need to create a folder called DeskFeed inside the Pictures folder located in your Home directory (resulting path: ~/Pictures/DeskFeed, where ~/ is your home directory). Then, <a href='http://www.cneophytou.com/wp-content/uploads/2008/04/deskfeed.py'>download this script</a> into the folder you just created, and run the following commands in the Terminal (found under <code>Applications/Utilities/Terminal</code>):</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x ~<span style="color: #000000; font-weight: bold;">/</span>Pictures<span style="color: #000000; font-weight: bold;">/</span>DeskFeed<span style="color: #000000; font-weight: bold;">/</span>deskfeed.py
~<span style="color: #000000; font-weight: bold;">/</span>Pictures<span style="color: #000000; font-weight: bold;">/</span>DeskFeed<span style="color: #000000; font-weight: bold;">/</span>deskfeed.py</pre></div></div>



<p>
This will automatically download the latest photos from the Chromasia and daily dose of imagery blogs, and save them in the DeskFeed folder. To modify which feeds are downloaded (or not), open the deskfeed.py file you just downloaded and comment out / uncomment the photo feeds you want on the first few lines that are clearly marked. You can have as many feeds as you want in the script, but they have to be generated from the App Engine script which I wrote.<br />
<br />
To set this up as a periodic task using <code>cron</code>, create a text file on your desktop called <code>dfcron</code>, and in it place this single line:</p>


<div class="wp_syntax"><div class="code"><pre class="cron" style="font-family:monospace;">5      1       *       *       *       /Users/username/Pictures/DeskFeed/deskfeed.py</pre></div></div>



<p>
Make sure you replace <code>username</code> with your own short username. What this file contains is a command that will tell <code>cron</code> to run the <code>deskfeed.py</code> file every day at 5 minutes past 1am. Then, run this command in the terminal once:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">crontab ~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>dfcron</pre></div></div>



<p>
You can now safely delete dfcron. To set this folder as your desktop background, go to System Preferences / Desktop &amp; Screen Saver, press the little '+' button under the list of folders, navigate to the DeskFeed folder, and select Choose. I found that "Fit to Screen" with a black background works best.<br />
<br />
I am open to suggestions about what other photo feeds to include in this application, it only takes about 10 minutes to add a new feed. Furthermore, I will be creating a more solid application that will be customisable through preferences rather than relying on a Python script. But as this is a first attempt, I'm publishing the code to the script to see if anyone else has any suggestions for improvement. If you just want to look at the code used instead of downloading the above file, here it is:</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">md5</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib</span> <span style="color: #ff7700;font-weight:bold;">import</span> URLopener
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">urllib2</span> <span style="color: #ff7700;font-weight:bold;">import</span> urlopen
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">dom</span>.<span style="color: black;">minidom</span> <span style="color: #ff7700;font-weight:bold;">import</span> parseString
&nbsp;
rss_feeds = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#</span>
<span style="color: #808080; font-style: italic;"># Comment / Uncomment the following lines to use the required feeds</span>
<span style="color: #808080; font-style: italic;">#</span>
rss_feeds<span style="color: black;">&#91;</span><span style="color: #483d8b;">'chromasia'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'http://deskfeed.neophytou.net/rss/chromasia'</span>
rss_feeds<span style="color: black;">&#91;</span><span style="color: #483d8b;">'ddoi'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'http://deskfeed.neophytou.net/rss/ddoi'</span>
&nbsp;
dst_dir = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">expanduser</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'~/Pictures/DeskFeed/'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Based on script found at HackerMoJo.com</span>
<span style="color: #808080; font-style: italic;"># http://hackermojo.com/mt-static/archives/2008/04/metropulse-launched.html</span>
<span style="color: #ff7700;font-weight:bold;">def</span> parseFeed<span style="color: black;">&#40;</span>rss<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    rss_contents = urlopen<span style="color: black;">&#40;</span> rss <span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Failed to read rss feed %s&quot;</span> <span style="color: #66cc66;">%</span> rss
    <span style="color: #ff7700;font-weight:bold;">return</span>
  rss_src = rss_contents.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  rss_contents.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  dom = parseString<span style="color: black;">&#40;</span> rss_src <span style="color: black;">&#41;</span>
  node_lst = dom.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'item'</span><span style="color: black;">&#41;</span>
  items = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>node_lst.<span style="color: black;">length</span><span style="color: black;">&#41;</span>
  hashes = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
  group = node_lst.<span style="color: black;">item</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'title'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'group'</span><span style="color: black;">&#41;</span>
  <span style="color: #808080; font-style: italic;"># Store the md5 hashes of all the saved photos</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> items:
    img_filename = <span style="color: #483d8b;">&quot;%s%s_%s.jpg&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>dst_dir,group,i<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      image_file = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>img_filename, <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
      hashes<span style="color: black;">&#91;</span>img_filename<span style="color: black;">&#93;</span> = <span style="color: #dc143c;">md5</span>.<span style="color: #dc143c;">md5</span><span style="color: black;">&#40;</span>image_file.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hexdigest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      image_file.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
      hashes<span style="color: black;">&#91;</span>img_filename<span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;&quot;</span>
      <span style="color: #ff7700;font-weight:bold;">pass</span>
  <span style="color: #808080; font-style: italic;"># Go through all the photos in the rss feed, in reverse order</span>
  items.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> items:
    element = node_lst.<span style="color: black;">item</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
    group = element.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">'title'</span> <span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'group'</span><span style="color: black;">&#41;</span>
    link = element.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span> <span style="color: #483d8b;">'link'</span> <span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">getAttribute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'source'</span><span style="color: black;">&#41;</span>
    photo_hash = element.<span style="color: black;">getElementsByTagName</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'hash'</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">firstChild</span>.<span style="color: black;">data</span>
    img_filename = <span style="color: #483d8b;">&quot;%s%s_%s.jpg&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>dst_dir, group, i<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;"># Check all the saved photos and see if any of them have the same hash</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> j <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:
      old_img_filename = <span style="color: #483d8b;">&quot;%s%s_%s.jpg&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>dst_dir, group, j<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>hashes<span style="color: black;">&#91;</span>old_img_filename<span style="color: black;">&#93;</span> == photo_hash<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># If one does, give it the current photo filename</span>
        <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mv %s %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>old_img_filename, img_filename<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        hashes<span style="color: black;">&#91;</span>old_img_filename<span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;&quot;</span>
        hashes<span style="color: black;">&#91;</span>img_filename<span style="color: black;">&#93;</span> = photo_hash
        <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      image_file = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>img_filename, <span style="color: #483d8b;">&quot;r&quot;</span><span style="color: black;">&#41;</span>
      old_hash = <span style="color: #dc143c;">md5</span>.<span style="color: #dc143c;">md5</span><span style="color: black;">&#40;</span>image_file.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hexdigest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      image_file.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>old_hash == photo_hash<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">continue</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
      <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      URLopener<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">retrieve</span><span style="color: black;">&#40;</span>link, img_filename<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
      <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">for</span> key <span style="color: #ff7700;font-weight:bold;">in</span> rss_feeds:
    parseFeed<span style="color: black;">&#40;</span>rss_feeds<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
  main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>



<p>
<a href='http://www.cneophytou.com/wp-content/uploads/2008/04/deskfeed.py'>Download this script</a><br />
<br />
That's pretty much it. As you can see, the script is not that complicated, and I can very easily modify the way the photos are served to the clients. This code has been tested under Leopard using Python 2.5, but also under Ubuntu using Python 2.4.3, and I believe it should also work under Tiger using Python 2.3, but I could very well be wrong. So as always, I'm open to feedback and suggestions. If anyone knows how to create a Preference pane, please contact me. I know exactly what needs to be done in the Preference pane, but I have no idea how to use Xcode.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2008/04/17/photo-feed-as-desktop-background/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Simulating encrypted physical disk in OS X</title>
		<link>http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/</link>
		<comments>http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 22:01:55 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Applescript]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Finder]]></category>
		<category><![CDATA[Folder Actions]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2008/01/30/simulating-encrypted-thumb-drive-in-os-x/</guid>
		<description><![CDATA[I&#8217;ve always wanted to do this: have my personal files on a memory stick, encrypted, and when I insert the memory stick in a USB port then OS X asks me for a password to decrypt the files. Unfortunately, this functionality isn&#8217;t offered directly as you can&#8217;t (at least from what I&#8217;ve tried and read) [...]]]></description>
			<content:encoded><![CDATA[I&#8217;ve always wanted to do this: have my personal files on a memory stick, encrypted, and when I insert the memory stick in a USB port then OS X asks me for a password to decrypt the files. Unfortunately, this functionality isn&#8217;t offered directly as you can&#8217;t (at least from what I&#8217;ve tried and read) encrypt a device directly. What you can do however is create encrypted disk images, and place them in your thumb drive. There are two disadvantages with this method: First the process isn&#8217;t automatic, first the drive mounts, then you have to double-click on the encrypted disk image to mount it, and second after you do that you have two volumes on your Desktop. One for the thumb drive, and one for the encrypted image.<br />
<br />
Obviously, in my obsessive compulsive need to have everything work as I want them, I didn&#8217;t like this. As a result, I created the following scripts (including a folder action) which pretty much simulate the whole experience. These scripts were only tested on 10.5.1, but they should work on at least Tiger.<br />
<span id="more-107"></span>
<br />
<span class="update">Update 2008-02-01:</span> <a href='http://www.cneophytou.com/wp-content/uploads/2008/02/secure_scripts.tgz' title='Applescripts bundle'>Download</a> all the scripts featured on this page in a single archive file.<br />
<br />
<span class="update">Update 2008-02-08:</span> The automount and prepare scripts in the download archive were reversed (my mistake) which is now fixed. I also added compatibility for sparsebundle types, and the password entry when preparing a drive is now hidden (thanks John!)<br />
<br />
There are two steps to the process. The first step is to prepare the memory stick, and the second is to configure the folder action. The first step only has to be applied once, and works best on a freshly formatted memory stick, however it doesn&#8217;t have to be formatted; it will use all the available space to create the encrypted image. Save the following script as an application bundle somewhere, plug in the memory stick you wish to use as your &#8216;encrypted storage&#8217;, and drag-drop the mounted volume on the bundle. It will ask you for a password (which is typed in the clear &#8211; fair warning), then after a few minutes (with the default settings) it will finish creating the encrypted image, mark the entire volume as hidden, and eject the drive.</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">(*
    Prepare secure drive droplet
    Part of 'Simulating encrypted physical disk in OS X' by Constantinos Neophytou
    http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/
&nbsp;
    Version 0.1.1
*)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">property</span> imageType : <span style="color: #009900;">&quot;UDIF&quot;</span> <span style="color: #808080; font-style: italic;">(* one of &quot;UDIF&quot;, &quot;SPARSE&quot; or &quot;SPARSEBUNDLE&quot; - the latter two have faster creation times, but leave empty space on the device *)</span>
<span style="color: #ff0033; font-weight: bold;">property</span> thePass : <span style="color: #009900;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #0066ff;">open</span> dropped_item
    <span style="color: #ff0033; font-weight: bold;">set</span> this_vol_alias <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>dropped_item <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
    <span style="color: #ff0033; font-weight: bold;">try</span>
        <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
            <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> kind <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Volume&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> freeSpace <span style="color: #ff0033; font-weight: bold;">to</span> free <span style="color: #0066ff;">space</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias
                <span style="color: #ff0033; font-weight: bold;">set</span> volumeName <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias
                <span style="color: #ff0033; font-weight: bold;">set</span> imgSize <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>round <span style="color: #000000;">&#40;</span>freeSpace <span style="color: #000000;">-</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">1024</span> <span style="color: #000000;">*</span> <span style="color: #000000;">1024</span> <span style="color: #000000;">*</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">/</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">1024</span> <span style="color: #000000;">*</span> <span style="color: #000000;">1024</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #808080; font-style: italic;">(* leave 1mb of free space, just in case *)</span>
&nbsp;
                <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Enter password for encrypted image:&quot;</span> <span style="color: #0066ff;">default answer</span> thePass <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #0066ff;">hidden</span> answer
                <span style="color: #ff0033; font-weight: bold;">set</span> thePass <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">text</span> returned <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">result</span> <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">string</span>
&nbsp;
                <span style="color: #ff0033; font-weight: bold;">set</span> createImg <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;hdiutil create &quot;</span> <span style="color: #000000;">&amp;</span> volumeName <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;.secure -size &quot;</span> <span style="color: #000000;">&amp;</span> imgSize <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;m -fs HFS+ -type &quot;</span> <span style="color: #000000;">&amp;</span> imageType <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; -encryption -volName Secure -passphrase &quot;</span> <span style="color: #000000;">&amp;</span> thePass
                <span style="color: #ff0033; font-weight: bold;">set</span> thePass <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #808080; font-style: italic;">(* clear the password from memory *)</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> hideVol <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;chflags hidden &quot;</span> <span style="color: #000000;">&amp;</span> volumeName
                <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #ff0033; font-weight: bold;">timeout</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">600</span> seconds
                    <span style="color: #0066ff;">do shell script</span> createImg
                    <span style="color: #0066ff;">do shell script</span> hideVol
                    <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;hdiutil detach &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">timeout</span>
            <span style="color: #ff0033; font-weight: bold;">else</span>
                display alert <span style="color: #009900;">&quot;This script can only be executed on volumes&quot;</span>
                <span style="color: #ff0033; font-weight: bold;">return</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
&nbsp;
    <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span> error_message <span style="color: #0066ff;">number</span> error_number
        <span style="color: #0066ff;">display dialog</span> error_message <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;OK&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #0066ff;">open</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2008/02/secure_scripts.tgz">Download all scripts</a><br />
<br />
At this point, the memory stick is configured for use with the folder action. Warning: If you remove the drive and plug it back in without the folder action being in place, the disk will mount but it won&#8217;t show up in the Finder. You will have to use Disk Utility or the Terminal to un-mount it. Go ahead and copy the following code, save it as a script under <code>/Library/Scripts/Folder Action Scripts/</code>, and attach it as a folder action to <code>/Volumes</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">(*
    Automount secure drive folder action
    Part of 'Simulating encrypted physical disk in OS X' by Constantinos Neophytou
    http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/
&nbsp;
    Version 0.1.1
*)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">property</span> secureName : <span style="color: #009900;">&quot;.secure&quot;</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span> this_folder <span style="color: #ff0033;">after</span> <span style="color: #0066ff;">receiving</span> these_volumes
    <span style="color: #ff0033; font-weight: bold;">try</span>
        <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
            <span style="color: #ff0033; font-weight: bold;">set</span> <span style="color: #ff0033;">the</span> volume_list <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">every</span> <span style="color: #0066ff;">file</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_folder
            <span style="color: #808080; font-style: italic;">(* go through all entries in /Volumes/ *)</span>
            <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> volume_list
                <span style="color: #ff0033; font-weight: bold;">set</span> this_item <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">the</span> original <span style="color: #0066ff;">item</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> volume_list
                <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> kind <span style="color: #ff0033; font-weight: bold;">of</span> this_item <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Volume&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> this_disk <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>this_item <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;">(* is this item the newly mounted disk? *)</span>
                    <span style="color: #ff0033; font-weight: bold;">if</span> this_disk <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033; font-weight: bold;">in</span> these_volumes <span style="color: #ff0033; font-weight: bold;">then</span>
                        <span style="color: #808080; font-style: italic;">(* iterate through all files in the root of disk *)</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> searchCmd <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;ls -d &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk <span style="color: #000000;">&amp;</span> secureName <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;*&quot;</span>
                        <span style="color: #808080; font-style: italic;">(* check to see if a hidden secure image  is available *)</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
                        <span style="color: #ff0033; font-weight: bold;">try</span>
                            <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> searchCmd
                        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
                        <span style="color: #808080; font-style: italic;">(* mount the secure image *)</span>
                        <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>searchResult starts <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #009900;">&quot;/Volumes&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                            <span style="color: #ff0033; font-weight: bold;">set</span> hiddenFile <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">POSIX file</span> searchResult <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span>
                            <span style="color: #0066ff;">open</span> hiddenFile
                        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> removing <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">from</span> this_folder <span style="color: #ff0033;">after</span> losing these_items
    <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
        <span style="color: #ff0033; font-weight: bold;">try</span>
            <span style="color: #ff0033; font-weight: bold;">set</span> <span style="color: #ff0033;">the</span> volume_list <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">every</span> <span style="color: #0066ff;">file</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_folder
            <span style="color: #808080; font-style: italic;">(* go through all entries in /Volumes/ *)</span>
            <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> volume_list
                <span style="color: #ff0033; font-weight: bold;">set</span> this_item <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">the</span> original <span style="color: #0066ff;">item</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> volume_list
                <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> kind <span style="color: #ff0033; font-weight: bold;">of</span> this_item <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Volume&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> this_disk <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>this_item <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
&nbsp;
                    <span style="color: #ff0033; font-weight: bold;">set</span> searchCmd <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;ls -d &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk <span style="color: #000000;">&amp;</span> secureName <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;*&quot;</span>
                    <span style="color: #808080; font-style: italic;">(* check to see if a hidden secure image  is available *)</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
                    <span style="color: #ff0033; font-weight: bold;">try</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> searchCmd
                    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
                    <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>searchResult starts <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #009900;">&quot;/Volumes&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                        <span style="color: #808080; font-style: italic;">(* eject the volume *)</span>
                        <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;hdiutil detach &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk
                        <span style="color: #808080; font-style: italic;">(* eject this_disk *)</span>
                        delay <span style="color: #000000;">1</span>
                        display alert <span style="color: #009900;">&quot;It is now safe to remove the encrypted disk&quot;</span>
                        <span style="color: #ff0033; font-weight: bold;">return</span>
                    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
<span style="color: #ff0033; font-weight: bold;">end</span> removing <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">from</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2008/02/secure_scripts.tgz">Download all scripts</a><br />
<br />
Now whenever you plug in the thumb drive, this script will kick in and try to open the encrypted image which will in turn make the Finder ask you for a password. Additionally, when you un-mount the encrypted image, it will find the thumb drive and un-mount it as well. You should wait for the confirmation dialog saying it&#8217;s safe to remove the device before doing so, as ejecting the encrypted image is not the same as ejecting the drive, the folder action has to kick in and do that for you.<br />
<br />
There are a couple of issues with this. The first one, I haven&#8217;t been able to find a decent solution for. If after you plug in the memory stick and you&#8217;re asked for a password for the encrypted device, you either enter the wrong password three times or press &#8216;Cancel&#8217;, then the encrypted image will not mount, but the memory stick will remain mounted. To eject it, you will have to use Disk Utility or the Terminal. The second one, is reverting the memory stick to its proper state. To do that, I created another script which you can just run by itself, after plugging in the memory stick and pressing &#8216;Cancel&#8217; when asked for the encrypted image password (i.e. it uses the &#8216;bad&#8217; state described in the previous problem). When run, the script will find the memory stick that contains the encrypted image and ask if you wish to revert it. If you press &#8216;Yes&#8217;, it will then ask if you wish to preserve the secure image. Pressing &#8216;Yes&#8217; will move the secure image on your Desktop (under the name <code>secure.dmg</code>), otherwise it will just delete it from the drive. It will also restore the hidden status of the thumb drive and eject it, so the next time you plug it in it will behave normally:</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">(*
    Restore secure drive
    Part of 'Simulating encrypted physical disk in OS X' by Constantinos Neophytou
    http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/
&nbsp;
    Version 0.1.1
*)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">property</span> secureName : <span style="color: #009900;">&quot;.secure&quot;</span>
<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
    <span style="color: #ff0033; font-weight: bold;">try</span>
        <span style="color: #ff0033; font-weight: bold;">set</span> <span style="color: #ff0033;">the</span> volume_list <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">every</span> <span style="color: #0066ff;">file</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">POSIX file</span> <span style="color: #009900;">&quot;/Volumes/&quot;</span> <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
        <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> volume_list
            <span style="color: #ff0033; font-weight: bold;">set</span> this_item <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">the</span> original <span style="color: #0066ff;">item</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> volume_list
            <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> kind <span style="color: #ff0033; font-weight: bold;">of</span> this_item <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Volume&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> this_disk <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>this_item <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
&nbsp;
                <span style="color: #ff0033; font-weight: bold;">set</span> searchCmd <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;ls -d &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk <span style="color: #000000;">&amp;</span> secureName <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;*&quot;</span>
                <span style="color: #808080; font-style: italic;">(* check to see if a hidden secure image  is available *)</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
                <span style="color: #ff0033; font-weight: bold;">try</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> searchCmd
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
                <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>searchResult starts <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #009900;">&quot;/Volumes&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                    <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Restore secure disk &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">name</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;?&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;No&quot;</span>, <span style="color: #009900;">&quot;Yes&quot;</span><span style="color: #000000;">&#93;</span> default button <span style="color: #000000;">2</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> the_button <span style="color: #ff0033; font-weight: bold;">to</span> button returned <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">result</span>
                    <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>the_button <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Yes&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                        <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Preserve copy of secure data on Desktop?&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#91;</span><span style="color: #009900;">&quot;No&quot;</span>, <span style="color: #009900;">&quot;Yes&quot;</span><span style="color: #000000;">&#93;</span> default button <span style="color: #000000;">2</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> the_button <span style="color: #ff0033; font-weight: bold;">to</span> button returned <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">result</span>
                        <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>the_button <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Yes&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                            <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #ff0033; font-weight: bold;">timeout</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">600</span> seconds
                                <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;mv &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> searchResult <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; ~/Desktop/secure.dmg&quot;</span>
                            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">timeout</span>
                        <span style="color: #ff0033; font-weight: bold;">else</span>
                            <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;rm -f &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> searchResult
                        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> volumeName <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk
                        <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;chflags nohidden &quot;</span> <span style="color: #000000;">&amp;</span> volumeName
                        <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;hdiutil detach &quot;</span> <span style="color: #000000;">&amp;</span> volumeName
                        delay <span style="color: #000000;">1</span>
                        display alert <span style="color: #009900;">&quot;Device restored. It is now safe to unplug the device.&quot;</span>
                        <span style="color: #ff0033; font-weight: bold;">return</span>
                    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
&nbsp;
    <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span> error_message <span style="color: #0066ff;">number</span> error_number
        <span style="color: #0066ff;">display dialog</span> error_message <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;OK&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2008/02/secure_scripts.tgz">Download all scripts</a><br />
<br />
As always, these scripts are provided without any warranty, and any suggested improvements are more than welcome!]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Automatically restart crashed applications</title>
		<link>http://www.cneophytou.com/2007/10/14/automatically-restart-crashed-applications/</link>
		<comments>http://www.cneophytou.com/2007/10/14/automatically-restart-crashed-applications/#comments</comments>
		<pubDate>Sun, 14 Oct 2007 21:31:07 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Applescript]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[Folder Actions]]></category>
		<category><![CDATA[Quicksilver]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2007/10/15/automatically-restart-crashed-applications/</guid>
		<description><![CDATA[I love Quicksilver. If I ever sit on a machine without it, I feel handicapped, like something extremely important is missing. My major gripe with Quicksilver (and, this might just be my setup, I&#8217;m not sure), is that it likes to crash a lot. At least 2-3 times a week. And when it crashes, it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[I love Quicksilver. If I ever sit on a machine without it, I feel handicapped, like something extremely important is missing. My major gripe with Quicksilver (and, this might just be my setup, I&#8217;m not sure), is that it likes to crash a lot. At least 2-3 times a week. And when it crashes, it&#8217;s extremely annoying because when I need to start a program up I use, well, Quicksilver!<br />
<br />
<span class='update'>Updated 2008-01-11:</span> Modified script for Leopard. New script at end of post.<br />
<br />
<span id="more-103"></span>
I&#8217;ve found a couple of different methods for relaunching Quicksilver easily or automatically. One was to keep an alias to Quicksilver on the bottom-left part of your desktop with no icon, and named as &#8221; &#8221; (i.e. a space). So when Quicksilver is off and you want to start it, you cmd-tab to the Finder, hit space, and cmd-o. Quick, easy, does the job, but hardly clean (or nerdy!).<br />
<br />
The other approach which I loved was playing around with launchd (found on <a href='http://www.macworld.com/weblogs/macosxhints/2007/04/autorestart/index.php'>MacWorld</a> and <a href='http://www.macosxhints.com/article.php?story=20060927132601570'>macosxhints.com</a>). With this service we can monitor a file for modifications, and when the file is modified we can perform an action. In this case, we would monitor the file <code>/Users/username/Library/Logs/CrashReporter/Quicksilver.crash.log</code>, and once its modified, launch Quicksilver. Clean, fully automated, but hardly simple. To make the task easier, <a href='http://lingon.sourceforge.net/'>Lingon</a> can be used to deal with launchd. However, I was never able to make this to work, and I don&#8217;t have the patience to sit down and figure out what the problem is.<br />
<br />
I did like the idea of monitoring Quicksilver.crash.log since that&#8217;s a direct indication that the application has crashed, but other than launchd I&#8217;m not familiar with any other automated service that can monitor for modifications in a file. I am however familiar with an automated service that monitors for modifications in a folder, courtesy of OS X, and that&#8217;s folder actions.<br />
<br />
I can therefore build a folder action which will monitor  <code>/Users/username/Library/Logs/CrashReporter/</code> for changes, see what files were added, and if a &#8216;marked&#8217; file was added then relaunch the relevant application and (this part is critical) delete the crash log so the script can work again. It took a couple of tries, but I think I found the most robust solution for this approach.<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> restartLog : <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;Quicksilver.crash.log&quot;</span><span style="color: #000000;">&#125;</span>
<span style="color: #ff0033; font-weight: bold;">property</span> restartID : <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;daed&quot;</span><span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span> this_folder <span style="color: #ff0033;">after</span> <span style="color: #0066ff;">receiving</span> these_items
    <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> these_items
        <span style="color: #ff0033; font-weight: bold;">set</span> fileAlias <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> these_items <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span>
        <span style="color: #ff0033; font-weight: bold;">set</span> fileInfo <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">info for</span> fileAlias
        <span style="color: #ff0033; font-weight: bold;">set</span> fileName <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">name</span> <span style="color: #ff0033; font-weight: bold;">of</span> fileInfo <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">text</span>
        <span style="color: #ff0033; font-weight: bold;">if</span> fileName <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033; font-weight: bold;">in</span> restartLog <span style="color: #ff0033; font-weight: bold;">then</span>
            <span style="color: #0066ff;">beep</span>
            <span style="color: #ff0033; font-weight: bold;">set</span> itm <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033; font-weight: bold;">my</span> list_position<span style="color: #000000;">&#40;</span>fileName, restartLog<span style="color: #000000;">&#41;</span>
            <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
                <span style="color: #0066ff;">open</span> <span style="color: #0066ff;">application</span> <span style="color: #0066ff;">file</span> <span style="color: #0066ff;">id</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">item</span> itm <span style="color: #ff0033; font-weight: bold;">of</span> restartID<span style="color: #000000;">&#41;</span>
                <span style="color: #0066ff;">delete</span> <span style="color: #0066ff;">item</span> fileAlias
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> list_position<span style="color: #000000;">&#40;</span>this_item, this_list<span style="color: #000000;">&#41;</span>
    <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">the</span> <span style="color: #0066ff;">count</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_list
        <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> this_list <span style="color: #ff0033; font-weight: bold;">is</span> this_item <span style="color: #ff0033; font-weight: bold;">then</span> <span style="color: #ff0033; font-weight: bold;">return</span> i
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
    <span style="color: #ff0033; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #ff0033; font-weight: bold;">end</span> list_position</pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2007/10/restartcrashedapp.scpt">Download this script</a><br />
<br />
Note that to get this to work initially, you&#8217;ll have to open that folder and delete the existing Quicksilver.crash.log, otherwise this folder action will never kick in for Quicksilver!<br />
<br />
The critical part are the two properties at the top. The first property contains the name of the file to watch for, and the second property contains (in the same order as in the first property) the application file id for the related application. The reason for using the application file id is that this will remain constant on every system even if the application is renamed by the user.<br />
<br />
This script can be expanded to listen for any number of applications simply by adding the two parameters for each application to these properties. To get the application file id of the application you wish to monitor, simply run this code once in Script Editor while the target application is running:<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033; font-weight: bold;">return</span> creator type <span style="color: #ff0033; font-weight: bold;">of</span> process <span style="color: #009900;">&quot;Process Name&quot;</span></pre></div></div>



<p>
and watch the output.<br />
<br />
As a side note, with this method you can chose to do any number of things instead of just relaunching the crashed application since it&#8217;s straight forward applescript. In my version I chose to &#8216;beep&#8217; as well, to let me know something went wrong (and the file trash sound is also a giveaway), but you really could do anything you want here.<br />
<br />
<span class='update'>Leopard Update:</span><br />
<br />
There have been a few changes in the way Leopard handles the crash log files, which simplifies the use of this script. Instead of using a single crash log file for each application and appending to it, Leopard simply creates a new timestamped crash log in the form <code>Name_YYYY-MM-DD-TIME_Hostname.crash</code>. This is easier, because we no longer have to worry about deleting the crash log, we can simply check if the newly added log starts with *Name*. Therefore, a simpler version of the above applescript (that can only work with a single app, but switching to lists shouldn&#8217;t be too hard), follows:<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> restartName : <span style="color: #009900;">&quot;Quicksilver&quot;</span>
<span style="color: #ff0033; font-weight: bold;">property</span> restartID : <span style="color: #009900;">&quot;daed&quot;</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span> this_folder <span style="color: #ff0033;">after</span> <span style="color: #0066ff;">receiving</span> these_items
    <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> these_items
        <span style="color: #ff0033; font-weight: bold;">set</span> fileAlias <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> these_items <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span>
        <span style="color: #ff0033; font-weight: bold;">set</span> fileInfo <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">info for</span> fileAlias
        <span style="color: #ff0033; font-weight: bold;">set</span> fileName <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">name</span> <span style="color: #ff0033; font-weight: bold;">of</span> fileInfo <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">text</span>
        <span style="color: #ff0033; font-weight: bold;">if</span> fileName starts <span style="color: #ff0033; font-weight: bold;">with</span> restartName <span style="color: #ff0033; font-weight: bold;">then</span>
            <span style="color: #0066ff;">beep</span>
            <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
                <span style="color: #0066ff;">open</span> <span style="color: #0066ff;">application</span> <span style="color: #0066ff;">file</span> <span style="color: #0066ff;">id</span> restartID
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2007/10/restartcrashedapp_10.5.scpt">Download this script</a><br />
<br />
If anyone can figure out how to use applescript instead of the shell commands and still have it work, please let me know.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2007/10/14/automatically-restart-crashed-applications/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>LiveSearch 1.3</title>
		<link>http://www.cneophytou.com/2007/03/27/livesearch-13/</link>
		<comments>http://www.cneophytou.com/2007/03/27/livesearch-13/#comments</comments>
		<pubDate>Tue, 27 Mar 2007 00:01:43 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WP Plugin]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[LiveSearch]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2007/03/27/livesearch-13/</guid>
		<description><![CDATA[I&#8217;ve finished work on LiveSearch 1.3 some time ago, just never got around to creating a tarball and uploading it to this site. The major change was that it no longer requires that the searchform.php and livesearch.css files be manually copied to your themes directory. If you want to make theme-specific changes to it you [...]]]></description>
			<content:encoded><![CDATA[I&#8217;ve finished work on LiveSearch 1.3 some time ago, just never got around to creating a tarball and uploading it to this site. The major change was that it no longer requires that the searchform.php and livesearch.css files be manually copied to your themes directory. If you want to make theme-specific changes to it you can still have a copy of those files in your theme directory, and if they&#8217;re found there they will be used instead of the default ones. Plus a few bug fixes and javascript improvements here and there.<br />
<br />
If you&#8217;re interested, I created a <a href='http://code.google.com/p/livesearchwpplugin/'>Google Code</a> project for this and all the files are stored there.<br />
<br />
As before, here are quicklinks to the <a href='http://www.cneophytou.com/wp-content/uploads/2007/03/livesearch.zip' title='LiveSearch 1.3 zip'>zip archive</a> and the <a href='http://www.cneophytou.com/wp-content/uploads/2007/03/livesearch.tgz' title='LiveSearch 1.3 tgz'>gzipped tarball</a>, as well as the main <a href='http://www.cneophytou.com/2006/03/26/livesearch/'>LiveSearch</a> post.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2007/03/27/livesearch-13/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<series:name><![CDATA[LiveSearch]]></series:name>
	</item>
		<item>
		<title>Pretty CAPTCHA</title>
		<link>http://www.cneophytou.com/2007/03/06/pretty-captcha/</link>
		<comments>http://www.cneophytou.com/2007/03/06/pretty-captcha/#comments</comments>
		<pubDate>Mon, 05 Mar 2007 22:32:59 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[CAPTCHA]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2007/03/06/pretty-captcha/</guid>
		<description><![CDATA[I know, I know&#8230; CAPTCHA is outdated. But there are still cases where it might be useful, and it&#8217;s hard to make one that&#8217;s good looking, easy to read by humans and has a certain level of complexity for machines. This is what led me to create my own PHP version, that can be pretty [...]]]></description>
			<content:encoded><![CDATA[I know, I know&#8230; CAPTCHA is outdated. But there are still cases where it might be useful, and it&#8217;s hard to make one that&#8217;s good looking, easy to read by humans and has a certain level of complexity for machines. This is what led me to create my own PHP version, that can be pretty much plugged in to anything. I was meaning to make this into a WordPress plugin, but meh&#8230;<br />
<span id="more-87"></span>
<br />
This script uses the $_SESSION variable to store the generated pass string. Therefore, to validate the CAPTCHA, you need this little code inserted in the page that handles the form post:
<br />
<br />
<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$captcha</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'captcha'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$captcha</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'captcha'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #000088;">$captcha</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Verification failed... Do what you will.</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'captcha'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
That&#8217;s it on that end. If you need help with that, and I have time, I might offer some assistance. Did not really check on how that could be implemented for WordPress, and therefore don&#8217;t (yet) know if it&#8217;s easy or not.<br />
<br />
Now on to the main course. The script is at the end of this post. To use it, simply save it as <code>captcha.php</code>, place <a id="p86" href="http://www.cneophytou.com/wp-content/uploads/2007/03/captchattf.zip" title="CAPTCHA TTF file">this font file</a> (or any other ttf font you want) in the same directory with it, and then place this image link anywhere in your code:</p>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;span&gt;&lt;img src='captcha.php' alt='Captcha image' id='captcha_img' /&gt;&lt;/span&gt;</pre></div></div>



<p>
That&#8217;s it. A nice looking CAPTCHA for your site <img src='http://www.cneophytou.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> <br />
<br />
If you want to add a link to refresh the image in-place, you can use this javascript (doesn&#8217;t work in Safari):</p>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href='#' onclick=&quot;var i = document.getElementById('captcha_img'); var n = new Image(); var rand = (Math.round((Math.random()*1000)+1)); n.onload = function() { i.parentNode.appendChild(n); i.parentNode.removeChild(i); n.id='captcha_img'; }; n.src='captcha.php?'+rand; return false;&quot;&gt;reload image&lt;/a&gt;</pre></div></div>



<p>
This piece of code will reload the image and reset the stored pass word.<br />
<br />
A preview of the image produced is after the jump.<br />
<br />
The CAPTCHA code, released under <a href='http://creativecommons.org/licenses/by-nc-sa/3.0/'>Creative Commons Attribution-Noncommercial-Share Alike 3.0</a> License</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Generate a CAPTCHA and display the image
 *
 * @author Constantinos Neophytou
 * @link http://www.cneophytou.com/
 * @license Creative Commons Attribution-Noncommercial-Share Alike 3.0  License
 *     {@link http://creativecommons.org/licenses/by-nc-sa/3.0/}
 **/</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Initialize page
 **/</span>
&nbsp;
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: image/png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: no-cache, must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// HTTP/1.1</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Date in the past</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Last-Modified: &quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">gmdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; GMT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
create_image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Generate a random string, and create a CAPTCHA image out of it
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> create_image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// generate pronouncable pass</span>
    <span style="color: #000088;">$pass</span> <span style="color: #339933;">=</span> genPassword<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$font</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'./captcha.ttf'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$maxsize</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sizeVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">25</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$rotate</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$bgcol</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">50</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// + 50</span>
    <span style="color: #000088;">$bgtextcol</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// + 50</span>
    <span style="color: #000088;">$textcol</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">205</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// + 50</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// remember the pass</span>
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;captcha&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$pass</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// calculate dimentions required for pass</span>
    <span style="color: #000088;">$box</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">imageTTFBbox</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$maxsize</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$font</span><span style="color: #339933;">,</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$minwidth</span> <span style="color: #339933;">=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$box</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$box</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$minheight</span> <span style="color: #339933;">=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$box</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$box</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// allow spacing for rotating letters</span>
    <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$minwidth</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$height</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$minheight</span> <span style="color: #339933;">+</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// give some air for the letters to breathe</span>
    <span style="color: #666666; font-style: italic;">// create initial image</span>
    <span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageCreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'imageantialias'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">imageantialias</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// define background color - never the same, close to black</span>
    <span style="color: #000088;">$clr_black</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageColorAllocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bgcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                    <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bgcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bgcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">imagefill</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clr_black</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// calculate starting positions for letters</span>
    <span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//($width / 2) - ($minwidth / 2);</span>
    <span style="color: #000088;">$xinit</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$minheight</span><span style="color: #339933;">-</span><span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$box</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$minheight</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// fill the background with big letters, colored a bit lightly, to vary the bg.</span>
    <span style="color: #000088;">$bgx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$maxsize</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxsize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// modify color a bit</span>
        <span style="color: #000088;">$clr_white</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageColorAllocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bgtextcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgtextcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                    <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bgtextcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgtextcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$bgtextcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgtextcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$angle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #000088;">$rotate</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rotate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$letter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$angle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$bgx</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clr_white</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$letter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">imageTTFBbox</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">,</span><span style="color: #000088;">$angle</span><span style="color: #339933;">,</span><span style="color: #000088;">$font</span><span style="color: #339933;">,</span><span style="color: #000088;">$letter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$bgx</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$x1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// for each letter, decide a color, decide a rotation, put it on the image,</span>
    <span style="color: #666666; font-style: italic;">//     and figure out width to place next letter correctly</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// modify color a bit</span>
        <span style="color: #000088;">$clr_white</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageColorAllocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$textcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$textcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
                            <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$textcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$textcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$textcol</span><span style="color: #339933;">,</span> <span style="color: #000088;">$textcol</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$angle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">-</span><span style="color: #000088;">$rotate</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rotate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$letter</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pass</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$size</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$maxsize</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$sizeVar</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxsize</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$tempbox</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">imageTTFBbox</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$size</span><span style="color: #339933;">,</span><span style="color: #000088;">$angle</span><span style="color: #339933;">,</span><span style="color: #000088;">$font</span><span style="color: #339933;">,</span><span style="color: #000088;">$letter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$y</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tempbox</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$tempbox</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
                                        <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height</span> <span style="color: #339933;">-</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tempbox</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$tempbox</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #990000;">imagettftext</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$size</span><span style="color: #339933;">,</span> <span style="color: #000088;">$angle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clr_white</span><span style="color: #339933;">,</span> <span style="color: #000088;">$font</span><span style="color: #339933;">,</span> <span style="color: #000088;">$letter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$x</span> <span style="color: #339933;">+=</span> <span style="color: #990000;">abs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tempbox</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #000088;">$tempbox</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// figure out final width (same space at the end as there was at the beginning)</span>
    <span style="color: #000088;">$width</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xinit</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$x</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// throw in some lines</span>
    <span style="color: #000088;">$clr_white</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageColorAllocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">160</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">160</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                             <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">160</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    imagelinethick<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                           <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clr_white</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$clr_white</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageColorAllocate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">160</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">160</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                             <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">160</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    imagelinethick<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                            <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                           <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$height</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$clr_white</span><span style="color: #339933;">,</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// generate final image by cropping initial image to the proper width,</span>
    <span style="color: #666666; font-style: italic;">//     which we didn't know till now.</span>
    <span style="color: #000088;">$finalimage</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ImageCreatetruecolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'imageantialias'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">imageantialias</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finalimage</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #990000;">imagecopy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finalimage</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$width</span><span style="color: #339933;">,</span> <span style="color: #000088;">$height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// clear some memory</span>
    <span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// dump image</span>
    <span style="color: #990000;">imagepng</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finalimage</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// clear some more memory</span>
    <span style="color: #990000;">imagedestroy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$finalimage</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Draw lines through an image
 * @param resource
 * @param int
 * @param int
 * @param int
 * @param int
 * @param int - the color to use for the line
 * @param int - line thickness
 * @return bool - true on success
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> imagelinethick<span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #339933;">,</span> <span style="color: #000088;">$thick</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>  <span style="color: #009900;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$thick</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #990000;">imageline</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000088;">$t</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$thick</span> <span style="color: #339933;">/</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">-</span> <span style="color:#800080;">0.5</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$x2</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$y1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$y2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
       <span style="color: #b1b100;">return</span> <span style="color: #990000;">imagefilledrectangle</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                           <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">min</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                           <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                           <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$t</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                   <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$y2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$y1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x2</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$x1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//y = kx + q</span>
   <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$t</span> <span style="color: #339933;">/</span> <span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">+</span> <span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000088;">$points</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
       <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x1</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y1</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
       <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x1</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y1</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
       <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x2</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y2</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
       <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$x2</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">-</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$y2</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
   <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
   <span style="color: #990000;">imagefilledpolygon</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$points</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #990000;">imagepolygon</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #000088;">$points</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #000088;">$color</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Generate a random, pronouncable password
 * Modified to exclude letters which don't show up well in the CAPTCHA
 * @link http://www.zend.com/code/codex.php?id=215&amp;single=1
 * @author Rival7 {@link http://www.zend.com/code/search_code_author.php?author=Rival7}
 * @author Constantinos Neophytou
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> genPassword<span style="color: #009900;">&#40;</span><span style="color: #000088;">$minlen</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxlen</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">srand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>double<span style="color: #009900;">&#41;</span><span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'e'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'i'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'o'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'u'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cons</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'g'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'h'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'j'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'k'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'m'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'n'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'p'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'s'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'t'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v'</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'w'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'tr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'br'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'th'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'dr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ch'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ph'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'st'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sp'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sw'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pr'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// removed 'l', 'sl', 'cl'</span>
&nbsp;
    <span style="color: #000088;">$num_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vowels</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$num_cons</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cons</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$minlen</span><span style="color: #339933;">,</span> <span style="color: #000088;">$maxlen</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$start</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cons</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$num_first</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num_cons</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$second</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vowels</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$num_second</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num_vowels</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$first</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$vowels</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$num_first</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num_vowels</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$second</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cons</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$num_second</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$num_cons</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$add</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$first</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_first</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$second</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num_second</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$add</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$i</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$add</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//$password = substr($password, 0, $length);</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2007/03/captcha.php.tgz">Download this script</a><br />
<br />
The produced image:<br />(if you know any coding whatsoever, you can easily change the thickness of the lines that run through the image)<br /><br />
<span><img src='/wp-content/uploads/scripts/captcha.php' alt='Captcha image' id='captcha_img' /></span><br />
<a href='#' onclick="var i = document.getElementById('captcha_img'); var n = new Image(); var rand = (Math.round((Math.random()*1000)+1)); n.onload = function() { i.parentNode.appendChild(n); i.parentNode.removeChild(i); n.id='captcha_img'; }; n.src='/wp-content/uploads/scripts/captcha.php?'+rand; return false;">reload image</a>]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2007/03/06/pretty-captcha/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Automating backup of Flash drives</title>
		<link>http://www.cneophytou.com/2007/03/06/automating-backup-of-flash-drives/</link>
		<comments>http://www.cneophytou.com/2007/03/06/automating-backup-of-flash-drives/#comments</comments>
		<pubDate>Mon, 05 Mar 2007 21:32:26 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Applescript]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[Folder Actions]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2007/03/06/automating-backup-of-flash-drives/</guid>
		<description><![CDATA[I have a lot of my critical work on a few flash drives which I carry around with me constantly, mainly because I work on a number of machines and would like to have a central repository of my work. After losing one of the drives recently though (bound to happen sooner or later), I [...]]]></description>
			<content:encoded><![CDATA[I have a lot of my critical work on a few flash drives which I carry around with me constantly, mainly because I work on a number of machines and would like to have a central repository of my work. After losing one of the drives recently though (bound to happen sooner or later), I looked further into automating the backup process (I had lost about 2 days of work). That&#8217;s when I stumbled accross <a href='http://www.macosxhints.com/article.php?story=2007022112171245'>this hint</a> on <a href='http://www.macosxhints.com/'>macosxhints.com</a>, but the script didn&#8217;t exactly fulfil what I had imagined my automated process to do.<br />
<br />
Ideally, the automated backup process would have these features:</p>
<ol>
<li>Not require installation of any third party app</li>
<li>Have the ability to somewhat customize the backup script for each thumb drive</li>
<li>Have the backup script silently run whenever the thumb drive is mounted</li>
<li>Require little to no set up for each new thumb drive that I want to backup</li>
<li>Support spaces in Volume names</li>
<li>Not try to backup the boot drive under any circumstances (don&#8217;t ask&#8230;)</li>
</ol>
<p><span class='update'>Updated 2007-03-10:</span> I updated the backup script to include automated handling of archives, and keeps an arbitrary number (specified in the preferences of the script) of incremental archives using rsync's <code>--link-dest</code> parameter, as recommended by John in the comments. This creates what functionally amounts to FULL backups, but with a fraction of the space needed for an actual <code>tar</code> backup. Also it can be configured to keep at most one backup per day, or with minor editing at most one backup for any period of time.</p>
<p><span class='update'>Updated 2007-03-18:</span> I've created a second script which can be used as a drag-n-drop solution. Simply save the script as an application bundle, save it somewhere handy, and drag it to your dock. Now you can drag your thumb drive on the script which will perform the backup, and then eject your drive!</p>
<p><span class='update'>Updated 2008-03-10:</span> Folder action modified to work with Leopard. Well, more like hacked at this point, but it seems to work.</p>
<p>
<span id="more-85"></span>
<br />
Now to satisfy the first parameter, using <a href='http://www.azarhi.com/Projects/DSW/index.php'>Do Something When</a> was out of the question, even though it seems like a good piece of software. That&#8217;s when I turned to Fodler Actions, and found <a href='http://www.macosxhints.com/article.php?story=2007011207351342'>this hint</a> again on macosxhints.com. I then proceeded to read the <a href='http://www.apple.com/applescript/folderactions/05.html'>Apple-published Folder Action</a> for automating backups (which, again, didn&#8217;t do quite what I wanted), so I came up with this:<br />
<br />
A folder action that scans the root folder of the mounted volume, and looks for (one of) a specified file name.</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> backup_script : <span style="color: #009900;">&quot;rsync.app&quot;</span> <span style="color: #808080; font-style: italic;">(* add/change this string to match the name of your script *)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span> this_folder <span style="color: #ff0033;">after</span> <span style="color: #0066ff;">receiving</span> these_volumes
    <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
        <span style="color: #ff0033; font-weight: bold;">try</span>
            <span style="color: #ff0033; font-weight: bold;">set</span> <span style="color: #ff0033;">the</span> volume_list <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">every</span> <span style="color: #0066ff;">file</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_folder
&nbsp;
            <span style="color: #808080; font-style: italic;">(* go through all entries in /Volumes/ *)</span>
            <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> volume_list
                <span style="color: #ff0033; font-weight: bold;">set</span> this_item <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">the</span> original <span style="color: #0066ff;">item</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> volume_list
                <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> kind <span style="color: #ff0033; font-weight: bold;">of</span> this_item <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Volume&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> this_disk <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>this_item <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;">(* is this item the newly mounted disk? *)</span>
                    <span style="color: #ff0033; font-weight: bold;">if</span> this_disk <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033; font-weight: bold;">in</span> these_volumes <span style="color: #ff0033; font-weight: bold;">then</span>
&nbsp;
                        <span style="color: #808080; font-style: italic;">(* iterate through all files in the root of disk *)</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> searchCmd <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;ls -d &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_disk <span style="color: #000000;">&amp;</span> backup_script
                        <span style="color: #808080; font-style: italic;">(* check to see if a backup script is available *)</span>
                        <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
                        <span style="color: #ff0033; font-weight: bold;">try</span>
                            <span style="color: #ff0033; font-weight: bold;">set</span> searchResult <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> searchCmd
                        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
                        <span style="color: #808080; font-style: italic;">(* run the backup script *)</span>
                        <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>searchResult starts <span style="color: #ff0033; font-weight: bold;">with</span> <span style="color: #009900;">&quot;/Volumes&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                            <span style="color: #ff0033; font-weight: bold;">set</span> backupFile <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">POSIX file</span> searchResult <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span>
                            <span style="color: #0066ff;">open</span> backupFile
                        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
                    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
        <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span> error_message <span style="color: #0066ff;">number</span> error_number
            <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> error_number <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">not</span> <span style="color: #000000;">-</span><span style="color: #000000;">128</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                <span style="color: #0066ff;">display dialog</span> error_message <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;OK&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #0066ff;">adding</span> <span style="color: #0066ff;">folder</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">to</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2007/03/auto_backup.scpt">Download this script</a><br />
<br />
If you already know how to apply folder actions, just save the script above and apply it as a folder action to the <code>/Volumes/</code> folder. If you don&#8217;t know (or don&#8217;t remember) how to do that, re-iterating the instructions from <a href='http://www.apple.com/applescript/folderactions/05.html'>this Apple page</a>, follow these steps to do it:</p>
<ol>
<li>Copy the above code in Script Editor</li>
<li>Save it as a script in <code>/Library/Scripts/Folder Action Scripts/</code>, and give it a descriptive name</li>
<li>In Finder, click <strong>Go</strong> -> <strong>Go to Folder</strong> (or just press cmd-shift-G), and type <code>/Volumes/</code>.</li>
<li>Launch the <code>Folder Actions Setup</code> utility (probably located in <code>/Applications/Apple Script/</code> folder)</li>
<li>Click the Add Folder Action (round plus) button at the bottom left</li>
<li>Drag the small folder icon from the Finder window title bar into the Choose Folder sheet dialog, and click ok.</li>
<li>From the dialog that comes up, chose the script you just saved.</li>
<li>You&#8217;re done.</li>
</ol>
<p>
Alternatively (or additionally), you can save this script as an application bundle, save it somewhere handy, and drag it to your dock. When you drag a thumb drive on this application the backup process will start, and as soon as it's done the drive will eject.
</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> backup_script : <span style="color: #009900;">&quot;rsync&quot;</span> <span style="color: #808080; font-style: italic;">(* add/change this to match the name of your script *)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #0066ff;">open</span> dropped_item
    <span style="color: #ff0033; font-weight: bold;">set</span> this_vol_alias <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>dropped_item <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">alias</span><span style="color: #000000;">&#41;</span>
    <span style="color: #ff0033; font-weight: bold;">try</span>
        <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
            <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> kind <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;Volume&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> <span style="color: #ff0033;">the</span> file_list <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #ff0033;">every</span> <span style="color: #0066ff;">file</span> <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias
                <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> j <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> file_list
                    <span style="color: #808080; font-style: italic;">(* check to see if a backup script is available *)</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> this_file <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">name</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> j <span style="color: #ff0033; font-weight: bold;">of</span> file_list
                    <span style="color: #ff0033; font-weight: bold;">if</span> this_file <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span>backup_script <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;.app&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                        <span style="color: #0066ff;">open</span> <span style="color: #0066ff;">item</span> j <span style="color: #ff0033; font-weight: bold;">of</span> file_list <span style="color: #808080; font-style: italic;">(* run the backup script *)</span>
                    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
            <span style="color: #ff0033; font-weight: bold;">else</span>
                display alert <span style="color: #009900;">&quot;This script can only be executed on volumes&quot;</span>
                <span style="color: #ff0033; font-weight: bold;">return</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
&nbsp;
        delay <span style="color: #000000;">2</span> <span style="color: #808080; font-style: italic;">(* give some time for the backup script to actually launch *)</span>
        <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
            <span style="color: #ff0033; font-weight: bold;">try</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> bakapp <span style="color: #ff0033; font-weight: bold;">to</span> creator type <span style="color: #ff0033; font-weight: bold;">of</span> process backup_script
                <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">while</span> bakapp <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">not</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
                    delay <span style="color: #000000;">1</span> <span style="color: #808080; font-style: italic;">(* the backup is still running, we can check again in a second *)</span>
                    <span style="color: #ff0033; font-weight: bold;">set</span> bakapp <span style="color: #ff0033; font-weight: bold;">to</span> creator type <span style="color: #ff0033; font-weight: bold;">of</span> process backup_script
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
            <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span>
                <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>ejectable <span style="color: #ff0033; font-weight: bold;">of</span> this_vol_alias <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #0066ff;">true</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                    <span style="color: #808080; font-style: italic;">(* display alert POSIX path of dropped_item *)</span>
                    <span style="color: #0066ff;">eject</span> this_vol_alias
                <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
&nbsp;
    <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span> error_message <span style="color: #0066ff;">number</span> error_number
        <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">the</span> error_number <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">not</span> <span style="color: #000000;">-</span><span style="color: #000000;">128</span> <span style="color: #ff0033; font-weight: bold;">then</span>
            <span style="color: #0066ff;">display dialog</span> error_message <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span><span style="color: #009900;">&quot;OK&quot;</span><span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">1</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #0066ff;">open</span></pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2007/03/auto_backup.zip">Download this script</a><br />
<br />
Now, for the modified backup script. The properties at the top can be modified on a per copy basis to customize the function of the script.</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> archive_backup : <span style="color: #0066ff;">true</span>
<span style="color: #ff0033; font-weight: bold;">property</span> number_of_archives : <span style="color: #000000;">3</span> <span style="color: #808080; font-style: italic;">(* 0 for unlimited *)</span>
<span style="color: #ff0033; font-weight: bold;">property</span> with_administrator_privileges : <span style="color: #0066ff;">false</span>
<span style="color: #ff0033; font-weight: bold;">property</span> one_archive_per_day : <span style="color: #0066ff;">true</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">property</span> display_notification : <span style="color: #0066ff;">false</span>
<span style="color: #ff0033; font-weight: bold;">property</span> backup_target : <span style="color: #009900;">&quot;Backups/&quot;</span> <span style="color: #808080; font-style: italic;">(* MUST be a folder location with trailing slash! Always relative to home folder *)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">property</span> rsync_params : <span style="color: #009900;">&quot;-aEz --delete-excluded&quot;</span>
<span style="color: #808080; font-style: italic;">(* END OF PREFERENCES *)</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">set</span> WhereImRunningFrom <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">path to</span> <span style="color: #0066ff;">me</span>
<span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
    <span style="color: #808080; font-style: italic;">(* can't run from the script editor *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> AppCreator <span style="color: #ff0033; font-weight: bold;">to</span> creator type <span style="color: #ff0033; font-weight: bold;">of</span> WhereImRunningFrom
    <span style="color: #ff0033; font-weight: bold;">if</span> AppCreator <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #009900;">&quot;ToyS&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
        <span style="color: #0066ff;">activate</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">me</span>
        <span style="color: #0066ff;">beep</span>
        display alert <span style="color: #009900;">&quot;This script cannot run directly from ScriptEditor&quot;</span>
        <span style="color: #ff0033; font-weight: bold;">return</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">(* NEVER run from the hard drive! *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> bootVolume <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">name</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">disk</span> <span style="color: #ff0033; font-weight: bold;">of</span> home <span style="color: #808080; font-style: italic;">(* safety feature! *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> NameOfDisk <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">name</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">disk</span> <span style="color: #ff0033; font-weight: bold;">of</span> WhereImRunningFrom
    <span style="color: #ff0033; font-weight: bold;">if</span> NameOfDisk <span style="color: #ff0033; font-weight: bold;">is</span> bootVolume <span style="color: #ff0033; font-weight: bold;">then</span>
        <span style="color: #0066ff;">beep</span>
        display alert <span style="color: #009900;">&quot;Should not run this script from the boot volume!&quot;</span>
        <span style="color: #ff0033; font-weight: bold;">return</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">(* setup backup dir *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> homeDir <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">path to</span> home <span style="color: #0066ff;">folder</span> <span style="color: #ff0033; font-weight: bold;">from</span> user domain
    <span style="color: #ff0033; font-weight: bold;">set</span> backup_folder <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> homeDir <span style="color: #000000;">&amp;</span> backup_target
    <span style="color: #ff0033; font-weight: bold;">try</span>
        <span style="color: #808080; font-style: italic;">(* quick and dirty check to see if folder exists - must be a good way to do this in applescript? *)</span>
        <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;cd &quot;</span> <span style="color: #000000;">&amp;</span> backup_folder
    <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span>
        <span style="color: #ff0033; font-weight: bold;">try</span>
            <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;mkdir &quot;</span> <span style="color: #000000;">&amp;</span> backup_folder
        <span style="color: #ff0033; font-weight: bold;">on</span> <span style="color: #ff0033; font-weight: bold;">error</span>
            display alert <span style="color: #009900;">&quot;There was an error creating the backup folder &quot;</span> <span style="color: #000000;">&amp;</span> backup_folder
            <span style="color: #ff0033; font-weight: bold;">return</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">try</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">(* find source and target *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> backupBase <span style="color: #ff0033; font-weight: bold;">to</span> NameOfDisk <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;-Backup&quot;</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> targetDir <span style="color: #ff0033; font-weight: bold;">to</span> backup_folder <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> backupBase
    <span style="color: #ff0033; font-weight: bold;">set</span> sourceDir <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">&#40;</span><span style="color: #009900;">&quot;/Volumes/&quot;</span> <span style="color: #000000;">&amp;</span> NameOfDisk <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">(* set up archive filename *)</span>
    <span style="color: #ff0033; font-weight: bold;">if</span> archive_backup <span style="color: #ff0033; font-weight: bold;">then</span>
        <span style="color: #ff0033; font-weight: bold;">set</span> thedate <span style="color: #ff0033; font-weight: bold;">to</span> current date
        <span style="color: #ff0033; font-weight: bold;">set</span> theday <span style="color: #ff0033; font-weight: bold;">to</span> day <span style="color: #ff0033; font-weight: bold;">of</span> thedate
        <span style="color: #ff0033; font-weight: bold;">set</span> themonth <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">month</span> <span style="color: #ff0033; font-weight: bold;">of</span> thedate
        <span style="color: #ff0033; font-weight: bold;">set</span> theyear <span style="color: #ff0033; font-weight: bold;">to</span> year <span style="color: #ff0033; font-weight: bold;">of</span> thedate
        <span style="color: #ff0033; font-weight: bold;">set</span> thetime <span style="color: #ff0033; font-weight: bold;">to</span> time <span style="color: #ff0033; font-weight: bold;">of</span> thedate
&nbsp;
        <span style="color: #ff0033; font-weight: bold;">set</span> thedate <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;-&quot;</span> <span style="color: #000000;">&amp;</span> theyear <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;-&quot;</span> <span style="color: #000000;">&amp;</span> shortMonth<span style="color: #000000;">&#40;</span>themonth<span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">me</span> <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;-&quot;</span> <span style="color: #000000;">&amp;</span> theday <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">string</span>
        <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #ff0033;">not</span> one_archive_per_day <span style="color: #ff0033; font-weight: bold;">then</span> <span style="color: #ff0033; font-weight: bold;">set</span> thedate <span style="color: #ff0033; font-weight: bold;">to</span> thedate <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;-&quot;</span> <span style="color: #000000;">&amp;</span> thetime <span style="color: #ff0033;">as</span> <span style="color: #0066ff;">string</span>
&nbsp;
        <span style="color: #ff0033; font-weight: bold;">set</span> backupFolder <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #000000;">&#40;</span>backup_folder <span style="color: #000000;">&amp;</span> backupBase <span style="color: #000000;">&amp;</span> thedate  <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;/&quot;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
&nbsp;
<span style="color: #808080; font-style: italic;">(* do the backup *)</span>
<span style="color: #808080; font-style: italic;">(* try *)</span>
<span style="color: #ff0033; font-weight: bold;">if</span> with_administrator_privileges <span style="color: #ff0033; font-weight: bold;">then</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> excluded <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot; &quot;</span>
<span style="color: #ff0033; font-weight: bold;">else</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> excluded <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot; --exclude='.Trash*' --exclude='.Spotlight*'  &quot;</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">if</span> archive_backup <span style="color: #ff0033; font-weight: bold;">then</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> posixDir <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">POSIX file</span> backup_folder
    <span style="color: #ff0033; font-weight: bold;">set</span> fileList <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">list</span> <span style="color: #0066ff;">folder</span> posixDir
    <span style="color: #ff0033; font-weight: bold;">set</span> archiveList <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
    <span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
        <span style="color: #808080; font-style: italic;">(* Search for existing backups *)</span>
        <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> i <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> fileList
            <span style="color: #ff0033; font-weight: bold;">set</span> thisFile <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">item</span> i <span style="color: #ff0033; font-weight: bold;">of</span> fileList
            <span style="color: #ff0033; font-weight: bold;">set</span> filePath <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">path to</span> resource thisFile <span style="color: #ff0033; font-weight: bold;">in</span> bundle posixDir
            <span style="color: #ff0033; font-weight: bold;">if</span> thisFile starts <span style="color: #ff0033; font-weight: bold;">with</span> backupBase <span style="color: #ff0033; font-weight: bold;">then</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> archiveList <span style="color: #ff0033; font-weight: bold;">to</span> archiveList <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">POSIX path</span> <span style="color: #ff0033; font-weight: bold;">of</span> filePath
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">(* List existing backups in order of last modified date *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> latestArchive <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> linkDest <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> listString <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
    <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> x <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> archiveList
        <span style="color: #ff0033; font-weight: bold;">set</span> listString <span style="color: #ff0033; font-weight: bold;">to</span> listString <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> x <span style="color: #ff0033; font-weight: bold;">of</span> archiveList
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> newString <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ls -dt &quot;</span> <span style="color: #000000;">&amp;</span> listString <span style="color: #808080; font-style: italic;">(* sort files by modified date *)</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> theList <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">paragraphs</span> <span style="color: #ff0033; font-weight: bold;">of</span> newString<span style="color: #000000;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">(* Clear out outdated backups *)</span>
    <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> theList <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">greater</span> than number_of_archives<span style="color: #000000;">&#41;</span> <span style="color: #ff0033;">and</span> <span style="color: #000000;">&#40;</span>number_of_archives <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">greater</span> than <span style="color: #000000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
        <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">with</span> y <span style="color: #ff0033; font-weight: bold;">from</span> <span style="color: #000000;">&#40;</span>number_of_archives <span style="color: #000000;">+</span> <span style="color: #000000;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#40;</span><span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> theList<span style="color: #000000;">&#41;</span>
            <span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;rm -Rf &quot;</span> <span style="color: #000000;">&amp;</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> y <span style="color: #ff0033; font-weight: bold;">of</span> theList
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
    <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> theList <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">greater</span> than <span style="color: #000000;">0</span> <span style="color: #ff0033; font-weight: bold;">then</span>
        <span style="color: #ff0033; font-weight: bold;">set</span> latestArchive <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">of</span> theList
        <span style="color: #ff0033; font-weight: bold;">if</span> latestArchive <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> backupFolder <span style="color: #ff0033; font-weight: bold;">then</span>
            <span style="color: #808080; font-style: italic;">(* Preserve same target folder *)</span>
            <span style="color: #ff0033; font-weight: bold;">if</span> <span style="color: #0066ff;">number</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">items</span> <span style="color: #ff0033; font-weight: bold;">in</span> theList <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">greater</span> than <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">then</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> latestArchive <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">quoted form</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">item</span> <span style="color: #000000;">2</span> <span style="color: #ff0033; font-weight: bold;">of</span> theList
            <span style="color: #ff0033; font-weight: bold;">else</span>
                <span style="color: #ff0033; font-weight: bold;">set</span> latestArchive <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span>
            <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
        <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
    <span style="color: #ff0033; font-weight: bold;">if</span> latestArchive <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">not</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;&quot;</span> <span style="color: #ff0033; font-weight: bold;">then</span>
        <span style="color: #ff0033; font-weight: bold;">set</span> linkDest <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot; --link-dest=&quot;</span> <span style="color: #000000;">&amp;</span> latestArchive
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> theScript <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;rsync &quot;</span> <span style="color: #000000;">&amp;</span> rsync_params <span style="color: #000000;">&amp;</span> linkDest <span style="color: #000000;">&amp;</span> excluded <span style="color: #000000;">&amp;</span> sourceDir <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; &quot;</span> <span style="color: #000000;">&amp;</span> backupFolder <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;; touch &quot;</span> <span style="color: #000000;">&amp;</span> backupFolder
<span style="color: #ff0033; font-weight: bold;">else</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> theScript <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #009900;">&quot;rsync &quot;</span> <span style="color: #000000;">&amp;</span> rsync_params <span style="color: #000000;">&amp;</span> excluded <span style="color: #000000;">&amp;</span> sourceDir <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; &quot;</span> <span style="color: #000000;">&amp;</span> targetDir <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;; touch &quot;</span> <span style="color: #000000;">&amp;</span> targetDir
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
<span style="color: #ff0033; font-weight: bold;">if</span> with_administrator_privileges <span style="color: #ff0033; font-weight: bold;">then</span>
    <span style="color: #0066ff;">do shell script</span> theScript <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
<span style="color: #ff0033; font-weight: bold;">else</span>
    <span style="color: #0066ff;">do shell script</span> theScript
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
<span style="color: #0066ff;">activate</span> <span style="color: #ff0033; font-weight: bold;">of</span> <span style="color: #0066ff;">me</span>
<span style="color: #0066ff;">beep</span>
<span style="color: #ff0033; font-weight: bold;">if</span> display_notification <span style="color: #ff0033; font-weight: bold;">then</span> display alert NameOfDisk <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot; drive backed up&quot;</span>
<span style="color: #808080; font-style: italic;">(*
on error
    display alert &quot;There was an error backing up &quot; &amp; NameOfDisk
end try
*)</span>
&nbsp;
&nbsp;
<span style="color: #ff0033; font-weight: bold;">to</span> shortMonth<span style="color: #000000;">&#40;</span>themonth<span style="color: #000000;">&#41;</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> MonthList <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">&#123;</span><span style="color: #0066ff;">January</span>, <span style="color: #0066ff;">February</span>, <span style="color: #0066ff;">March</span>, <span style="color: #0066ff;">April</span>, <span style="color: #0066ff;">May</span>, <span style="color: #0066ff;">June</span>, <span style="color: #0066ff;">July</span>, <span style="color: #0066ff;">August</span>, <span style="color: #0066ff;">September</span>, <span style="color: #0066ff;">October</span>, <span style="color: #0066ff;">November</span>, <span style="color: #0066ff;">December</span><span style="color: #000000;">&#125;</span>
    <span style="color: #ff0033; font-weight: bold;">set</span> mm <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #000000;">1</span>
    <span style="color: #ff0033; font-weight: bold;">repeat</span> <span style="color: #ff0033; font-weight: bold;">until</span> <span style="color: #0066ff;">item</span> mm <span style="color: #ff0033; font-weight: bold;">of</span> MonthList <span style="color: #000000;">=</span> themonth
        <span style="color: #ff0033; font-weight: bold;">copy</span> mm <span style="color: #000000;">+</span> <span style="color: #000000;">1</span> <span style="color: #ff0033; font-weight: bold;">to</span> mm
    <span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">repeat</span>
    <span style="color: #ff0033; font-weight: bold;">if</span> mm <span style="color: #000000;">&lt;</span> <span style="color: #000000;">10</span> <span style="color: #ff0033; font-weight: bold;">then</span> <span style="color: #ff0033; font-weight: bold;">copy</span> <span style="color: #009900;">&quot;0&quot;</span> <span style="color: #000000;">&amp;</span> mm <span style="color: #ff0033; font-weight: bold;">to</span> mm
    <span style="color: #ff0033; font-weight: bold;">return</span> mm
<span style="color: #ff0033; font-weight: bold;">end</span> shortMonth</pre></div></div>



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2007/03/rsync.zip">Download this script</a><br />
<br />
Copy the above code in Script Editor, and save it as an Application bundle giving it the name &quot;rsync&quot;. If you wish to give it a different name, or maintain a number of copies each with a different name, make sure to modify the first line of the Folder Action script appropriately.<br />
<br />
Finally, I can just drop a copy of the rsync application bundle in any thumb drive I want to keep a backup of, and apply the Folder Action to any computer I want to maintain backups on. Since the backup script is using <code>rsync</code> for the backup, I can have multiple backups on any number of computers and they will all mirror the contents of the thumb drive whenever the thumb drive is mounted on them. Additionally, a tar-gzipped copy of the thumb drive can optionally be created with a date stamp for archiving. Deletion of old archives must be managed manually.<br />
<br />
If you are having permission issues with some of the files on one of your thumb drives, you can enable the <code>with_administrator_privileges</code> property in the backup script. This will cause the script to ask you for a password every time the thumb drive is mounted before performing the backup, but will perform the backup as an administrator eliminating any permission issues.<br />
<br />
I feel that this script can still be improved, but for now it works just fine for my needs. If you have any suggestions for improving this process, feel free to add them in the comments.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2007/03/06/automating-backup-of-flash-drives/feed/</wfw:commentRss>
		<slash:comments>77</slash:comments>
		</item>
		<item>
		<title>LiveSearch 1.2</title>
		<link>http://www.cneophytou.com/2006/04/06/livesearch-12/</link>
		<comments>http://www.cneophytou.com/2006/04/06/livesearch-12/#comments</comments>
		<pubDate>Thu, 06 Apr 2006 21:02:32 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WP Plugin]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[LiveSearch]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2006/04/07/livesearch-12/</guid>
		<description><![CDATA[Ok. I&#8217;ve widgetized this plugin, and updated the headers of the plugin. Now you can just install it, and put it anywhere on your sidebar using the Widget plugin. In its options you can specify whether or not you want the searchbox to be encompassed in the default widget wrappers (disabled by default). Pretty straight [...]]]></description>
			<content:encoded><![CDATA[Ok. I&#8217;ve widgetized this plugin, and updated the headers of the plugin. Now you can just install it, and put it anywhere on your sidebar using the Widget plugin. In its options you can specify whether or not you want the searchbox to be encompassed in the default widget wrappers (disabled by default). Pretty straight forward.<br />
<br />
The css still needs to be updated, but I don&#8217;t know if I&#8217;ll be getting to that anytime soon. Any upgrades/suggestions anyone might have are more than welcome.<br />
<br />
Here is a quicklink to the <a id="p34" href="http://www.cneophytou.com/wp-content/uploads/2006/03/livesearch.zip">zip archive</a>. Original files are always linked from the main <a href="http://www.cneophytou.com/2006/03/26/livesearch/">LiveSearch post</a>.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2006/04/06/livesearch-12/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<series:name><![CDATA[LiveSearch]]></series:name>
	</item>
		<item>
		<title>LiveSearch Update</title>
		<link>http://www.cneophytou.com/2006/03/28/livesearch-update/</link>
		<comments>http://www.cneophytou.com/2006/03/28/livesearch-update/#comments</comments>
		<pubDate>Tue, 28 Mar 2006 20:55:48 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WP Plugin]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[LiveSearch]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2006/03/28/livesearch-update/</guid>
		<description><![CDATA[I&#8217;ve updated the code to include an option for the size of the search box which I neglected the first time around. I also crudely fixed up the code so that no javascript errors occur when a page without the searchbox is loaded. (For example on my site, any page outside the blog). I&#8217;ve done [...]]]></description>
			<content:encoded><![CDATA[I&#8217;ve updated the code to include an option for the size of the search box which I neglected the first time around. I also crudely fixed up the code so that no javascript errors occur when a page without the searchbox is loaded. (For example on my site, any page outside the blog). I&#8217;ve done some preliminary testing and it seems to work. If anyone can (or wants to) improve on this, please do so! The archive files on the <a href='http://www.cneophytou.com/2006/03/26/livesearch/'>original post</a> have been updated to reflect the new changes.<br /><span id="more-45"></span>
<br />
The next step would be to fix up the css a little bit. One thing I don&#8217;t like on my site is that the first result is not indented properly with the rest&#8230; I need to take a look at that. Also, would anyone be interested in moving all the color/size options for the css in the wp-options page? Is this a good/bad idea? This would be done by moving the css outside of the file and into the plugin code, so it gets printed directly in the header rather than linked to. It would make it relatively easier to make simple changes, but much harder to customize the css completely. It&#8217;s not that hard to do it, I just don&#8217;t know if people would prefer it that way or not. Also worth to note that with an external css file people can customize a different css for each of their themes if they are using multiple ones, whereas with all the color/size options moved into the WP Options page, then all that would have to be changed manually&#8230;]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2006/03/28/livesearch-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[LiveSearch]]></series:name>
	</item>
		<item>
		<title>LiveSearch</title>
		<link>http://www.cneophytou.com/2006/03/26/livesearch/</link>
		<comments>http://www.cneophytou.com/2006/03/26/livesearch/#comments</comments>
		<pubDate>Sun, 26 Mar 2006 13:39:17 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WP Plugin]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[LiveSearch]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2006/03/14/livesearch/</guid>
		<description><![CDATA[You might notice the spiffy little search box on the navigation bar of this blog. It&#8217;s an implementation of the LiveSearch code which I incorporated recently. It&#8217;s a nice little tool, using pretty straight basic (albeit a bit complex) AJAX code. However it did require some effort to implement this, as several files needed to [...]]]></description>
			<content:encoded><![CDATA[You might notice the spiffy little search box on the navigation bar of this blog. It&#8217;s an implementation of the <a href="http://fernando.dubtribe.com/archives/2005/06/01/livesearch-for-wordpress-1512/">LiveSearch code</a> which I incorporated recently.<br />
<br />
It&#8217;s a nice little tool, using pretty straight basic (albeit a bit complex) AJAX code. However it did require some effort to implement this, as several files needed to to be modified, and the required files placed all over the place. This meant that the ability to use this tool was restricted to people with some understanding of PHP, and also did not enable the clean integration of the code in WordPress.<br /><span id="more-31"></span>
<br />
So what I have done is integrate the required code/css into a small plugin which can be managed via the WordPress administration section. The plugin consists of a single directory which should be placed in your wp-content/plugins directory<span class='deprecated'>, as well as two files that should be placed in your chosen theme directory. If you use multiple themes (for example via the Theme Switcher plugin), then you should place a copy of these two files in the directory of each theme you want to use it in. The two files are:</span><br />
<code>
<span class='deprecated'>livesearch.css</span><br />
<span class='deprecated'>searchform.php</span>
</code><br />
<span class='deprecated'>The first one is so that users can style the LiveSearch boxes so it fits with their chosen theme, and users should not be required to make manual modifications to their code every time they change a theme. The second file is a replacement of the existing one, and enables the LiveSearch box. In case the plugin is disabled from the WordPress admin section (or simply disabled from the LiveSearch Options page, or the plugin deleted from the plugins directory), then the default search box will be displayed instead, without breaking its functionality (with some minor modifications). Note that running your WordPress installation with LiveSearch enabled will turn the &#8220;Search&#8221; button off, but disabling the plugin will turn it back on.</span><br />
<span class='update'>Updated 2007-03-27:</span> <a href='http://www.cneophytou.com/2007/03/27/livesearch-13/'>LiveSearch 1.3</a> is now up! No files need to be moved to make this version work. You can edit <code>livesearch.css</code> in-place, in its plugin directory. If you wish to maintain multiple themes, then you can still move <code>livesearch.css</code> into your theme directory. A style file found in the current theme&#8217;s directory will always be used over the one located in the plugin directory.<br />
<br />
<span class='update'>Updated 2008-03-17:</span> Updated to <a href="http://downloads.wordpress.org/plugin/livesearch.1.4.zip">LiveSearch 1.4</a>, to support WordPress 2.5.<br />
<br />
As a side note, the outputed html should be XHTML Strict, with tentative WCAG triple-a compliance (like the rest of this site. Visit <a href="/about/accessibility/">the accessibility page</a> for more information.<br />
</p><h2>Download:</h2><p>
You can download the plugin from its <a href='http://wordpress.org/extend/plugins/livesearch/' title='LiveSearch'>WordPress Plugin Directory</a>, unpack it and follow the README instructions to install it (which are pretty much a review of this post).<br />
<br />
As is always the case in this sort of thing, there are no guarantees. The plugin should work, but just in case make a backup of any files you replace (though now you shouldn&#8217;t need to even do that). There&#8217;s always room for improvement, and I&#8217;m not just referring to the LiveSearch code, but also to the plugin wrapper. Feel free to comment.<br />
</p><h2>Change Log</h2>
<ul>
<li>v1.4 &#8211; Plugin works with WordPress 2.5 (thanks to the Korean <a href="http://unfusion.kunsan.ac.kr/word/archive/779">Club despair</a> blog) and is now also hosted on the <a href='http://wordpress.org/extend/plugins/livesearch/' title='LiveSearch'>WordPress Plugin Directory</a></li>
<li>v1.3 &#8211; Plugin now completely drag-and-drop, no need to copy any files anywhere. Minor bug fixes/improvements in javascript code. Now hosted on <a href='http://code.google.com/p/livesearchwpplugin/'>Google Code</a></li>
<li>v1.2 &#8211; Plugin widgetized, minor javascript changes</li>
<li>v1.1 &#8211; Fixed potential javascript errors</li>
<li>v1.0 &#8211; Original plugin, LiveSearch code copy/pasted directly without modifications</li>
</ul>
<p>
For more detailed explanations of the changes in each version, follow the series links in the sidebar.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2006/03/26/livesearch/feed/</wfw:commentRss>
		<slash:comments>71</slash:comments>
	
		<series:name><![CDATA[LiveSearch]]></series:name>
	</item>
		<item>
		<title>WP-Cron-Mail plugin</title>
		<link>http://www.cneophytou.com/2006/03/14/wp-cron-mail-plugin/</link>
		<comments>http://www.cneophytou.com/2006/03/14/wp-cron-mail-plugin/#comments</comments>
		<pubDate>Tue, 14 Mar 2006 18:20:30 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Review]]></category>
		<category><![CDATA[Terminal]]></category>
		<category><![CDATA[Web Standards]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[WP Plugin]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2006/03/14/wp-cron-mail-plugin/</guid>
		<description><![CDATA[Another tool I decided to use was WordPress&#8217; post-by-email feature. However this was very limiting, as after posting via email usually action was required within WordPress &#8211; visit the wp-mail.php page, and then modify the post to place it in the necessary categories etc. The first problem is overcome by using the wp-cron-mail plugin which [...]]]></description>
			<content:encoded><![CDATA[Another tool I decided to use was WordPress&#8217; post-by-email feature. However this was very limiting, as after posting via email usually action was required within WordPress &#8211; visit the wp-mail.php page, and then modify the post to place it in the necessary categories etc.<span id="more-35"></span><br />
<br />
The first problem is overcome by using the wp-cron-mail plugin which comes with the <a href="http://www.skippy.net/">wp-cron</a> plugin. However I still wanted some more functionality from this.<br />
<br />
My modification to wp-cron-mail.php accomplishes the following things:</p>
<ol>
<li>Adds the option of specifying which categories the post should be placed in. This is accomplished by (optionally) having the first non-empty line of your email message be in the form of:<br /><br />
<code>{* Category Name, Another category name, catID *}</code><br /><br />
where &#8220;Category Name&#8221; can be any text. If the specified category exists it will be correctly identified, if not it will be created. catID needs to be the ID number of an existing category. You can use either method to define categories, or both of them together.</li>
<li> Limits posting to registered users only. When an email is received the plugin will try to match the Reply-To: address (or if that does not exist, the From: address) with all the emails of the registered users. If such an address is found, then the owner of that email address will be assigned as author (as with the default version of this plugin). Even though posting this way is strictly limiting, the option to override this is provided through the Options page, where you have the ability to either allow all email addresses, or provide a list of additional allowed email addresses (which can be empty &#8211; the default). With either of the last two options, if a message is posted from a non-registered email address, it will be marked as authored by the admin of the blog.</li>
<li>Added the ability to replace wp-mail.php with a single function call to take advantage of the wp-cron-mail improvements, with the option of sending a notification email if posts are retrieved through this file, instead of just outputing the results to the browser.</li>
<li>Added an Options page in the admin section to manage a lot of the configurable options of this plugin.</li>
</ol>
<p>
<br />
You can download a copy of this file in a <a id="p37" href="http://www.cneophytou.com/wp-content/uploads/2006/03/wp-cron-mail.zip">zip archive</a> or a <a id="p36" href="http://www.cneophytou.com/wp-content/uploads/2006/03/wp-cron-mail.tar.gz">tarball</a>, which include a README file with instructions for replacing wp-mail.php. Before you use these files however, you should make sure of two things: that your wp-mail.php file is indeed working (to ensure that you do not suffer from <a href="http://wordpress.org/support/topic/5436">the known bug</a> that WordPress is still suffering from), and also that you have a working installation of wp-cron-mail. Then you can go ahead and replace that file with the one found here.<br />
<br />
As always, no guarantees, and there is always room for improvement. I haven&#8217;t changed the code from the original wp-cron-mail, merely added to it. And it seems to be working, both this and the previous post were posted using this plugin <img src='http://www.cneophytou.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Feel free to comment.<br />
<br />
<strong>UPDATE:</strong> Apparently something went wrong with this plugin&#8230; If you visited this site in the first 3 hours after this post was posted, you would notice something very strange&#8230; all spaces were encoded as &#8220;=20&#8243;!!! I need to track this down&#8230;]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2006/03/14/wp-cron-mail-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

