Version 5 of Making Tk apps look native

Updated 2002-07-19 16:17:10

With a compiler, and a modified tclAppInit, it is of course pretty easy to make a executable to run a particular set of tcl/tk code. However, it is nice to be able to write pure tcl/tk as much as possible to accomplish the same task. There are a few aspects to this:

  • provide an icon for the application which can be double-clicked on to run
  • allow drag-n-drop of documents onto the icon to interact with them
  • allow other applications to send files to the application.
  • ...

Obviously there may be some platform-specific aspects to correct "native" behaviour as well (for example on Windows, we might want to add a shortcut to the application and its help/readme to the Start->Programs menu).

Windows

On Windows, any .tcl file can be double-click to launch wish by default. We can also easily add the application to the Programs menu as follows:

    windows::CreateGroup Alphatk ~/Apps/Alphatk8.1 alphatk.tcl AlphaCore/alpha.icr

    package require dde
    proc windows::CreateGroup {name root script icon} {
        # This won't work with a scripted document.  In that case
        # we will have to be a bit cleverer...
        windows::ProgmanExecute CreateGroup $name
        windows::ProgmanExecute AddItem [file join $root $script] \
          $name $iconfile
        windows::ProgmanExecute AddItem [file join $root Readme.txt] \
          Readme
    }

    proc windows::ProgmanExecute {name args} {
        eval [list windows::DdeExecute PROGMAN PROGMAN $name] $args
    }

    proc windows::DdeExecute {service topic name args} {
        set cmd "\[$name\("
        set subcmds [list]
        foreach a $args {
            lappend subcmds "\"$a\""
        }
        append cmd [join $subcmds ","] "\)\]"
        dde execute $service $topic $cmd
    }

But how can we allow 'drag-n-drop' onto an icon for our application?

Unix

please fill in

MacOS

please fill in

MacOS X

The WishShell application on MacOS X is actual a 'bundle' -- it's a directory of stuff pretending to be an application. If you go inside that directory (which you can do from the command-line shell), you will find a directory 'Scripts'. Place a file "AppMain.tcl" in there, and that file will be sourced when you run the application -- you now have a new application. You can also replace the application icon (there is Wish.icns in the bundle). I'm not sure whether you can change the application menu name from "Wish" without recompiling, however...

If you write a proc "proc tk::mac::OpenDocument {args} {...}" it will be called with the list of file names any time files are dropped onto the Wish application, or "open document" (odoc) events are sent.

please add more


[Wasn't someone intending to write up a bunch of options to mollify Tk's appearance? I remember things like fiddling with borderwidths on Windows, and so on ...]