Imbue
Imbue

Reputation: 3907

What is the best way to open a file with its associated application from TCL?

I have a TCL/TK app on Windows. What is the best way to open a file with its associated program? For example, I am generating a PDF, and I want it to open automatically.

I have been using:

proc OpenDocument {filename} {
    if {[catch {
        exec rundll32.exe url.dll,FileProtocolHandler $filename &
    }]} {
        tk_messageBox -message "Error opening $filename."
    }
}

But I'm not sure how robust it is, and I would like to have a better error message. For example, how can I even detect if any program is installed and associated with PDFs?

I know that many programs (including Foxit PDF Reader) add a registry entry under "HKEY_CLASSES_ROOT.extension", but Adobe Reader seems to use a different system.

Is there any one best practice? I am going to be deploying my program to a few hundred users, and I want to be fairly sure there won't be widespread issues if someone has an odd configuration.

Thanks.

Upvotes: 1

Views: 1272

Answers (3)

mrcalvin
mrcalvin

Reputation: 3434

For the records: Using the Tcl extension TWAPI, one might also want to look at:

twapi::shell_execute -path $filename

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246877

There's an example to see what the association is on the registry man page

package require registry
set ext .tcl

# Read the type name
set type [registry get HKEY_CLASSES_ROOT\\$ext {}]
# Work out where to look for the command
set path HKEY_CLASSES_ROOT\\$type\\Shell\\Open\\command
# Read the command!
set command [registry get $path {}]

puts "$ext opens with $command"

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246877

I would write (for Tcl 8.5)

exec {*}[auto_execok start] "" $filename

(discussion here). That may display a cmd window though.

If you have an earlier Tcl,

eval exec [auto_execok start] {""} [list $filename]

Upvotes: 4

Related Questions