#!/usr/bin/env perl # This software is released under the same license and terms as perl # itself. # # This is a script for launching applications on OS X from the command line # by grepping through a cache file. use strict; use warnings; use File::Find; use Tie::File; my $cache_path = $ENV{HOME} . "/.launch_cache.txt"; my @roots = ("/Volumes/Macintosh HD/Applications"); sub WantedFiles { # exclusions return unless (-d); return unless (/\.app$/); print CACHE $File::Find::name . "\n"; return $File::Find::prune = 1 if (/\.app$/); } sub execCommand { my $path = shift; exec "open", "-a", $path; } if ($ARGV[0] =~ m/^--/) { $ARGV[0] =~ s/^--//; if ($ARGV[0] eq "crawl") { open CACHE, ">$cache_path"; find(\&WantedFiles, @roots); exit; } shift @ARGV; } my $search_for = $ARGV[0] . ".*\.app\$"; tie my @cache, "Tie::File", $cache_path, memory => 500; my @matches = grep(/$search_for/i, @cache); # If no matches, give up unless (@matches) { print "No matches.\n"; exit; } # If one match, take immediate action if ($#matches == 0) { execCommand($matches[0]); } # If multiple matches, prompt for selection my $counter = 1; foreach (@matches) { my $padded = sprintf("%3s", "$counter)"); print $padded; print " " . $_ . "\n"; $counter++; } print "Which one? "; my $selection = ; chomp($selection); if ($selection eq 'q') { exit; } execCommand($matches[$selection - 1]);