[TxMt] Inserting a File Path

Paul McCann pmccann at maths.adelaide.edu.au
Wed Feb 9 05:26:38 UTC 2005


Greetings all (from a relatively new BBEdit->TextMate convert)

Is there an easy way to insert a file/directory path into a TextMate window?
I can do it using the slightly ugly applescript hack below, "Insert Finder
Selection", but it'd be nice to drag-then-hit-command-while-dragging or some
such.

Just for fun I've included a couple of Perl commands for reading in a file
"Perl Infile" (it's up to you what you do with it!) and for writing to a
file "Perl Outfile". They try to be a little bit helpful: if you've selected
text in TextMate the commands assume it's a filename and wrap around it,
otherwise CocoaDialog is pulled up and you must select a file. in "Outfile"
you can also select a directory: in this case it prompts for a filename to
write to. See Eric's earlier post about CocoaDialog (http://cocoadialog.sourceforge.net/)

You will need to change the $CD variable used in these scripts if you choose
to install it somewhere other than /Applications .

Regards,
Paul

------------------------------------------------------------------------
Insert Finder Selection:
------------------------------------------------------------------------
Save: Nothing
Command:
osascript<<END
set linefeed to "
"
tell application "Finder"
 set x to the selection
 if the length of x is 0 then
   return ""
 end if
 set mylist to {}
 repeat with i from 1 to the number of items in x
   set y to item i of x as alias
   set mylist to mylist & (POSIX path of y)
 end repeat
end tell
END

Input: None
Output: Insert after selected text
Key Equivalent: command-option-ctrl F
------------------------------------------------------------------------


------------------------------------------------------------------------
Perl Infile
------------------------------------------------------------------------
Save: Nothing
Command:
perl -e '
my $CD="/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";
my $TMD=$ENV{'TM_DIRECTORY'};
my $selection=$ENV{'TM_SELECTED_TEXT'};
my $file;
if ($selection){
($file=$selection)=~s/\s+$//; # kill any trailing space 
}else{
my $rv=`$CD fileselect --text "Select a file" --no-newline --with-directory "$TMD"`;
chomp($file = $rv);
}
exit unless $file; # user cancelled
print <<HERE
my \$infile = "$file";
open IN , "\$infile"   or   die "Input error: \$infile\\n\$!";
while ( <IN> ) {

}
HERE
'

Input: None
Output: Replace selected text
Key Equivalent: command-option-ctrl I
------------------------------------------------------------------------

------------------------------------------------------------------------
Perl Outfile
------------------------------------------------------------------------

Save: nothing
Command:
perl -e '
my $CD="/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog";
my $TMD=$ENV{'TM_DIRECTORY'};
chomp(my $rv=`$CD fileselect --text "Select a file" --no-newline --select-directories --with-directory "$TMD"`);
if (-d $rv){ # dir selected: need to append name
  my $out=`$CD standard-inputbox --text "$TMD" --informative-text  "Enter the name of the file" --no-cancel`;
  my ($name)=($out=~m/.*\n(.*)/m);
  $rv.="/$name";
}

chomp(my $file = $rv);
exit unless $file; # user cancelled
print <<HERE
my \$outfile = "$file";
open OUT , ">\$outfile"  or  die "Outfile: \$outfile\\n\$!";
select OUT;     # print to OUT filehandle by default

HERE
'             

Input: None
Output: Replace selected text
Key Equivalent: command-option-ctrl O
------------------------------------------------------------------------




More information about the textmate mailing list