Uli's Web Site
[ Zathras.de - Uli's Web Site ]
Other Sites: Stories
Pix
Abi 2000
Stargate: Resurgence
Lost? Site Map!
 
 
     home | blog | moose | programming | articles >> blogging

 Blog Topics
 
 Archive
 

15 Most Recent

 Less work through Xcode and shell scripts
2011-12-16 @600
 
 iTunesCantComplain released
2011-10-28 @954
 
 Dennis Ritchie deceased
2011-10-13 @359
 
 Thank you, Steve.
2011-10-06 @374
 
 Cocoa Text System everywhere...
2011-03-27 @788
 
 Blog migration
2011-01-29 @520
 
 All you need to know about the Mac keyboard
2010-08-09 @488
 
 Review: Sherlock
2010-07-31 @978
 
 Playing with Objective C on Debian
2010-05-08 @456
 
 Fruit vs. Obst
2010-05-08 @439
 
 Mixed-language ambiguity
2010-04-15 @994
 
 Uli's 12:07 AM Law
2010-04-12 @881
 
 Uli's 1:24 AM Law
2010-04-12 @874
 
 Uli's 6:28 AM Law
2010-04-12 @869
 
 Uli's 3:57 PM Law
2010-04-12 @867
 

More...

Blog

This is a collection of short articles or just phrases about everything from books and movies to web design and usability to programming.
 
This weblog is also available as an RSS Feed.
Subscribe to this RSS Feed
Less work through Xcode and shell scripts
Update: If you like this, you can find more scripting vaguely related to Xcode here.

Like most programmers, I'm not much a fan of repetitive work. I like to do the real work myself, while leaving the computer to take care of the drudgework. Among the most handy tools for this are scripts of all kinds. Having tried to fight AppleScript for a while, I finally realized that, while they may be uglier, shell scripts are a much better tool for this task. Here's a selection of my favorite shell scripts:

Delete Subversion folders

I use Subversion to keep track of my source code and to make it easy for me to undo any bigger changes I make. Sadly, Subversion litters all folders with its ".svn" folders. As of Xcode 1.5, when I add a folder reference to my "Resources" group, Xcode will also copy along those ".svn" folders, which are completely useless to end users and double the size of the executable. So, I've been adding the following bash script to a Shell Script Files build phase in most of my Xcode projects:

sudo find -d "${BUILD_DIR}/${PRODUCT_NAME}.app/Contents/Resources/" -name ".svn" -exec rm -r '{}' \; -print

This scans the generated application's "Resources" directory for any ".svn" folders and deletes them. The -d option is important here: It causes a depth-first traversal, meaning that if the ".svn" folder contains another file of the same name, it will be deleted first. Otherwise you get odd error messages because the deeper file has been deleted by the time find gets to it.

Auto-generating a Help Index

To get an index for your Help Book, and to be able to bring up anchors directly from code, you need to run the Apple Help Indexer on it. Since this is something I easily forget, I use the following script in another Shell Script Files build phase:

open -a /Developer/Applications/Utilities/Apple\ Help\ Indexing\ Tool.app ${BUILD_DIR}/${PRODUCT_NAME}.app/Contents/Resources/${PRODUCT_NAME}\ Help

or on Tiger:

open -a /Developer/Applications/Utilities/Help\ Indexer.app ${BUILD_DIR}/${PRODUCT_NAME}.app/Contents/Resources/${PRODUCT_NAME}\ Help


Note that open launches the indexer asynchronously, which means the build continues (and could finish) while the indexer is running. So, you may have to build the app a second time to make sure you don't get the old index.

Using PHP for your Help Books

One problem with help books is that they are restricted to being plain HTML files plus AppleScripts. There's really no nice way to "#include" a title or navigation area or a common design. PHP would let you do things like that. But PHP files need to be displayed through Apache, with PHP turned on in the httpd.conf. This would require you to install your help book in the user's Sites folder, mess with Apache's configuration, restart Apache and view your help through Safari (which doesn't have Help Viewer's great search feature, nor its facilities for integrating with your program).

But luckily, Apple started shipping along the new PHP command-line tool with MacOS X 10.3. So, if you can live with static content, you can generate HTML files from your PHP scripts through a simple command:

php script.php > page.htm

Of course, we want to automate this. We can use the find command to run this on all php files in a particular folder. But sadly, we can't have a redirect operator (">") in the -exec parameter to find. So, create a new shell script file "php2html.sh" and write the following script into it and chmod +x it:
#! /bin/bash
php "$1" > "$1.html"
This script will take a PHP file path as its parameter and create an html file with the executed script's output next to it. Once that's done, all you need is a shell script build phase:
dir="./${PRODUCT_NAME} Help/"
find -d "$dir" -name '*.php' -exec "${dir}php2html.sh" '{}' \; -print
And after that, maybe another call to find like above that deletes all the PHP source files after copying, or that copies everything but PHP files from the project directory. Neat, huh? Just remember to run your help indexing script after this one so there is something to be indexed.

Including the Subversion Revision in your App

I like to have the current SVN revision number somewhere in my app so there's a way to distinguish copies of the app even if I forget to bump up the version number. To do that, I use the following script:

#! /bin/bash

echo -n "Finding revision in "
pwd
revnum=`/usr/local/bin/svnversion . | cut -f '2' -d ':'`
# Now write the constant declaration to the file:
echo "#define SVN_VERSION \"$revnum\"" > svn_version.h
echo "Wrote revision $revnum to svn_version.h"

This creates a file named "svn_version.h" in the project's folder that contains the statement #define SVN_VERSION "46". I.e. you get a string constant that you can use in your C files wherever you want to display the current revision. It's important that this is a string, as a modified working copy gets a version number like "46M", which would cause trouble if you use an int.

Build and Upload File for Deployment

I also have a neat little shell script that switches my project's build style to "Deployment", builds it, compresses it and uploads it to a web server:

#! /bin/bashecho '===== BUILDING FILIE FOR DEPLOYMENT ====='
cd `dirname $0`
xcodebuild -project Filie.xcode -buildstyle Deployment clean build

cd build/
echo '===== CREATING ARCHIVE ====='
tar -czf Filie.tgz Filie.app

echo '===== UPLOADING ARCHIVE ====='
curl --upload-file Filie.tgz ftp://home-up.t-online.de/

echo '===== FINISHED ====='

Note that this script uses Tar/GZip, and thus can't cope with resource forks (though I've heard rumors that Tiger's command line tools have been changed to fix this). Also, you can use man curl to find out what parameters to include to authenticate with the FTP server to which you're uploading if your ISP doesn't pre-authenticate you like mine does.

I've put most of these scripts into files in my central library so I can just call them from Xcode, and to avoid code duplication that would make maintenance hard. I'd be interested in hearing what kinds of scripts you are using.


Read More...2011-12-16 @600
iTunesCantComplain released
(...)
Read More...2011-10-28 @954
Dennis Ritchie deceased

Apparently, a few days ago, Dennis Ritchie, the "R" in "K&R", co-creator of the C programming language has died.

Thank you for laying the groundwork for our profession, Mr. Ritchie.
Read More...2011-10-13 @359

Thank you, Steve.

Thank you, Steve.

[Screenshot of the Apple web site at the day of Steve Jobs's death]

We'll take it from here.


Read More...2011-10-06 @374
Cocoa Text System everywhere...
Sometimes, you need to draw text with more control than an NSTextField or NSTextView will let you do, and sometimes you need better performance than the NSStringDrawing category will provide. And maybe you need to draw text into a CGContext or even inside a Carbon application. (...)
Read More...2011-03-27 @788

 
Created: 2004-03-30 @182 Last change: 2011-12-16 @600 | Home | Admin | Edit
© Copyright 2003-2024 by M. Uli Kusterer, all rights reserved.