#!/usr/bin/perl -s #Written by: Jim Turner 3/2001. # # usage: invoke.pl application [args] # # Invokes application and passes the contents of the clipboard as it's # 1st argument. This allows one to highlight a file and click a Wharf # button and have the app launched with the highlighted file. # # -e option means clipboard content must exist as a valid file. # -d option means chdir to the pgm's directory before invoking. # -d=/dir option means chdir to that directory b4 invoking. # -d=* option means chdir to the .cdout directory b4 invoking (useful for Wharf) # -d=ARGV[#|n] means chdir to path given in $ARGV[#]|$ARGV[$#ARGV] to command # -n option is same as invoking as 'invokendir', see below. # -s option is same as invoking as 'invokstr', see below. # # usage: invokestr.pl application [string] # # Similar to invoke.pl, but [string] argument is not appended to the default # path and treated as a file, rather it is just passed as a string. Useful # for apps like "tkman" that expect a string rather than a filename as their # argument. # # usage: invokendir.pl 0|1|2|3|4 application [args] # # Invokes application and either switches to the selected directory and # or passes the contents of the clipboard as it's # last argument if it is a valid directory. This allows one to # highlight a directory and click a Wharf button and have the app # launched in the highlighted directory. Good use is gqview or # konqueror. If highlighted directory not a valid path, then .cdout # is read, if that fails, the "current-working-directory" is used. # # methods: # 0 invokes application with the directory passed to it. # 1 does a chdir, then invokes app with no arguments # 2 does a chdir, then invokes app with a "."; # 3 does a chdir, then invokes app with the directory passed to it. # 4 does a chdir, then invokes app with no args (digikam, et. al.). # # In order to enable the ".cdout" current directory to work (the current # directory you most recently switched to among all shells - and for Wharf's # purposes, add the following line to your /etc/bashrc or .bashrc file: # #function cd () { builtin cd $1 >/dev/null 2>&1 || builtin cd "$1"; echo $PWD >~/.cdout; shift; } use Cwd; use Tk; my $DEBUG = 1; #WRITE LOG FILE IF DEBUGGING. my $clipboard; my $cwd = cwd(); $s ||= 0; $s = 1 if ($0 =~ /invokestr/o); $n ||= 0; $n = 1 if ($0 =~ /invokendir/o); `export CLIPBOARD_FID=/tmp/clipboard`; #NEXT 2 NEEDED BY TK-APPS DUE TO TK-BUG. $ENV{CLIPBOARD_FID} = '/tmp/clipboard'; eval {$clipboard = MainWindow->new->SelectionGet;}; chomp ($clipboard); $clipboard = '' if ($clipboard !~ /\S/o); if ($DEBUG) { open T, ">/tmp/invoke.log"; print T "-0: cb=$clipboard=\n"; } #1unless ($clipboard =~ /\S/o) #1{ #1 if (open IN, $ENV{CLIPBOARD_FID}) #1 { #1 while () #1 { #1 $clipboard .= $_; #1 } #1 close IN; #1 } #1 print T "-0.5: NO PRIMARY, TRIED $ENV{CLIPBOARD_FID}; CB=$clipboard=\n" if ($DEBUG); #1} #1$clipboard = '' if ($clipboard !~ /\S/o); #APPEND PROPER DIRECTORY TO CLIPBOARD CONTENT UNLESS "-s" (string) OR CLIPBOARD IS #ALREADY AN ABSOLUTE PATH OR IS A URL: if ((!$clipboard && $n) || ($clipboard && !(-d $clipboard) && !$s && ($clipboard !~ m#^(?:\/|https?\:|ftp\:|file\:)#o))) { print T "-1: do it!\n" if ($DEBUG); if (-r "$ENV{HOME}/.cdout" && open(F,"<$ENV{HOME}/.cdout")) { $_ = ; chomp; close F; } $_ ||= $cwd; $_ .= '/' unless (m#\/$#o); if ($d eq '*') { $ENV{PWD} = $_ if (chdir $_); print T "-1a: chdir=$_= cb=$clipboard=\n" if ($DEBUG); } else { $clipboard = $_ . $clipboard; # unless ($s); print T "-1b: cb=$clipboard=\n" if ($DEBUG); } } $clipboard =~ s/[\r\n]//gso; print T "-1 cb=$clipboard= pgm=$0=\n" if ($DEBUG); my ($method, $chdir); $method = shift(@ARGV); if ($n) #(INVOKENDIR) - 0: cmd ; 1: cd ; cmd; 2: (1; 0); { print T "-1.1: method=$method= cb=$clipboard= pwd=".cwd()."=\n" if ($DEBUG); $clipboard =~ s#\/[^\/]+$#\/#o unless (-d $clipboard); #USER HIGHLIGHTED A FILE, REDUCE TO IT'S PATH print T "-1.1a: cb:=$clipboard=\n" if ($DEBUG); unless (-d $clipboard) #STILL NOT A VAILD PATH, USE .cdout, IF AVAILABLE: { if (-r "$ENV{HOME}/.cdout" && open(F,"<$ENV{HOME}/.cdout")) { $_ = ; chomp; close F; } $_ ||= $cwd; $_ .= '/' unless (m#\/$#o); $clipboard = $_; print T "-1.1b: no valid directory highlighted; cb:=$clipboard=\n" if ($DEBUG); } chop($clipboard) if ($clipboard =~ m#[^\/]\/$#o); if ($method) { $ENV{PWD} = $clipboard if (chdir $clipboard); print T "-1.2: pwd now =".cwd()."= cb=$clipboard=\n" if ($DEBUG); } $clipboard = ' ' if ($method == 1); $clipboard = '.' if ($method == 2); print T "-2: m=$method= cb=$clipboard=\n" if ($DEBUG); } else #NORMAL INVOKE OR INVOKESTR: { print T "-2b: method=$method= cb=$clipboard= d=$d=\n" if ($DEBUG); $chdir = ''; if ($d == 1) #1ST, SWITCH TO THE PROGRAM'S DIRECTORY B4 INVOKING PROGRAM: { $chdir = $1 if ($method =~ s#^(.+\/)([^\/]+)$#\.\/$2#); if ($chdir) { $ENV{PWD} = $chdir if (chdir $chdir); } } elsif ($d) #CHDIR TO A DIRECTORY BEFORE B4 INVOKING PROGRAM { if ($d eq '*') #FETCH "CURRENT" DIRECTORY FROM .CDOUT AND USE THAT: { if (-r "$ENV{HOME}/.cdout" && open(F,"<$ENV{HOME}/.cdout")) { $_ = ; chomp; close F; } $_ ||= $cwd; $_ .= '/' unless (m#\/$#o); $ENV{PWD} = $_ if (chdir $_); print T "-2c1: d=* chdir=$_=\n" if ($DEBUG); } elsif ($d =~ /^ARGV(.)/o) { my $one = $1; my $arg = ($one =~ /\d/o) ? $one : $#ARGV; #print T "-???- 1=$one= arg=$arg= args=".join('|',@ARGV)."=ARGV1=$ARGV[1]=\n" if ($DEBUG); $one = $ARGV[$arg]; #print T "-???- 1=$one= arg=$arg=\n" if ($DEBUG); $one =~ s#\/[^\/]+$##; # unless (-d $one); print T "-2c2: chdir=$one=\n" if ($DEBUG); #print T "-???- cddir=$one=\n" if ($DEBUG); $ENV{PWD} = $one if (chdir $one); } else #CHDIR TO THE SPECIFIED DIRECTORY (-d=/some/dir) { print T "-2c3: chdir=d=$d=\n" if ($DEBUG); $ENV{PWD} = $d if (chdir $d); } } } if ($method == 4) #INVOKE COMMAND W/O USING THE CLIPBOARD CONTENT AS AN ARGUMENT: { if ($DEBUG) { print T "-4: method=4: argcnt=$#ARGV= args=".join('|',@ARGV)."=\n"; close T; } if ($#ARGV > 0) { exit (exec(join(' ',@ARGV))); } else { exit (exec($ARGV[0])); } } elsif ($n) #INVOKE COMMAND W/CLIPBOARD AS LAST ARGUMENT (INVOKENDIR) { if ($DEBUG) { print T "-3: invokendir: argcnt=$#ARGV= args=".join('|',@ARGV)."= cb=$clipboard=\n"; close T; } if ($#ARGV > 0) { exit (exec(join(' ',@ARGV,$clipboard))); } else { exit (exec($ARGV[0], $clipboard)); } } elsif ($clipboard && ($s || -r $clipboard)) { if ($DEBUG) { print T "-5: cb=$clipboard= INVOKESTR || -r clipboard! argcnt=$#ARGV= method=$method= args=".join('|',@ARGV)."=\n"; close T; } if ($#ARGV >= 0) { exit (exec(join(' ',$method,@ARGV,$clipboard))); } else { exit (exec($method, $clipboard)); } } else { if ($DEBUG) { print T "-6: argcnt=$#ARGV= method=$method= args=".join('|',@ARGV)."=\n"; close T; } if ($#ARGV >= 0) { exit (exec(join(' ',$method,@ARGV))); } else { exit (exec($method)); } } exit (0); __END__