Stairways Software IconStairways Software

Excellence In Software For Over Ten Years – Only with a Mac

Stairways Software Technical Blog Archive

Remembering the path with tcsh in Terminal/Lion

In Lion, the new version of Terminal remembers your Windows and Tabs, including your scroll history, and with bash, also your path. If you use tcsh (as I have for 20 years or so), then it does not remember your path - here is how to fix it.

A little digging finds that bash sets this up in /etc/bashrc, specifically with update_terminal_cwd which it adds to PROMPT_COMMAND. It is careful to only do this for Apple_Terminal and while not inside Emacs. Since I don't use any other terminals (except TouchTerm on my iPhone) and I don't use Emacs, I've ignored that, but keep in mind there may be cases you need to disable this.

The trick is done by outputting a URL in an escape sequence.

In tcsh, you can set an alias to a command that is run immediately before printing your path, so you can set it to print the magic escape sequence using something like this:

alias precmd ~/perl/update_terminal_cwd.pl

update_terminal_cwd.pl duplicates the behaviour of update_terminal_cwd in /etc/bashrc:

#!/usr/bin/perl

my $pwd = $ENV{PWD};
$pwd =~ s! !%20!g;
$pwd = "file://$ENV{HOSTNAME}$pwd";
printf( "\e]7;%s\a", $pwd );

Add the alias to you .login and Terminal will remember your paths in tcsh too.

Posted Sunday, August 7, 2011. Permalink. Post a Comment.

Field Editor Issues

The editing a text field, cocoa uses a shared field editor, which switches text fields as the user tabs around the window. This technique is designed to save resources, and it is unlikely it is needed on modern hardware, but for now we're stuck with it.

The field editor brings up a number of issues, here are some helpful tips for handling them.

There is usually a single shared editor for a window, but the window delegate can provide a custom field editor or different editors for different text fields. To return a custom field editor, use the window delegate method windowWillReturnFieldEditor:toObject:

To test whether a text field is being edited, check [field currentEditor], which returns the field editor or nil if the text field is not being edited.

To get from the field editor to the current text field, you use (NSTextField*)[fieldEditor delegate].

The field editor is added as a subview of the text field. So to determine if a text field is the current first responder, use [textField.window.firstResponder isDescendantOf:textField]. Note that you need to check that textField.window.firstResponder isKindOfClass:[NSView class] first.

Posted Friday, May 6, 2011. Permalink. Post a Comment.

Installing ImageMagick

I've beat my head against installing ImageMagick far too many times. Its a powerful tool very useful for scripted image manipulation, but it has always been a royal pain to install.

The following describes how to install it under Mac OS X 10.6 (Snow Leopard) in 64 bit architecture (if applicable). All of the files are downloaded into ~/unix/install and all the components are installed in ~/unix/local, except for the perl modules, and (annoyingly) some ImageMagick man pages which refuse to honour the prefix, though no doubt there is some other magick (ha!) incantation that would force the man pages to the right location.

Although the code below is written like a script, you can’t (well, certainly shouldn’t) just run it as a script. For one thing, by the time I’ve finished writing this article, one or other of the components will likely have been updated. For another, you really should check that each part compiles and installs before proceeding. Its a slow and painful process, but a lot faster with a plan to follow.

Good luck!

mkdir ~/unix
mkdir ~/unix/install
cd ~/unix/install
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv LDFLAGS -L$HOME/unix/local/lib

curl -O ftp://ftp.remotesensing.org/pub/libtiff/tiff-3.9.1.tar.gz
tar zxf tiff-3.9.1.tar.gz
cd tiff-3.9.1
./configure --enable-shared --prefix=$HOME/unix/local
make
make check
make install
cd ..

curl -O ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng-1.2.40.tar.gz
tar zxf libpng-1.2.40.tar.gz
cd libpng-1.2.40
./configure --enable-shared --prefix=$HOME/unix/local
make
make install
make check
cd ..


curl -O http://www.ijg.org/files/jpegsrc.v7.tar.gz
tar zxf jpegsrc.v7.tar.gz
cd jpeg-7/
./configure --enable-shared --prefix=$HOME/unix/local
make
make check
make install
cd ..

curl -O http://mirror.dknss.com/nongnu/freetype/freetype-2.3.9.tar.gz
tar zxf freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure --enable-shared --prefix=$HOME/unix/local
make
make install
cd ..

curl -O http://internode.dl.sourceforge.net/project/ghostscript/GPL%20Ghostscript/8.70/ghostscript-8.70.tar.gz
tar zxf ghostscript-8.70.tar.gz
cd ghostscript-8.70
./configure --disable-cups --prefix=$HOME/unix/local
make
make install
cd ..

curl -O http://www.vg.kernel.org/pub/mirrors/gentoo/source/distfiles/pkg-config-0.23.tar.gz
tar zxf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure --enable-shared --prefix=$HOME/unix/local CPPFLAGS=-I$HOME/unix/local/include LDFLAGS=-L$HOME/unix/local/lib
make
sudo make install
cd ..

