« September 2005 | Main | November 2005 »

October 2005

October 29, 2005

Subversion

Two weeks ago I've decided to give Subversion another try. So far I have zero complaints and if you're wondering whether it is ready to be used in a real project or not, my opinion is that it is indeed ready.

October 28, 2005

AppleScript, Export iCal Birthdays to Remind

Another AppleScript to export all Birthdays from iCal to remind-formated text file:

property reminders_path : POSIX path of ((path to home folder) as text) & "Reminders/Birthdays.txt"
property days_in_advance : 7 -- how many days in advance to remind

tell application "iCal"
    do shell script "mv " & reminders_path & " " & reminders_path & ".bak"
    set this_calendar to first calendar whose title contains "Birthdays"
    repeat with this_event in every event of this_calendar
        tell this_event
            set es to summary as string
            set n to offset of "’" in es
            set l to length of es
            set event_summary to (text 1 thru (n - 1) of es) & "'" & (text (n + 1) thru l of es)
            set start_date to start date
            set date_string to (((month of start_date) as string) & " " & (day of start_date) as string)

            set reminder_string to date_string & " +" & days_in_advance & " MSG " & event_summary & " %b.%"
            do shell script "echo \"REM " & reminder_string & "\" >> " & reminders_path
        end tell
    end repeat
end tell

Make sure to update reminders_path and days_in_advance properties according to your situation and/or liking.

P.S. I promise it's the last one. At least for a little while.

Mute/unmute the sound

I absolutely love Mac OS X for that behind the cute facade there's an even more beautiful thing called unix. Combine the power of unix shell with wide adoption of AppleScript and you can achieve pretty much anything. For example, to automatically mute the sound at night, just do the following:

1. Start the Terminal
2. Open crontab for editing using the following command:

$ crontab -e

3. Now copy these two lines into the editor:

0 1 * * * osascript -e 'tell application "System Events" to set volume with output muted'
0 6 * * * osascript -e 'tell application "System Events" to set volume without output muted'

4. Save and quit and you're done. This will mute the sound at 1:00AM and un-mute it at 6:00AM.

October 27, 2005

AppleScript, "Open Safe Files" Folder Action

Continuing the AppleScript theme, here's the script that I wrote to use as a Folder Action. It automatically opens "safe" files after you download or copy them to the designated folder—I disabled this feature in Safari, since my understanding of "safe" is quite different from Safari's.

property safe_types_list : {
    "org.gnu.gnu-tar-archive", "public.tar-archive",
    "org.gnu.gnu-zip-archive", "org.gnu.gnu-zip-tar-archive", 
    "com.apple.binhex-archive", "com.apple.macbinary-archive",
    "public.cpio-archive", "com.pkware.zip-archive", 
    "com.allume.stuffit-archive", 
    "com.apple.disk-image-udif", "public.iso-image"
}

on adding folder items to this_folder after receiving added_items
tell application "Finder"
repeat with a_file in added_items
set UTI to type identifier of (info for a_file)
-- display dialog UTI as string
if UTI is in safe_types_list then
open a_file
end if
end repeat
end tell
end adding folder items to


If you'd like this script to handle some additional file types, un-comment the display dialog UTI as string line, drop the file of the desired type into the folder and the script will display the type identifier that you need to add to the safe_types_list list.

AppleScript, Email Announcer

Here's a little AppleScript I wrote to announce new emails in Mail.app. It pauses the iTunes if it's playing, speaks the name of the sender if the message is not marked as junk and then resumes the playback.

on perform_mail_action(info)
    -- see if iTunes is playing
    set itunes_playing to false
    tell application "System Events"
        if (get name of every process) does not contain "iTunes" then
            set itunes_playing to false
        else
            tell application "iTunes"
                if player state is playing then set itunes_playing to true
            end tell
        end if
    end tell

-- pause iTunes if needed
if itunes_playing is true then tell application "iTunes" to pause

tell application "Mail"
set the_messages to |SelectedMessages| of info
repeat with a_message in the_messages
set the_sender to extract name from sender of a_message
if (junk mail status of a_message) is not true then
say "Mail from " & the_sender
end if
end repeat
end tell

-- resume iTunes if it was playing before
if itunes_playing is true then tell application "iTunes" to play
end perform_mail_action


Keep in mind that the script might turn up to be quite chatty depending on the amount of mail that you receive every day. One way of dealing with this is to apply the script only to those emails sent by people in a certain group (Friends, Family, Colleagues and so on). Here's one of my email rules:


Email From Family Rule

October 24, 2005

Refactoring

To Really Improve Your System You Can't Refactor

I firmly believe in constantly improving my code and I see a difference in quality because of that. Seemingly this would put me firmly in the refactoring camp. But it doesn't. Why? Refactoring says you can't break interfaces. That puts me in an awkward position.

Refactoring is all about improving your code and making sure your tests still pass along the way. There could be no single method that works equaly good for everyone and in every situation, though. I once was refactoring the library and at some point just realized that it's going to take far less time if I just rewrote it. And so I did, and the resulting code was only 20% as big and it was all new code. But I still call it refactoring! Because even though I was writing a new code, I still had the existing code to consult with.

What's good about the method, though, is that we can automate some of the routine tasks of refactoring.