<?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; Folder Actions</title>
	<atom:link href="http://www.cneophytou.com/tag/folder-actions/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>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>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>
	</channel>
</rss>

