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 ------------------------------------------------------------------------
On Feb 9, 2005, at 6:26, Paul McCann wrote:
Is there an easy way to insert a file/directory path into a TextMate window?
You could make a drag command that does this. Although this wasn't the intent of the system. Basically (to also address Eric's recent letter ;) ) a drag command is a normal command executed when a file is dropped on TextMate (I do consider changing this to just a new activation method).
A drag command is chosen based on (current) document extension and file extension (of dropped file), so you can't make a catch all, and currently the document extension is not exposed in the bundle editor, because I want to use the (forthcoming) scope system instead.
You can check qualifiers in the command, these are in the TM_MODIFIER_FLAGS variable (and can be any combination of: SHIFT|CONTROL|OPTION|COMMAND). But there is no way to pass on handling of the drop when the drag command has been chosen.
So basically, you can use this, but you'll lose the default behavior for drops, and you'll have to specify all the file types you want to handle. There are more details about variables and property list keys in the release notes.
Drag commands were introduced to handle the case where binary files (like images) where dropped onto documents, and a command could generate the proper thing to insert (like an <img> tag with dimensions). In their current iteration I'm not to fond of them, because they are sort of limited.
Adding that TextMate just inserts the filename dropped when holding down a qualifier sounds like something that I could just do as a built-in function.
Hi Allan,
[nice discussion of drag commands elided]
Adding that TextMate just inserts the filename dropped when holding down a qualifier sounds like something that I could just do as a built-in function.
Thanks: It's definitely something I learnt to like about "the old editor", and makes scripting tasks that little bit slicker.
Best wishes, Paul