Categories
Computers

once again welcoming your comments. and what is lua.

I’ve upgraded my moveable type installation to version 3.2. I hope this solves the spam comment problem I was having with moveable type 3.0.1.
In the next few months, I’ll be introducing you to the programming language “lua”.
Lua can be downloaded from http://lua.org. Here is the direct link : http://luaforge.net/frs/download.php/1106/lua5_1b1_Darwin83.tar.gz for Mac OS X.
Lots of people comment how they “used to program” or would “like to program” and yet none of these people ever do. What a shame. Lua is a simple programming language, so why not get started with a little teaser to make you see how easy it is to play.

function shuffle()
local suits={"spades", "clubs", "diamonds", "hearts"}
local cards={"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}
deck = {}
local index = 1
for suitIndex, suit in ipairs( suits ) do
for cardIndex, card in ipairs( cards ) do
deck[ index ] = { suit = suit, card = card, value = { cardIndex } }
if cardIndex == 1 then
-- in blackjack, the ace is worth either 1 or 11
table.insert( deck[ index ].value, 11 )
end
index = index + 1
end
end
for i = 1, 52 do
local randomIndex = math.random( 52 )
deck[ randomIndex], deck[ i ] = deck[ i ], deck[ randomIndex ]
end
curCard = 1
end
function draw()
print( string.format("%s of %s", deck[ curCard ].card, deck[ curCard ].suit))
curCard = curCard + 1
end

And here is example output:

< shuffle()
< draw()
4 of clubs
< draw()
6 of clubs

Ok. Who am I kidding. You guys don’t want to learn lua. But if you do, here are some good questions: what is a function and how do you call one? What does call a function mean? What does it mean when the word “local” appears before a variable name? Are the words “for”, “function”, “end”, “do” and “if” special in some way? What is math.random? What is the significance of the curly braces “{” and “}”? Brackets “[” and “]” and parenthesis “(” and “)”?
I await with baited breath to see if anyone really asks me any programming questions.

Categories
Computers

Backup this File, but only if it changes…

Have you ever wanted to try make a backup of a large file, or a large number of files, but didn’t want to waste the disk space and time it took to do it? For example, I recently ran iPhoto 5 for the first time, and it needed to upgrade my iPhoto library. But I still wanted to be able to run iPhoto 4 with it.
On Mac OS X, this is one task that is made easy by the UNIX underpinnings of that operating system.
You see, on a UNIX system, each file is actually simply a pointer to something called an INODE. It is possible that two files can share an inode. In that case, the files are identical. However, if one of the files is deleted or rewritten, then a new inode is created. The other file that shared the inode will be untouched.
I’ve leveraged this fact to make my “upgrade” to iPhoto 5 without modifying my original iPhoto library.
First, I renamed my iPhoto Library
mv "iPhoto Library" "old iPhoto Library"
Then I created a mirror directory structure
cd "old iPhoto Library"
find . -type d -exec mkdir -p ../iPhoto\ Library/{} ';'
Then I used the ln command to create new files pointing to the INODES of the existing files
find . -type f -exec ln {} ../iPhoto\ Library/{} ';'
This task will consume very little disk space, even if the images are really huge. If I ever want to revert to the old library, simply swap the names of the directories around.

Categories
Computers

AppleScript trick


If you have multiple copies of the same application on your hard disk, it can be a bit unpredictable which one will launch if you say
tell application "MyApp" to activate
in AppleScript.
Here is a code snippet I figured out for making sure it is the one in the /Applications directory that gets launched.
set mypath to ""
tell application "Finder" to set mypath to path to the "apps" from system domain as string
set mypath to mypath & "MyApp.app"
tell application mypath to activate