Run code from clipboard

MEd 2006/03/09: Just another kind of "wiki-reaping". This little script allows you to run source code which is currently located in the system's clipboard. You can choose between

  • Sandbox mode: The code runs in a safe interpreter to protect the system against unwanted actions (delete files, ...)
  • Standard mode: The code runs in a normal interpreter, everything is allowed

To run code on wiki pages just select the text passage in your browser window -> right-click -> copy to clipboard. I tried to run A Simple Fan Animation this way and it worked with both, sandbox and standard mode.


 package require Tk

 proc runinterp {} {
     .out.output delete 1.0 end
     if {$::runsecure} {
         puts "Starting safe interpreter..."
         set engine [safe::interpCreate -nestedLoadOk]
         safe::loadTk $engine
         $engine alias wm caughtwm
         $engine alias tk_messageBox caughtmsgbox
     } else  {
         puts "Starting standard interpreter..."
         set engine [interp create]
         $engine eval "package require Tk"
     }
     $engine alias puts slaveputs
     $engine eval [clipboard get]
 }

 proc caughtmsgbox {args} {
     eval tk_messageBox $args -parent .
 }

 proc caughtwm {args} {
     .out.output insert end "Caught: wm $args\n"
 }

 proc slaveputs {pstring} {
     .out.output insert end "> $pstring\n"

 }

 proc viewclip {w} {
     $w delete 1.0 end
     $w insert 1.0 [clipboard get]    
 }

 set runsecure 1

 . configure -padx 5
 frame .ctrl -pady 5 -pady 5 -relief ridge -borderwidth 2
 frame .out
 checkbutton .ctrl.trusted -text "Sandbox mode" -variable runsecure
 button .ctrl.run -text "Run clipboard code" -command runinterp
 button .ctrl.view -text "View clipboard content" -command {viewclip .out.output}
 button .ctrl.exit -text "Exit" -command exit
 scrollbar .out.vbar -orient vertical -command {.out.output yview}
 scrollbar .out.hbar -orient horizontal -command {.out.output xview}
 text .out.output -font "Courier 10" -width 50 -height 15 -xscrollcommand {.out.hbar set} -yscrollcommand {.out.vbar set} -wrap none

 grid .out.output -row 1 -column 1 -sticky news
 grid .out.vbar -row 1 -column 2 -sticky ns
 grid .out.hbar -row 2 -column 1 -sticky we
 grid rowconfigure .out 1 -weight 1
 grid columnconfigure .out 1 -weight 1

 pack .ctrl.trusted .ctrl.run .ctrl.view .ctrl.exit -side left -expand 1 -fill x -padx 5
 pack .ctrl -expand 1 -fill x -pady 5
 pack .out -expand 1 -fill both -pady 5
 ### End of script

MEd Beside this there seems to be a bug in Tcl/Tk 8.4 when running on Windows: It was not possible for me to enter text in an entry or text widget within an safe interpreter, however it works fine under Linux (and it also works fine on both, Linux and Windows when using Tcl/Tk 8.5).


Category Application