curl -O ftp://ftp.gnu.org/pub/gnu/gettext/gettext-0.17.tar.gz
tar zxf gettext-0.17.tar.gz
cd gettext-0.17
./configure --enable-shared --prefix=$HOME/unix/local CPPFLAGS=-I$HOME/unix/local/include LDFLAGS=-L$HOME/unix/local/lib
make
make install
cd ..

curl -O http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.9.2.tar.gz
tar zxf libiconv-1.9.2.tar.gz
cd libiconv-1.9.2
./configure --enable-shared --prefix=$HOME/unix/local CPPFLAGS=-I$HOME/unix/local/include LDFLAGS=-L$HOME/unix/local/lib
make
make install
cd ..

curl -O http://laotzu.acc.umu.se/pub/gnome/sources/glib/2.20/glib-2.20.5.tar.gz
tar zxf glib-2.20.5.tar.gz
cd glib-2.20.5
./configure --enable-shared --prefix=$HOME/unix/local CPPFLAGS=-I$HOME/unix/local/include LDFLAGS=-L$HOME/unix/local/lib --with-libiconv
make
make install
cd ..

curl -O  http://www.imagemagick.org/download/delegates/liblqr-1-0.1.0-1.tar.gz
tar zxf liblqr-1-0.1.0-1.tar.gz
cd liblqr-1-0.1.0
./configure --enable-shared --prefix=$HOME/unix/local GLIB_CFLAGS=-I$HOME/unix/local/include GLIB_LIBS=-L$HOME/unix/local/lib
make
make install
cd ..

[sudo is needed to install some man pages and the perl modules]

curl -O  ftp://mirror.aarnet.edu.au/pub/ImageMagick/ImageMagick-6.5.7-6.tar.gz
tar zxf ImageMagick-6.5.7-6.tar.gz
cd ImageMagick-6.5.7-6
./configure --enable-shared --prefix=$HOME/unix/local CPPFLAGS=-I$HOME/unix/local/include LDFLAGS=-L$HOME/unix/local/lib --with-perl
make
sudo make install
cd ..

Posted Tuesday, November 10, 2009. Permalink. Post a Comment.

Open Finder Selection in Specific Application

Recently on the Keyboard Maestro User Group someone asked how to open the finder selection in a specific application. Keyboard Maestro does not have a direct action for this, but you can easily add an Execute AppleScript text action to do this. Here is a simple script to do this.

tell application "Finder" to set finderfiles to selection as list
set finderaliases to {}
repeat with i in finderfiles
    set a to i as alias
    set finderaliases to finderaliases & a
end repeat
tell application "BBEdit"
    activate
    open finderaliases
end tell

Posted Sunday, July 12, 2009. Permalink. Post a Comment.

Creating an NSImage from an NSView

There are many different techniques for creating an NSImage from an NSView - finally I found one that works well!

There are various techniques:

  • lockFocus on the image, and then drawRect: the view

But this only draws the root view, not subviews.

  • lockFocus on the image, and then drawRect: the view and draw subviews

But its nigh on impossible to actually get the transformations correct for adjusting to the subview co-ordinate systems.

  • lockFocus on the view, and use initWithFocusedViewRect:

But this only gets the visible parts of the view.

  • use dataWithPDFInsideRect

But this is quite slow and has some drawing artefacts when the resulting image is displayed on the screen.

I finally found Gerd Knops’ post which yields a good result.

- (NSImage *)imageWithSubviews
{
 NSSize mySize = self.bounds.size;
 NSSize imgSize = NSMakeSize( mySize.width, mySize.height );
 
 NSBitmapImageRep *bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
 [bir setSize:imgSize];
 [self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
 
 NSImage* image = [[[NSImage alloc]initWithSize:imgSize] autorelease];
 [image addRepresentation:bir];
 return image;
}

I am combining this with Matt Gemmell’s MGViewAnimation with good results for use in alpha fading views under 10.5.

Posted Tuesday, April 21, 2009. Permalink. 1 Comments.

Technical Blog

I get a lot of value out of some of the technical blogs I read, such Mike Ash’s NSBlog and Matt Gallagher’s Cocoa with Love, as well as from sites like CocoaDev.

It is always good to return the favor and put out some useful information and in the past I have put information up on my personal web site, but I thought it was about time to create a proper developer oriented blog as a place to post technical information which is not directly related to our customers but which is related to what we are doing as a company.

Hence this blog.

I hope to post useful information related to whatever I am doing, especially related to anything that proves less that obvious on the first attempt and resists a google search to easily find the answers. Most of it will likely be related to Mac or iPhone programming, but occasionally it may stray in to other technical areas.

Posted Sunday, March 29, 2009. Permalink. Post a Comment.

Syndicated Content for Technical Blog

Other Blogs

Buy Now

User Database

Stairways