Skip to content

Instantly share code, notes, and snippets.

@pwelch
Created June 26, 2012 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pwelch/2997537 to your computer and use it in GitHub Desktop.
Save pwelch/2997537 to your computer and use it in GitHub Desktop.
Ruby/Tk script with GUI to print a folder using a system call to lp (CUPS). (Requires cups gem)
#!/usr/bin/env ruby
require 'tk'
require 'tkextlib/tile'
require 'tkextlib/bwidget'
require 'cups'
$dirname = TkVariable.new
root = TkRoot.new {title "Print Folder"}
content = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid(:sticky => 'nsew')
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1
Tk::Tile::Label.new(content) {text 'Folder to Print:'}.grid(:column => 1, :row => 2, :sticky => 'w')
Tk::Tile::Label.new(content) {textvariable $dirname }.grid(:column => 2, :row => 2, :sticky => 'we')
Tk::Tile::Label.new(content) {text 'Printer:'}.grid(:column => 1, :row => 1, :sticky => 'w')
Tk::Tile::Button.new(content) {text 'Print'; command { print_dirname }}.grid(:column => 1, :row => 3, :sticky => 'we')
Tk::Tile::Button.new(content) {text 'Select Folder'; command { get_dirname }}.grid(:column => 2, :row => 3, :sticky => 'we')
Tk::Tile::Button.new(content) {text 'Quit'; command 'exit'}.grid(:column => 3, :row => 3, :sticky => 'we')
$combobox = Tk::BWidget::ComboBox.new(content).grid(:column =>2, :row => 1, :sticky =>'w')
$combobox.values = Cups.show_destinations
$combobox.place('height' => 25,
'width' => 150,
'x' => 10,
'y' => 10)
TkWinfo.children(content).each {|w| TkGrid.configure w, :padx => 5, :pady => 5}
root.bind("Return") { get_dirname }
def get_dirname
begin
$dirname.value = Tk.chooseDirectory
rescue
$dirname.value = ""
end
end
def print_dirname
if $combobox.get.empty? || $dirname.value == "" then
Tk.messageBox(
'icon' => 'error', 'title' => 'ERROR',
'message' => "Please make sure a printer and folder are selected.")
else
system "lp #{$dirname}/*.* -o fitplot -d #{$combobox.get}"
Tk.messageBox('title' => 'Printing', 'message' => "Printing folder #{$dirname}")
end
end
Tk.mainloop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment