Add Terminal 'create and edit' feature to any app

Oct 18, '02 10:03:50AM

Contributed by: cubixe

I love the command line tools that BBEdit ships with. They let you open and create files in BBEdit all from the terminal. For example, typing bbedit -c foo.cppin the terminal will create and open a file called foo.cpp in BBEdit. Using perl and the "open" command in OSX, you can easily create this functionality for any application.

The following code works for Project Builder. I put it in a file called pb in ~/bin (make it executable and type 'rehash' to use) and it works just liked bbedit!

#!/usr/bin/perl

my $create = 0 ;

foreach (@ARGV)
{
if($_ eq "-c") {
$create = 1 ;
}
else {
$filename = $_ ;
}
}

if (! -e $filename )
{
chomp $filename ;
if($create) {
system "touch $filename"; }
else
{
print "$filename doesn't exist. Specify -c to create.n" ;
exit ;
}
}
elsif( $create == 1 )
{
print "$filename already exists. Cannot create.n" ;
exit ;
}

system "open -a /Developer/Applications
/Project\ Builder.app $filename" ;
[The last line is shown on two lines; enter it on one with no spaces.]

I'm no perl expert, so feel free to make any suggestions in the comments on how to shrink or improve this code. The one thing about this versus how bbedit works, or programs such as emacs, is because I use touch to create the file, it is created even if you never add anything to the file and close it immediately.

By the way, this would be pointless for many applications and file types, as open will open it automatically in the application you want. But, if you switch between bbedit, project builder, emacs, etc., you may not want a .cpp file to open in the same application.

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20021018070350273