Version 11 of Droplets

Updated 2006-04-12 18:42:34

What is a droplet?

It is a way to drag and drop a file(s) or directory(ies) onto a tcl program which can then pick up the file/directory names using the standard argc and argv command line argument variables.


ET Here is how one goes about this on windows (w2k and up, not w9x):

Create a text file with a .bat extention (cp. DOS BAT magic), and prefix the tcl code with

 ::if 0 {
 start wish "%~f0" %* 
 exit
 }

 Tcl/tk code follows....

Note: This assumes you have wish in your path. If you install ActiveState's tcl/tk, this should set the path for you.

AET 12apr06 If you want to use TclKit.exe instead of wish, a little jiggery pokery is required, because you cannot use

 tclkit myScript myArguments

the myArguments are simply lost. I save the list to an environment variable, then pick it up again within Tcl. For example;

 ::if 0 {FIRST THE BATCH SCRIPT
   @echo off
   if not {%1}=={} (
     set myFiles=%*
     start /min tclkit "%~f0"
     exit
   ) else (
     echo No arguments supplied; You probably didn't drop any files onto the batch file.
     pause & exit
   )
 }
 # NOW TCL STARTS
 wm wi .
 foreach f [string map {\\ \\\\} $env(MYFILES)] {append fileList "\n" $f}
 tk_messageBox -message "You dropped these files: $fileList"
 exit

NOTES:

The batch script sets an environment variable (only within it's own shell) which is lost when it closes. The variable is a space delimited list (quoted to preserve spaces) of dropped files. Another shell is started within this shell before it closes, so inherits the environment. Tclkit - started within this child shell - can read it with $env(MYFILES). Note that the variable name must be capitalised, regardless of the case in the saved variable.

Of course, a minimal script only needs lines 4,5,6; I prefer the minimal;

  if not {%1}=={} @echo off & set myFiles=%* & start /min tclkit "%~f0" & exit

The /min switch tends to minimise the ugly DOS box that briefly appears.

Note the exit at the end of Tcl. if you leave this out, the instance of tcl will not close. You will see see them accumulate in your process list in Task Manager.


Here are 2 examples,


2clip.bat

This simply lets one drag/drop a file onto the batch file and the full path to the file is placed into the clipboard. It will put up a dialog box because the clipboard gets cleared when the program exits. It will also just quit in 30 seconds as well. So, before the 30 seconds are up, you can paste the full name of the file into, say, a console windows.

 ::if 0 {
 start wish "%~f0" %* & exit
 }
 wm wi .
 if {$argc > 0} {
         clipboard clear
         set f [lindex $argv 0]
         clipboard append $f
         after 30000 exit
         tk_messageBox -message "Clipboard:\n$f" 
 }
 exit

pic2tcl.bat

Drag/drop a .gif or .jpg file(s) onto the batch file and it will create a file of the same path and name but with a .tcl extension that can be used as an inline image.

 ::if 0 {
 start wish "%~f0" %* & exit
 }
   package require base64
   proc trans {arg} {

     set file $arg

     set fd [open $file r]
     fconfigure $fd -translation binary
     set rawdata [read $fd]
     close $fd
     set fd [open [file root $arg].tcl w]
     set b64data [base64::encode $rawdata]
     puts $fd "image create photo -data {\n$b64data\n}"
     close $fd
   }


  wm wi .

  if {$argc > 0} {
    foreach f $argv {
      set f [file normalize $f]
      #tk_messageBox -message "Doing something with \n$f"
      trans $f
    }
  }
  tk_messageBox -message Done.
  exit

PR I chose a two files approach to turn Tcl/Tk scripts into Droplets on Windows (XP). The BAT file is very compact:

Gray2Jpeg.bat

  @echo off

  C:
  chdir %HOMEPATH%\Desktop
  chdir

  start "Gray2Jpeg" /B "%HOMEDRIVE%%HOMEPATH%\hier\bin\Gray2Jpeg.tcl" %1

All meaningful things happen in the TCL file:

[email protected]

  #!env wish

  proc Gray2Jpeg {} {
  # the command line to execute
    global gFilNam gDir gRows gCols gDepth gArgs
    set lOWD [ pwd ]
    cd $gDir
    eval exec env convert.exe -depth $gDepth -size $gRows\x$gCols $gArgs $gFilNam $gFilNam\.jpg
    cd $lOWD
    WriteConf
  }

  proc ChooseFile {} {
  # select file to convert
    global gFilNam gDir
    set lFilNamL [ tk_getOpenFile -filetypes { { RAW { .gray .GRAY } } } -initialfile $gFilNam -initialdir $gDir ]
    set gFilNam [ file tail $lFilNamL ]
    set gDir [ file dirname $lFilNamL ]
    WriteConf
  }

  proc WriteConf {} {
  # write conf file
    global env gFilNam gDir gConfFil gRows gCols gDepth gArgs
    set gConfFil [ open [ file join $env(HOME) ".Gray2Jpeg.tcl.conf" ] "w" ]
    puts $gConfFil [ list set gDir $gDir ]
    puts $gConfFil [ list set gFilNam $gFilNam ]
    puts $gConfFil [ list set gRows $gRows ]
    puts $gConfFil [ list set gCols $gCols ]
    puts $gConfFil [ list set gDepth $gDepth ]
    puts $gConfFil [ list set gArgs $gArgs ]
    close $gConfFil
  }

  # default parameters
  set gDir "."
  set gFilNam ""
  set gRows 0
  set gCols 0
  set gDepth 8
  set gArgs ""
  # get parameters from conf file
  catch { source [ file join $env(HOME) ".Gray2Jpeg.tcl.conf" ] }

  # build window
  labelframe .lf -text "Raw Gray Image" -padx 2 -pady 2
  label .lf.l11 -text "Filename:  " -padx 2 -pady 2
  label .lf.l12 -textvariable ::gFilNam -width 20 -anchor w -padx 2 -pady 2
  button .lf.b13 -text "..." -command ChooseFile -bd 1 -padx 2 -pady 2
  label .lf.l21 -text "Rows:  " -padx 2 -pady 2
  entry .lf.e23 -textvariable ::gRows -width 5 -justify right
  label .lf.l31 -text "Columns:  " -padx 2 -pady 2
  entry .lf.e33 -textvariable ::gCols -width 5 -justify right
  label .lf.l41 -text "Depth:  " -padx 2 -pady 2
  entry .lf.e43 -textvariable ::gDepth -width 5 -justify right
  label .lf.l51 -text "Arguments:  " -padx 2 -pady 2
  entry .lf.e52 -textvariable ::gArgs
  label .sep -padx 2 -pady 2 -height 0
  button .bGO -text "Go" -command Gray2Jpeg -padx 2 -pady 2
  button .bEX -text "Exit" -command exit -padx 2 -pady 2

  # display
  wm resizable . 0 0
  grid .lf - - - - -sticky news
  grid .lf.l11 .lf.l12 .lf.b13 -sticky ew
  grid .lf.l21      x .lf.e23 -sticky ew
  grid .lf.l31      x .lf.e33 -sticky ew
  grid .lf.l41      x .lf.e43 -sticky ew
  grid .lf.l51 .lf.e52 - -sticky ew
  grid .sep - - - - -sticky news
  grid x .bGO x .bEX x -sticky ew

  # start up
  if { [ expr { $argc > 0 } ] } {
    set gFilNam [ file tail [ lindex $argv 0 ] ]
    set gDir [ file dirname [ lindex $argv 0 ] ]
  }

Category Windows