Joe Maller.com

Setting icon position and window size on disk images

Setting up icon placement and window sizes on a disk image isn’t as easy as it should be. Here’s part of my solution for automating a designed disk image with AppleScript.

Fixing Window Size

According to this thread on Apple’s Installer-dev list, window size isn’t written until there is some user interaction, specifically clicking either the green zoom button or the little resizing thingie in the lower right corner. I was unable to get UI Scripting to dependably click the resizing corner, but I did notice that two clicks on the zoom button, after setting a window’s size, toggles between the set size and the zoomed size. So I specified my desired window size, then had System Events click the zoom button (button 2) twice:

set bounds of window 1 to {50, 75, 580, 680}
tell application "System Events" to tell process "Finder" to click button 2 of window 1
tell application "System Events" to tell process "Finder" to click button 2 of window 1

Forcing .DS_STORE to Record Icon Positions

The Finder’s .DS_STORE files are a bit of a mystery. The binary format is closed and there is no programatic way of forcing these files to update. Some smart people have suggested some harebrained workarounds for dealing with this problem, but I wanted something cleaner and more flexible.

My solution was to start fresh by obliterating any .DS_STORE files that might be on the disk image; this appears in the window-layout script before doing anything else:

do shell script "rm " & quoted form of POSIX path of dmgPath & ".DS_STORE"

Next the window is sized, icons are positioned and background image is assigned. Then the script waits to eject the disk* until the .DS_STORE file has been created (open in Script Editor):

set waitTime to 0
set ejectMe to false
repeat while ejectMe is false
    delay 1
    set waitTime to waitTime + 1
    if (do shell script "[ -f " & quoted form of POSIX path of dmgPath & ".DS_STORE ]; echo $?") = "0" then set ejectMe to true
end repeat
log "waited " & waitTime & " seconds for .DS_STORE to be created."
eject dmgPath

On my MacBook Pro, it usually takes 4 seconds for the file to appear. First deleting the file makes sure that the resulting file contains all the new placement information.

Next the disk image gets run through DropDMG and is ready for uploading. I’m ejecting the disk image before converting because I’m getting filesystem errors when I try converting while the source DMG is still mounted. I don’t remember that happening in the past, but it’s probably a good idea to eject first anyway.


At last, Widescreen Mail.app

Aaron Harnly’s Letterbox addon for Apple’s Mail.app:

Letterbox is a plugin for Apple’s Mail.app that takes advantage of your widescreen monitor. It rearranges the interface into three vertical columns —so the message pane is to the right of the message list, rather than below.

This makes me so, so happy. I had this working in previous versions of OSX and it was one of the first things I tried after upgrading to 10.4.. Seeing more messages is a big productivity boost for me because things that need attention don’t get pushed out of sight.
I’m glad someone finally got it working, thank you Aaron. I hope he does eventually post the source code, I’m curious to see how he did it (and if it changed from the Ars posting).

update URL updated.


EXIF and the Unix Strings command

I got an email over the weekend pointing out a bug in my iPhoto Date Reset if an original image contained a single-quote in its name. Most all of my iPhoto images were imported from the camera, so I hadn’t seen this before, but I’m pretty sure I’ve already gotten it fixed.

While fixing that, I did a little revising of the EXIF sniffing script. I was using a one-line Perl snippet to scrape the date out of the first kilobyte of the file. Here’s the command broken across several lines

 perl -e 'open(IMG, q:[ABSOLUTE PATH}:);
 read(IMG, $exif, 1024); 
 $exif =~ s/\n/ /g; 
 $exif =~ s/.*([0-9]{4}(?::[0-9]{2}){2} [0-9]{2}(?::[0-9]{2}){2}).*$/$1/g;
 print STDOUT $exif;'

That worked, but perl one-liners usually need to be enclosed in single-quotes, since AppleScript was filling in the path, single-quotes in the name broke the script. I’m not that fluent in Perl, so there’re probably better ways of doing that.

But then I stumbled across the Unix Strings command. This basically does most of what I was doing. It scrapes a binary file (meaning non-text) and extracts anything that seems to be a string. The output from JPEGs often contains a bunch of gibberish, but right above the gibberish is every unencoded string from the EXIF header.

Using strings, sed for the pattern and head to trim, that somewhat convoluted perl script became this trim little shell script:

 strings [ABSOLUTE PATH] | sed -E -n '/([0-9]{4}(:[0-9]{2}){2} [0-9]{2}(:[0-9]{2}){2})/p' | head -n1

They’re both essentially instant on my computer so I’m not going to bother building a test to figure out which is actually faster.


AppleScript source links from TextMate

AppleScript’s URL Protocol Support allows the full source code of an AppleScript to be shared through an encoded URL. Here’s a short little TextMate Command for converting AppleScripts to encoded URLs.

Create a new command in TextMate’s AppleScript bundle with the following code:

#!/usr/bin/env ruby -KA -rcgi
src = CGI.escape(STDIN.read)
src = src.gsub('+', '%20')
src = 'applescript://com.apple.scripteditor?action=new&script=' + src
`echo -n "#{src}" | pbcopy`
print "The URL encoded AppleScript was copied to the clipboard"

There’s no reason this idea can’t be used for all sorts of stuff. So why not make it easier, it’s not AppleScript, but here’s that code in Script Editor for easier copying.

This is exceptionally fast, much faster than Apple’s provided encoding script. As an extreme example, converting the 558 lines of my iPhoto Date Reset script took just under a minute using Apple’s script. The little Ruby script in TextMate does it instantly. (running the AppleScript in Script Editor with the Event Log Window open took nearly 7 minutes.)

Update: I committed this command to the TextMate Bundles repository and it’s now included in the default set of Bundles shipping with TextMate. (And slightly improved by other TextMate bundle developers)

Share |

link: May 24, 2006 1:43 pm
posted in: misc.
Tags: , , ,

RFC 822 Dates with AppleScript

Here’s a little AppleScript subroutine which converts date objects into correctly formatted RFC date strings: RFCdate() (click to open in Script Editor)

This is a timesaver for anything related to RSS, which requires dates be in the RFC 822 format, ie. Wed, 24 May 2006 01:30:22 -0400


Avon Walk for Breast Cancer [research]

My cousin Sarah, a breast cancer survivor, will be participating in her first Avon Walk for Breast Cancer on July 8-9 in San Francisco, she has been using the http://thebustboosters.com/breast-actives-full-product-review products and she has recovered pretty well. She already met her fundraising goals, but it’d be great if she raised a little more.

Share |

link: May 10, 2006 10:36 pm
posted in: misc.

Hey Adobe

Quark’s got a Universal Binary Beta, what have you got??

Working in CS2 on a MacBook Pro is slow and painful. We will probably need new hardware before CS3 ships, that hardware will be Intel Macs. Hurry up or someone’s gonna take your market.

Share |

link: May 09, 2006 1:56 pm
posted in: Mac OS X


« Previous PageNext Page »