Last Edited: 01 May 2008 by superuser
Importered from old WiKi -- 30/04-08 18:04.

File /root/Choices/MIME-types/app_start

#!/usr/bin/wish

set filename [join $argv]
if {$filename == ""} {destroy . ; return}

wm transient .
wm frame .
wm withdraw .

proc input_scan_file {file} {
if {[file exists $file]==1} {
set fid [open $file]
if {$fid > 0} {
while {[eof $fid]==0} {
gets $fid line
set info [split $line]
set idx [lindex $info 1]
set name [join [lrange $info 2 end]]
if {[lindex $info 0] == "appexts"} {
set ::appexts($idx) [split [lindex $info 2] ,]
}
if {[lindex $info 0] == "extname"} {
set ::extname($idx) $name
}
if {[lindex $info 0] == "appext_cmdline"} { set ::appext_cmdline($idx) $name }
if {[lindex $info 0] == "appname"} {
catch "exec which $idx " path
if {[llength [split $path /]] > 1} {set ::appname($idx) $name }
}
}
close $fid
}
}
}
#load associations data
input_scan_file /root/Choices/AppFileTypes
input_scan_file /root/.AppFileTypes

#xli -onroot -quiet -fillscreen /usr/share/images/in_flight.jpg

set mouse_x [expr "[lindex [winfo pointerxy .] 0] - [winfo vrootx .] - 50"]
set mouse_y [expr "[lindex [winfo pointerxy .] 1] - [winfo vrooty .] - 10"]

if {$mouse_x < 0} {set mouse_x 0}
if {$mouse_y < 0} {set mouse_y 0}

# Create a menu
set m [menu .popupMenu -tearoff 0 -borderwidth 2 -activeborderwidth 1]

#Find the files extension
set f [lindex [split $filename /] end]
set extension ""
if {[string first . $f ] > 0} {
set extension [string tolower [lindex [split $f .] end]]
}
set candidates ""

proc get_opencmd {app file fmtstr} {
set output ""
if {$app=="" || $file==""} {return ""}
catch {set output [subst $fmtstr] }
return $output
}

#Select programs associated with the files extension
if {$extension != ""} {
foreach app [array names ::appexts] {
foreach ex $::appexts($app) {
if {$ex == $extension} {
set path ""
catch "exec which $app " path
if {[llength [split $path /]] > 1} {
set act $app|ext
set fmtdcmd "$path \"$filename\""
if {[info exists ::appext_cmdline($act)]} {
catch {
set fmtdcmd [get_opencmd $path $filename $::appext_cmdline($act)]
}
}
set cmd " catch {exec $fmtdcmd } err "
if {![info exists ::extname($extension)]} {set ::extname($extension) "$extension File"}
set info "$::extname($extension) - $::appname($app)"
lappend candidates [list $path $cmd $info]
}
}
}
}
}

#Select text editor programs if no other program selected
if {[llength $candidates] == 0} {
foreach app [array names ::appexts] {
if {[lsearch $::appexts($app) txt] > -1 || [lsearch $::appexts($app) text] > -1} {
foreach ex [list text,txt] {
set path ""
catch "exec which $app " path
if {[llength [split $path /]] > 1} {
set act $app|ext
set fmtdcmd "$path \"$filename\""
if {[info exists ::appext_cmdline($act)]} {
catch {
set fmtdcmd [get_opencmd $path $filename $::appext_cmdline($act)]
}
}
set cmd " catch {exec $fmtdcmd } err "
if {![info exists ::extname($extension)]} {set ::extname($extension) "$extension File"}
set info "$::extname($extension) - $::appname($app)"
lappend candidates [list $path $cmd $info]
}
}
}
}
}

#Put items into the menu for popup.
set app_list ""
foreach {can} $candidates {
foreach {app cmd info} $can {
if {[lsearch $app_list $app] == -1} {
$m add command -label $info -command $cmd
lappend app_list $app
}
}
}

if {[llength $candidates] == 0} {
$m add command -label "Execute" -command "exec $filename &"
$m add command -label "Open with Leafpad" -command "exec leafpad \"$filename\" &"
}

$m add command -label "Select..." -command "exec /root/Choices/MIME-types/select.tcl \"$filename\" &"

tk_popup $m $mouse_x $mouse_y

# Set the application to close when focus leaves the popup menu.
bind $m <Leave> { destroy .}



CategoryApplicationPreinstalled