The MacView

Virtual Instrumentation from a Mac perspective

My Photo
Name:
Location: Pflugerville, TX, United States

Friday, March 08, 2013

More Magic with Post-Build Steps

I'm in a Post-Build magic mood. I was chatting with a LabVIEW user who wanted to have a drag and drop install experience for their LabVIEW built app. My previous post about merging the LabVIEW Runtime Engine into your built app is part of the puzzle. The magic he was looking for was creating one of those spiffy .dmg files with a background image giving instructions to drag-and-drop onto a symlink to /Applications folder.

This gave rise to my DMG Post-Build Step. It's really not meant to be a Post-Build step, but an example of using the DMG and Finder VIs I wrote. You can combine these VIs into an amalgamation of the LabVIEW Runtime Engine Merge, the example DMG step here and your own magic.

The idea is that you can mix and match the parts to do what you need to get done for your project. In the example I've included in the project, it builds a Read/Write .dmg (which is required to arrange it correctly) and then a Read-Only, Compressed .dmg for distribution.

The .dmg tools allow you to create DMGs, mount and unmount them and convert them to other formats. The convert VI allows you to convert any .dmg to two difference compressed formats (pre-10.4 compatible and a 10.4+ that has better compression), Read/Write, ISO (for CD/DVD Burning) and Sparse (disk image size on disk grows as contents of disk image grow).

After creating the .dmg tools, I realized there was some somewhat tricky things I needed to do in the Finder to get things to look just right. Luckily the lingua franca of the Finder is AppleScript, and LabVIEW just happens to ship with a VI to run AppleScripts, at vi.lib/Platform/Run AppleScript Code.vi. Also lucky for us, AppleScript's native path format is Classic Mac (ie Macintosh HD:Users:labview vs POSIX /Users/labview).
Above is the contents of Set Finder Item Position.vi. It calls Open Finder Window.vi to make sure the window is open in the Finder, and Close Finder Window.vi at the end. The Finder is particular about when it will persist changes made via AppleScript. Goofy things like opening and closing windows can make a difference.

Speaking of which, you'll notice that in the example Post-Build VI (Create App DMG.vi), we open the Finder Window (and don't close it) right before we unmount the Read/Write .dmg file. After reading the tea-leaves on the internet (or at least my poor recollection of reading them) reveals that in order for the Finder to persist the position changes made, you need to have the window(s) open when you unmount.
So with all those magic AppleScript incantations and tweaking the sizes and positions until it is just right, when you build, the output directory will have your application (as well as the Preferences and .aliases files) along with a Read/Write .dmg file (helpful for debugging positions, etc.) and the final, compressed .dmg.

When you double-click on the Read/Write .dmg file, you'll need to find the disk to open it (Finder -> Go -> Computer). However the Compressed .dmg file will open it's window when double-clicked. Not only does it open, but it opens in the top-left corner (with a little space from the edge of the screen), it shows my background image (you can have any .jpg file you want there) and my application (with the extension hidden).

My Finder tools do have some limitations (and someone may want to modify/extend them). They only work on Icon View (list, column, etc are not supported). I've hard-coded the following behaviors: no side bar, no status bar, no tool bar, no item info shown, no icon preview shown, icons are not arranged and the label position is at the bottom of the icons. The Set Finder Icon Window Options.vi was intended to give you just the tools you need to create the customary .dmg drag/drop install.

Oh, and I did omit creating the symlink to /Applications folder. I'll leave that as an exercise for the reader, but I'll give you some hints: System Exec.vi and ln -s /Applications /Volumes/Example/Applications, just don't forget to build the path for /Volumes/Example/Applications from the root path passed out of Create DMG.vi and of course, don't forget Path to Command Line String.vi to get it in POSIX format.

Happy app building!







Labels: , , , ,

The views expressed on this website/weblog are mine alone and do not necessarily reflect the views of my employer.