Shell script 1 /root/Choices/MIME-types/select.tcl
#! /usr/bin/wish
#
# Select Application To choose app filetype association, in Rox.
# Written by Jesse Liley for Puppy Linux, 2005.
#
# get screen resolution
set scr_x [winfo screenwidth .]
set scr_y [winfo screenheight .]
# get command line parameters
set filename [lindex $argv 0]
set ext [string tolower [lindex [split $filename .] end]]
#function to scan database for associations
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
}
}
}
# These two files are to be scanned.
set userConfigfile "/root/.AppFileTypes"
input_scan_file /root/Choices/AppFileTypes
input_scan_file $::userConfigfile
#calculate nice viewing sizes for the canvas
set ::max_view_height 140
set ::item_height 20
set ::canv_height 180
proc recalc {} {
set n_items [llength [array names ::appname]]
set ::canv_height [expr "$::item_height * $n_items"]
set ::max_view_height [expr "int( ($::max_view_height + $::item_height / 2) / $::item_height) * $::item_height"]
if {$::canv_height < $::max_view_height} {set ::canv_height $::max_view_height}
}
recalc
# create an image - would eventually get an icon for each app...
image create photo ::img::redicon -width 5 -height 5
::img::redicon put #cc0000 -to 0 0 4 4
# setup window location
set app_width 350
set app_height 300
# start with mouse pointer location, and then center the window over that spot,
# without going over screen edge.
set mouse_x [expr "[lindex [winfo pointerxy .] 0] - [winfo vrootx .] - ($::app_width/2)"]
set mouse_y [expr "[lindex [winfo pointerxy .] 1] - [winfo vrooty .] - ($::app_height/2)"]
if {$mouse_x < 0} {set mouse_x 0}
if {$mouse_y < 0} {set mouse_y 0}
if {$mouse_x > [expr "$scr_x - ($::app_width/2)"]} {set mouse_x [expr "$scr_x - ($::app_width/2)"]}
if {$mouse_y > [expr "$scr_y - ($::app_height/2)"]} {set mouse_y [expr "$scr_y - ($::app_height/2)"]}
#set window
wm title . "Select Application Run type..."
wm geometry . "$::app_width\x$::app_height+$mouse_x+$mouse_y"
# layout window components
# filename
pack [label .lab -text "File: $filename" -font Helvetica ]
# Application list
frame .my -bd 1 -relief sunken
canvas .my.list -height $::max_view_height -scrollregion "0 0 0 $::canv_height" -yscrollcommand {.my.s set} -yscrollincrement $::item_height
scrollbar .my.s -orient vertical -command {.my.list yview}
pack .my.s -in .my -side right -fill y
pack .my.list -in .my -side left -fill x -expand yes
pack .my -in . -fill x -expand yes
proc set_selector_size {} {
set w [expr "[lindex [.my.list configure -width] 4] - [lindex [.my.s configure -width] 4] -8"]
image create photo ::img::selector -width $w -height $::item_height
::img::selector put #bbbbbb -to 0 0 [expr "$w-1"] $::item_height
::img::selector put #eeeeee -to 0 0 [expr "$w-2"] 1
::img::selector put #eeeeee -to 0 0 1 [expr "$::item_height - 1"]
::img::selector put #555555 -to 1 [expr "$::item_height - 0"] [expr "$w-1"] [expr "$::item_height - 1"]
::img::selector put #555555 -to [expr "$w-2"] 1 [expr "$w-1"] [expr "$::item_height - 1"]
image create photo ::img::lighter -width $w -height $::item_height
::img::lighter put #cccccc -to 0 0 [expr "$w-1"] $::item_height
}
set_selector_size
.my.list create image 30 -50 -tags .my.list.selector -image ::img::selector -anchor nw
.my.list create image 30 -50 -tags .my.list.lighter -image ::img::lighter -anchor nw
set item 0
foreach idx [lsort [array names ::appname]] {
set y_pos [expr "$::item_height * $item + $::item_height / 2"]
.my.list create text 20 $y_pos -anchor w -text "$::appname($idx)"
.my.list create image 10 $y_pos -anchor center -image ::img::redicon
incr item
}
#show 'hilite' non-real selector to show that you can click
bind .my.list <Motion> { mouse_over .my.list %x %y }
#allow click on 'hilite' to activate selector on item
bind .my.list <ButtonPress-1> { mouse_click .my.list %x %y }
#bind mouse wheel on scroll bar and list to allow scrolling....
bind .my.list <ButtonPress-4> { mouse_wheel .my.list -1 }
bind .my.s <ButtonPress-4> { mouse_wheel .my.list -1 }
bind .my.list <ButtonPress-5> { mouse_wheel .my.list 1 }
bind .my.s <ButtonPress-5> { mouse_wheel .my.list 1 }
# show application info, text description of application, and binary name of app.
set ::inp_name "\"Full Description\""
set ::inp_bname "unknown"
frame .myapp -bd 1 -relief raised
pack [label .myappname -text "App Name"] [entry .e1 -textvariable ::inp_name] -in .myapp -side left
pack [label .myapppath -text 'Binary'] [entry .e2 -textvariable ::inp_bname] -in .myapp -side right
pack .myapp -fill x -expand yes -in .
bind .e2 <KeyRelease> { key_press .e2 }
# Show file extesions that are associated with this Application.
set ::ext_list "..."
frame .myappexlst -bd 1 -relief raised
pack [label .i1 -text "App Extensions:"] -in .myappexlst -side left
pack [entry .e3 -textvariable ::ext_list] -in .myappexlst -side right -fill x -expand yes
pack .myappexlst -fill x -expand yes -in .
# Show file extesion and its associated description String.
set ::ext_add ""
set ::ext_name "unk"
set ::ext_fullname "Unknown File"
if { $ext != "" && $ext != {} && [string first / $ext]==-1 } {
set ::ext_name $ext
set ::ext_add $ext
if {[info exist ::extname($ext)]} { set ::ext_fullname $::extname($ext) }
}
frame .myappexnfo -bd 1 -relief raised
pack [label .i2 -text "Extension Name"] -in .myappexnfo -side left
pack [entry .e4 -textvariable ::ext_fullname ] -in .myappexnfo -side right -fill x -expand yes
pack [entry .e5 -textvariable ::ext_name -width 6] -in .myappexnfo -side right
pack .myappexnfo -fill x -expand yes -in .
bind .e4 <KeyRelease> { key_press .e4 }
bind .e5 <KeyRelease> { key_press .e5 }
# Show file extesion and its associated description String.
set ::open_cmdline ""
frame .openact -bd 1 -relief raised
#pack [label .i3 -text "Action:"] -in .openact -side left
pack [entry .e6 -textvariable ::open_cmdline ] -in .openact -fill x -expand yes -side right
pack [label .i4 -text "Cmd Line:"] -in .openact -side left
#pack [entry .e7 -textvariable ::open_action -width 9] -in .openact -side left
pack .openact -fill x -expand yes -in .
bind .e6 <KeyRelease> { key_press .e6 }
#bind .e7 <KeyRelease> { key_press .e7 }
# Show Execute command
set ::open_final ""
pack [label .i5 -text "exec:" -font courier] -fill x -expand yes
# Buttons to Accept or Cancel
frame .mybtn -bd 0 -relief raised
pack [button .mybtn1 -text "Accept ALL" -command { accept_all } -state disabled] -in .mybtn -side left
pack [button .mybtn2 -text "Cancel" -command { destroy . } ] -in .mybtn -side right
pack .mybtn -in .
proc calc_y_pos {item y} {
set yoff [expr "int ([lindex [$item yview] 0] * [lindex [$item configure -scrollregion] 4 3] + 0.0001)"]
set row [expr "($y + $yoff) / $::item_height"]
set y1 [expr "$row * $::item_height"]
return $y1
}
proc mouse_over {item x y} {
set y1 [calc_y_pos $item $y]
if {[expr "int([lindex [$item coords $item.selector] 1])"] == $y1} {
$item coords $item.lighter 1 -50
} else {
$item coords $item.lighter 1 $y1
}
}
proc mouse_wheel {item delta} {
if {$delta > 0} {
$item yview scroll 1 units
} else {
$item yview scroll -1 units
}
}
proc key_press {item} {
set is_good true
set app $::inp_bname
if {$item == ".my.list"} {
if {![info exists ::appexts($app)]} { set ::appexts($app) "" }
if {$::ext_add != ""} {
set ::ext_list [join [lsort -dictionary -unique [concat $::ext_add [concat $::ext_name $::appexts($app)]]] ,]
set act $app|ext
if {[info exists ::appext_cmdline($act)]} {
set ::open_cmdline $::appext_cmdline($act)
} else {
set ::open_cmdline "\$app \"\$file\""
}
} else {
set ::ext_list [join [lsort -dictionary -unique $::appexts($app)] ,]
set act $app|mime|mime
if {[info exists ::appext_cmdline($act)]} {
set ::open_cmdline $::appext_cmdline($act)
} else {
set ::open_cmdline "\$app \"\$file\""
}
}
}
# if type a known extension, set the extension description
if {$item== ".e5"} {
if {[info exists ::extname($::ext_name)]} {
set ::ext_fullname $::extname($::ext_name)
}
}
set apppath ""
if {[catch "exec which $app " options]} {
#tk_messageBox -message $options -type ok
set msg "no app $app"
set is_good false
} else {
set apppath $options
if {$options != ""} {
if {![info exists ::appexts($app)]} { set ::appexts($app) "" }
set ::ext_list [join [lsort -dictionary -unique [concat $::ext_add $::ext_name $::appexts($::inp_bname)]] ,]
}
}
if {$::open_cmdline == ""} {set ::open_cmdline "\$app \"\$file\""}
set ::open_final $::open_cmdline
set path [get_opencmd $apppath $::filename $::open_final]
if {$path==""} {
set msg "options: $apppath $::filename"
set is_good false
}
if {$is_good} {
.i5 configure -text "exec: $path"
.mybtn1 configure -state normal
} else {
.i5 configure -text "Bad: $msg"
.mybtn1 configure -state disabled
}
}
proc get_opencmd {app file fmtstr} {
set output ""
if {$app=="" || $file==""} {return ""}
catch {set output [subst $fmtstr] }
return $output
}
proc mouse_click {item x y} {
set y1 [calc_y_pos $item $y]
$item coords $item.selector 1 $y1
$item coords $item.lighter 1 -50
set app [lindex [lsort [array names ::appname]] [expr "$y1/$::item_height"]]
set ::inp_bname $app
set ::inp_name $::appname($app)
if {![info exists ::appexts($app)]} {set ::appexts($app) ""}
if {$::ext_add != ""} {
set ::ext_list [join [lsort -dictionary -unique [concat $::ext_add $::ext_name $::appexts($app)]] ,]
} else {
set ::ext_list [join [lsort -dictionary -unique $::appexts($app)] ,]
}
key_press $item
}
proc accept_all {} {
set fid [open $::userConfigfile a]
if {$fid > 0} {
set app $::inp_bname
#app-extension-commandline
if {$::ext_add != ""} {
if {$::ext_list != [join $::appexts($app) ,]} {
puts $fid "appexts $app $::ext_list"
}
#app-extension-commandline
set act $app|ext
if {$::open_cmdline != "\$app \"\$file\""} {
if {![info exists ::appext_cmdline($act)] || $::appext_cmdline($act) != $::open_cmdline} {
puts $fid "appext_cmdline $act $::open_cmdline"
}
}
}
#application-name
if {![info exists ::appname($app)]} {
set ::appname($app) ""
}
if {$::appname($app) != $::inp_name} {
puts $fid "appname $app $::inp_name"
}
#extension
if {![info exists ::extname($::ext_name)]} {
set ::extname($::ext_name) ""
}
if {$::ext_fullname != $::extname($::ext_name)} {
puts $fid "extname $::ext_name $::ext_fullname"
}
close $fid
}
destroy .
}
CategoryApplicationPreinstalled
There are no comments on this page. [Add comment]