<?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; OS X</title>
	<atom:link href="http://www.cneophytou.com/tag/os-x/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>Tue, 20 Oct 2009 13:54:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Smooth SSH Passwordless Authentication</title>
		<link>http://www.cneophytou.com/2008/02/05/smooth-ssh-passwordless-authentication/</link>
		<comments>http://www.cneophytou.com/2008/02/05/smooth-ssh-passwordless-authentication/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 14:36:17 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Terminal]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2008/02/05/smooth-ssh-passwordless-authentication/</guid>
		<description><![CDATA[This post first appeared on the Voodoo Finance blog which is maintained by my friend Constantinos Michael, and I&#8217;m recreating it here with some extensions so I can find the code more easily when I need it. Using computers remotely is a big part of what I do. Whether I&#8217;m at home and need to [...]]]></description>
			<content:encoded><![CDATA[This post first appeared on the <a href='http://cmichae.acm.jhu.edu/blog/2007/01/18/ssh-passwordless-authentication/'>Voodoo Finance</a> blog which is maintained by my friend <a href='http://cmichae.acm.jhu.edu/'>Constantinos Michael</a>, and I&#8217;m recreating it here with some extensions so I can find the code more easily when I need it.<br />
<br />
Using computers remotely is a big part of what I do. Whether I&#8217;m at home and need to connect to a computer in the lab, or in the lab or the road and need to connect to my home computer. The way I (and pretty much most other linux users) is through SSH. If you&#8217;ve ever had to open more than one ssh connection however, it gets old pretty quickly. First you need to remember the host (and potentially the port, if it&#8217;s not standard) of the machine you&#8217;re connecting to, and also your username on that machine. After those are typed in, you need to enter your password every time you initiate a new connection. Well, all of the above can be automated with a few quick keystrokes in the terminal.<br />
<span id="more-109"></span>
<br />
The first step is to generate an SSH key pair. This private / public key pair will be used to authenticate you on the target machine. As it&#8217;s using an RSA key pair, you can probably leave the passphrase empty as the attacker would first need access to your private key. If you&#8217;re really paranoid, you can type in an easy password (but you&#8217;ll need to type this password in every time you use the key). To generate this key, type the following in a terminal window:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh-keygen</span> <span style="color: #660033;">-t</span> rsa</pre></div></div>



<p>
After this is done, you&#8217;re ready to install this key on any remote machine you wish to access. Before you do that however, I suggest setting up some aliases to the target machine so you don&#8217;t have to type the host and username every time you wish to log in. To do that, you need to edit (or create) a file called <code>~/.ssh/config</code>. In this file, you can enter a block of text for each host you wish to create an alias for. You can add as many hosts you want, just make sure there&#8217;s an empty line between each configuration. This is an example of what needs to go in this file:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">host lab
user admin
HostName www.example.com
port 12345</pre></div></div>



<p>
There are many more options that can go in there, and it mostly depends on your setup. But for anything that you don&#8217;t need to specify in the ssh command line, you don&#8217;t need to add an option for in this file. For example with the text I provided above, when you type <code>ssh lab</code> in the command line, it will try to connect to <code>www.example.com</code> using the username <code>admin</code> on port <code>12345</code>. This would be equivalent to typing <code>ssh &#x61;&#x64;&#x6d;&#x69;&#x6e;&#x40;&#x77;&#x77;&#x77;&#x2e;&#x65;xample.com -oPort=12345</code>. Much easier, isn&#8217;t it?<br />
<br />
Now on to the juicy stuff. Create a script that will install your key on the remote machine. To do that, paste these instructions in the terminal:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ssh-install-key ;
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> a+<span style="color: #c20cb9; font-weight: bold;">w</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ssh-install-key
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;cat ~/.ssh/id_rsa.pub | ssh <span style="color: #000099; font-weight: bold;">\$</span>{1} <span style="color: #000099; font-weight: bold;">\&quot;</span>cat - &gt;&gt; ~/.ssh/authorized_keys<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> \
     <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ssh-install-key
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">chmod</span> a-w+x <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>ssh-install-key</pre></div></div>



<p>
At this stage, you&#8217;re done with the setup. All you need to do is type</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ssh-install-key lab</pre></div></div>



<p>
in the terminal window (assuming you&#8217;ve set up the above <code>config</code> file with a host called <code>lab</code>), enter  your password as many times as you&#8217;re prompted, and you&#8217;re done! Every subsequent time you wish to connect to this machine, all you need to do is type <code>ssh lab</code>, and you&#8217;re immediately connected, no questions asked! (Unless of course you provided a passphrase for your key pair, in which case you will be prompted for that password).]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2008/02/05/smooth-ssh-passwordless-authentication/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Simulating encrypted physical disk in OS X</title>
		<link>http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/</link>
		<comments>http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 22:01:55 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Applescript]]></category>
		<category><![CDATA[CC Licence]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Finder]]></category>
		<category><![CDATA[Folder Actions]]></category>

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


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


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


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


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


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



<p>
<a href="http://www.cneophytou.com/wp-content/uploads/2008/02/secure_scripts.tgz">Download all scripts</a><br />
<br />
As always, these scripts are provided without any warranty, and any suggested improvements are more than welcome!]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2008/01/30/simulating-encrypted-physical-disk-in-os-x/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Spoofing Leopard&#8217;s MAC address</title>
		<link>http://www.cneophytou.com/2008/01/19/spoofing-leopards-mac-address/</link>
		<comments>http://www.cneophytou.com/2008/01/19/spoofing-leopards-mac-address/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 19:34:50 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Terminal]]></category>
		<category><![CDATA[Ethernet]]></category>
		<category><![CDATA[Leopard]]></category>
		<category><![CDATA[MAC Address]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2008/01/19/spoofing-leopards-mac-address/</guid>
		<description><![CDATA[There are many legitimate reasons why someone would want to spoof a system&#8217;s MAC address. In my case, my University binds our network ports to a specific computer&#8217;s MAC address, and only allows you to reset that address once a week. My problems start when I want to switch my two computers for whatever reason, [...]]]></description>
			<content:encoded><![CDATA[There are many legitimate reasons why someone would want to spoof a system&#8217;s MAC address. In my case, my University binds our network ports to a specific computer&#8217;s MAC address, and only allows you to reset that address once a week. My problems start when I want to switch my two computers for whatever reason, and connect my smaller iBook to the wall (let&#8217;s say I want to keep a web server online, but wish to take my MacBook Pro on the road).<br />
<span id="more-106"></span>
<br />
In Tiger, it was very easy to spoof a MAC address:<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ifconfig</span> en0 ether 00:00:00:00:00:00</pre></div></div>



<p>
where <code>en0</code> is the network interface you wish to change the MAC address of, and <code>00:00:00:00:00:00</code> is the target MAC address. With Leopard, that line no longer works. No matter how much I searched, I couldn&#8217;t find a solid alternative. Turns out, it&#8217;s extremely simple:<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ifconfig</span> en0 lladdr 00:00:00:00:00:00</pre></div></div>



<p>
That&#8217;s it!<br />
<strong>Note:</strong> This has been tested under 10.5.1 with both en0 and en1, and at least for the wired interface it works as advertised.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2008/01/19/spoofing-leopards-mac-address/feed/</wfw:commentRss>
		<slash:comments>62</slash:comments>
		</item>
		<item>
		<title>Bandwidth throttling in OS X</title>
		<link>http://www.cneophytou.com/2008/01/19/bandwidth-throttling-in-os-x/</link>
		<comments>http://www.cneophytou.com/2008/01/19/bandwidth-throttling-in-os-x/#comments</comments>
		<pubDate>Sat, 19 Jan 2008 14:27:52 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Terminal]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2008/01/19/bandwidth-throttling-in-os-x/</guid>
		<description><![CDATA[It&#8217;s been a while since my last post, and that&#8217;s because I&#8217;ve been busy with coursework, so I haven&#8217;t had time to mess around with OS X. However there was one feature I needed to figure out out of necessity if anything else. I have certain files I wish to freely share with some friends [...]]]></description>
			<content:encoded><![CDATA[It&#8217;s been a while since my last post, and that&#8217;s because I&#8217;ve been busy with coursework, so I haven&#8217;t had time to mess around with OS X. However there was one feature I needed to figure out out of necessity if anything else. I have certain files I wish to freely share with some friends of mine, and the easiest way for me to do that was put them on my local web server and serve them over http. The only problem with this approach, is that the university has a weekly data transfer limit of 5Gb per week during peak hours, those being between 6pm and midnight. Additionally, local traffic doesn&#8217;t count towards that limit, and if that limit is exceeded then my Internet would be blocked and limited to only the local network for the remainder of that week.<br />
<br />
Therefore, the solution I was looking for would not require me to turn off my web server (as I&#8217;d like to still access it locally), and would ideally simply limit the outgoing traffic automatically between those two times.<br />
<span id="more-105"></span>
<br />
One aspect that made this particular solution usable, was the fact that port 80 is firewalled from the external world. This means that if I want to run a web server on my local machine accessible from the outside, I have to do it on a different port. Therefore I use port 80 to access the web server locally, and port 8080 to access it from the outside. This gives me a dedicated point of entry for the traffic that I wish to limit the bandwidth.<br />
<br />
The solution is fairly simple, and involves only two tools: <code>cron</code>, for enabling and disabling the filter at specific times, and <code>ipfw</code> for actually setting up the filters. The idea is this: create a <code>pipe</code> using <code>ipfw</code> that limits outgoing traffic on port 8080 to a specified rate (let&#8217;s say 15KByte/s), and using a cron job add the pipe to my network interface at 6pm, and delete it at midnight. That&#8217;s it! Theoretically you only have to create the pipe once, but in order to have this survive across restarts, I added another cron job at 5:59pm that re-creates the pipe. The full contents of the crontab file (located at <code>/etc/crontab</code>) are as follows:<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Set the bandwidth limit at 5:59pm</span>
<span style="color: #000000;">59</span> <span style="color: #000000;">17</span>	<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span>	root	<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>ipfw pipe <span style="color: #000000;">1</span> config bw 15KByte<span style="color: #000000; font-weight: bold;">/</span>s
<span style="color: #666666; font-style: italic;"># Add the bandwidth limit to the specified port at 6:00pm</span>
<span style="color: #000000;">0</span> <span style="color: #000000;">18</span>	<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span>	root	<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>ipfw add <span style="color: #000000;">1</span> pipe <span style="color: #000000;">1</span> src-port <span style="color: #000000;">8080</span>
<span style="color: #666666; font-style: italic;"># Lift the bandwidth limit at 12:00am</span>
<span style="color: #000000;">0</span> <span style="color: #000000;">0</span>	<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span>	root	<span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span>ipfw delete <span style="color: #000000;">1</span></pre></div></div>



<p>
The problem with this is if the computer boots up or reboots after 6pm and before midnight, the filters will not be set. The solution is to use <code>anacron</code> for executing the ipfw commands, But as I rarely shut down or reboot my computer, it wasn&#8217;t worth it for me to make that happen. If I do make those modifications, I will post an update here. Also note that the ipfw commands need to be executed as root.<br />]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2008/01/19/bandwidth-throttling-in-os-x/feed/</wfw:commentRss>
		<slash:comments>0</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>Share Windows XP license between Parallels and Boot Camp</title>
		<link>http://www.cneophytou.com/2007/07/18/share-windows-xp-license-between-parallels-and-boot-camp/</link>
		<comments>http://www.cneophytou.com/2007/07/18/share-windows-xp-license-between-parallels-and-boot-camp/#comments</comments>
		<pubDate>Tue, 17 Jul 2007 19:46:40 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[OS X]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Boot Camp]]></category>
		<category><![CDATA[Parallels]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2007/07/18/share-windows-xp-license-in-parallels-and-boot-camp/</guid>
		<description><![CDATA[Since my first installation of Boot Camp and Parallels, I&#8217;ve kept two separate Windows XP installations on my drive. One on Boot Camp, used for gaming and other resource-intensive tasks, and the other exclusively on Parallels, used for running some random software I come across and also to test the websites I build in IE. [...]]]></description>
			<content:encoded><![CDATA[Since my first installation of <a href='http://www.apple.com/bootcamp/'>Boot Camp</a> and <a href='http://www.parallels.com/'>Parallels</a>, I&#8217;ve kept two separate Windows XP installations on my drive. One on Boot Camp, used for gaming and other resource-intensive tasks, and the other exclusively on Parallels, used for running some random software I come across and also to test the websites I build in IE. The reason I&#8217;ve been doing this is because even though Parallels touts being able to <a href='http://lifehacker.com/software/geek-to-live/virtualize-and-dual+boot-the-same-windows-on-your-mac-267905.php'>run a boot camped windows installation</a> [via <a href='http://www.lifehacker.com'>lifehacker</a>], you can&#8217;t do BOTH (at least not off the shelf). You have a Boot Camp installation that you either run through Parallels, or by itself. The reason for this is that Parallels has to create a new hardware profile which XP picks up as a different machine, and invalidates your installation.<br />
<br />
<span id="more-95"></span>
No matter how much I searched I couldn&#8217;t find a way to use the same installation under both circumstances. Thus, when Parallels 3 came out I immediately paid for the upgrade (even before the release) for the much promised &#8217;3D&#8217; support, which, I believed, would enable me to play at least some of my games under Parallels, thus relatively eliminating the need to boot into XP. Alas, as most others, I was frustrated to find out that Parallels&#8217; so-called 3D support was actually marginal support for DirectX 8.1, and absolutely NO support for DX9 or greater.<br />
<br />
After all but giving up hope, I came across <a href='http://lifehacker.com/software/geek-to-live/virtualize-and-dual+boot-the-same-windows-on-your-mac-267905.php#c1816617'>this comment</a> on the afore-mentioned lifehacker post, which pointed me to a <a href='http://forum.parallels.com/showthread.php?t=13394'>Parallels&#8217; forum thread</a>. To cut a long story short, I am now able to boot the same Boot Camp installation of XP both directly AND via Parallels without having WGA complain and invalidate my copy!<br />
<br />
The process (reiterated from the above thread), is very simple:<br />
</p>
<ol>
<li>Find the MAC address of your Apples&#8217;s &#8220;en0&#8243; adaptor. To do that, either:<ul><li>Open Terminal.app and type <code>ifconfig en0</code>, the MAC address should be located on the line that says &#8220;ether 00:00:00:00:00:00&#8243;, where &#8217;00:00:00:00:00:00&#8242; would be the code you&#8217;re looking for</li><li>OR open &#8220;System Preferences&#8221;, click on the &#8220;Network&#8221; icon, from the &#8220;show&#8221; drop down select &#8220;Built-in Ethernet&#8221;, click the &#8220;Ethernet&#8221; tab, and the MAC address should be listed under &#8216;Ethernet ID:&#8217;</li></ul></li>
<li>Start up Parallels and select your Boot Camp installation (do NOT start the virtual machine)</li>
<li>Go to &#8220;Edit&#8221;/&#8221;Virtual Machine&#8221;</li>
<li>Select &#8220;Network Adapter 1&#8243; on the left, and the &#8220;Advanced&#8221; tab on the right</li>
<li>In the &#8220;MAC address&#8221; field replace the entry with the MAC address of en0 which you lifted above, minus the colons.</li>
</ol>
<p>
<br />
That&#8217;s it! No more complaining by that damned WGA <img src='http://www.cneophytou.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2007/07/18/share-windows-xp-license-between-parallels-and-boot-camp/feed/</wfw:commentRss>
		<slash:comments>1</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>68</slash:comments>
		</item>
		<item>
		<title>Hints and code snippets for a more sane OS X Terminal experience</title>
		<link>http://www.cneophytou.com/2006/11/13/hints-and-code-snippets-for-a-more-sane-os-x-terminal-experience/</link>
		<comments>http://www.cneophytou.com/2006/11/13/hints-and-code-snippets-for-a-more-sane-os-x-terminal-experience/#comments</comments>
		<pubDate>Mon, 13 Nov 2006 11:06:56 +0000</pubDate>
		<dc:creator>Constantinos</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Hack]]></category>
		<category><![CDATA[Terminal]]></category>
		<category><![CDATA[Unix]]></category>

		<guid isPermaLink="false">http://www.cneophytou.com/2006/11/13/hints-and-code-snippets-for-a-more-sane-os-x-terminal-experience/</guid>
		<description><![CDATA[Pretty long title, huh? Well, I&#8217;ve finally made the decision that there&#8217;s no way I&#8217;ll ever be able to keep a journal. So instead, I&#8217;ll start posting code snippets and general hints that I mostly harvested online (and try to cite wherever I can &#8211; remember that is -), but also modified to suit my [...]]]></description>
			<content:encoded><![CDATA[Pretty long title, huh? Well, I&#8217;ve finally made the decision that there&#8217;s no way I&#8217;ll ever be able to keep a journal. So instead, I&#8217;ll start posting code snippets and general hints that I mostly harvested online (and try to cite wherever I can &#8211; remember that is -), but also modified to suit my purposes&#8230; First up is seamless integration of X11 in OS X with the Terminal application.. Read on for the code<br />
<span id="more-71"></span>
<br />
First off, the reason I got started in this: I HATE xterm. Really dislike it, to the point where I would actively avoid any xwindow apps. That&#8217;s until I came across <a href='http://www.macosxhints.com/article.php?story=2005091004025818&#038;query=x11%2Bterminal'>this hint</a> at <a href='http://www.macosxhints.com'>macosxhints.com</a>. Really nice hint, but to get the best possible approach you have to shift through all the comments, and merge some bash with some csh scripts in order to get the most seamless approach. So this is my solution, for the bash shell, derived from that hint on macosxhints, and explicitly avoiding any modifications of master files (which might get overwritten with an OS X upgrade).<br />
<br />
First step, is to prevent X11 from opening up an xterm window. That&#8217;s fairly easy. Simply create a file called <code>.xinitrc</code> under your home (<code>~/</code>) directory with your favorite editor (like <a href='http://macromates.com/'>TextMate</a> <img src='http://www.cneophytou.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  ), and paste this code in it (lifted straight off the <a href='http://www.macosxhints.com/article.php?story=2005091004025818&#038;query=x11%2Bterminal'>aforementioned hint</a>):<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>X11R6<span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># surpress starting of an initial xterm</span>
xterm<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># remember the display name</span>
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DISPLAY</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>.tmp<span style="color: #000000; font-weight: bold;">/</span>X11-display-<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">hostname</span> -s<span style="color: #000000; font-weight: bold;">`</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># run system-defined xinitrc (doesn't return)</span>
<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>X11<span style="color: #000000; font-weight: bold;">/</span>xinit<span style="color: #000000; font-weight: bold;">/</span>xinitrc</pre></div></div>




<p>
Now what this does, is redefine the function that calls up the xterm window, and make it do nothing. Then the master file is sourced, so no harm no foul. This modification will survive across upgrades too.<br />
<br />
Now to the more tricky part. In your <code>.bash_profile</code> file, insert this little gem of a code:<br />
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #007800;">$DISPLAY</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #007800;">$SSH_TTY</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #007800;">$REMOTEHOST</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DISPLAY</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ps</span> xw <span style="color: #660033;">-o</span> <span style="color: #7a0874; font-weight: bold;">command</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">'/^\/.*X11.*\:/s/.*:/:/p'</span><span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$DISPLAY</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>
<span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #000000; font-weight: bold;">if</span> xwininfo <span style="color: #660033;">-display</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DISPLAY</span>&quot;</span> <span style="color: #660033;">-root</span> <span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;&amp;</span><span style="color: #000000;">1</span>
    <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> Using X11 display <span style="color: #007800;">$DISPLAY</span>
        <span style="color: #7a0874; font-weight: bold;">export</span> DISPLAY
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">unset</span> DISPLAY
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>



<p>
<br />
This piece of code simply tries to capture the $DISPLAY given by X11, so it can be used in the Terminal. This is usually <code>:0</code>, but not if you&#8217;re running it over ssh. This should catch either.<br />
<br />
So far so good. Whenever I need xwindows, I can start X11, then open a new terminal window and voila. But wait a minute. Why would I have to manually start X11? When that app is idle, it&#8217;s using up almost zero resources. I could very easily place it in my startup items. That&#8217;s a good solution, with the problem being that I rarely need X11, and I don&#8217;t want to have it in my dock all the time.<br />
<br />
This is where the magic happens (btw this same method can be used to hide the dock and menu-bar of pretty much ANY app). Find the X11 app in the finder under <code>/Applications/Utilities/</code>. Ctrl-click on it, and &#8220;Show Package Contents&#8221;. Then navigate to <code>Contents/Info.plist</code>, open it up in the Property List Editor, and add a new sibling under Root called <code>LSUIElement</code>, set it to boolean, and give it the value &#8216;Yes&#8217; (or set it to string and give it the value 1). Save the plist file, and close it.<br />
<br />
Almost there. Now in the terminal, give the command <code>touch /Applications/Utilities/X11.app/</code>, and you&#8217;re all set. Double click on the X11 app to start it, and&#8230; nothing! The only way you can know it&#8217;s running, is if you look for it in the Activity Monitor! This won&#8217;t survive across upgrades of X11, and in fact if X11 gets upgraded you <em>will</em> have to open Activity Monitor to kill it before the upgrade, but I feel it&#8217;s an easy change to make, and a change that&#8217;s almost a second nature to me.<br />
<br />
So that&#8217;s my solution for this. Right now I have X11 running in the background at all times, and terminal simply captures the display and uses it whenever it needs it. And since all the modified files are in the user&#8217;s home directory, this solution will survive across OS upgrades. As a final note, I do have the capture script for tcsh, but it&#8217;s the same as the script listed in a comment of the previously mentioned macosxhints hint, so I won&#8217;t enter it here again. Just look for it there.]]></content:encoded>
			<wfw:commentRss>http://www.cneophytou.com/2006/11/13/hints-and-code-snippets-for-a-more-sane-os-x-terminal-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
