forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint.dropzone
101 lines (87 loc) · 3.36 KB
/
Print.dropzone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/ruby
# Dropzone Destination Info
# Name: Print
# Description: Dropped files will be printed. If you have multiple printers, you will be prompted for which printer you wish to use.
# Handles: NSFilenamesPboardType
# Events: Dragged
# Creator: Aptonic Software
# URL: http://aptonic.com
# IconURL: http://aptonic.com/destinations/icons/print.png
$printing_tmp_folder = "/tmp/Dropzone-Printing"
def dragged
standard_types = ["pdf", "jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff", "txt", "rtf"]
rich_types = ["xlsx", "xls", "docm", "dotx", "dotm", "xlsm", "doc", "docx", "ppt", "pptx", "pptm"]
# Check for valid type
$items.each do |item|
ext = File.extname(item).downcase[1..-1]
if not standard_types.include?(ext) and not rich_types.include?(ext)
$dz.finish("Unsupported Type")
$dz.url(false)
Process.exit
end
end
printer = get_printer()
if printer == "use_default_printer"
printer_flag = ""
else
printer_flag = "-d #{printer}"
end
lp_output = ""
lp_command = ""
system("/bin/rm -rf #{$printing_tmp_folder}")
# Process and print each item
$items.each do |item|
ext = File.extname(item).downcase[1..-1]
if standard_types.include?(ext)
lp_command = "lp #{printer_flag} \"#{item}\" 2>&1"
lp_output = `#{lp_command}`
else
# Convert document to PDF with qlmanage
pdf_output_file = make_pdf(item)
lp_command = "lp #{printer_flag} \"#{pdf_output_file}\" 2>&1"
lp_output = `#{lp_command}`
end
if lp_output =~ /Error/
$dz.error("Printing Error", "The lp command was called with #{lp_command} and returned " + lp_output)
end
end
$dz.finish("Printing...")
$dz.url(false)
end
def get_printer
output = `lpstat -v 2>&1`
if output =~ /No destinations added/
$dz.finish("No Printers Available")
$dz.url(false)
Process.exit!
end
lpstat_output = output.split("\n")
printers = ""
return "use_default_printer" if lpstat_output.length == 1
lpstat_output.each {|printer| printers = printers + " \"" + printer.split(" ")[2].gsub("_", " ")[0..-2] + "\""}
s = ($items.length > 1 ? "s" : "")
result = `./CocoaDialog dropdown --title "Print #{$items.length} Item#{s}" --text "Select Printer:" --items #{printers} --button1 "OK" --button2 "Cancel" --string-output`
output_split = result.split("\n")
if output_split[0] == "Cancel"
$dz.finish("Cancelled")
$dz.url(false)
Process.exit!
else
return output_split[1].gsub(" ", "_")
end
end
def make_pdf(path)
Dir.mkdir($printing_tmp_folder) unless File.exists?($printing_tmp_folder)
file = File.basename(path)
`qlmanage -p -o #{$printing_tmp_folder} "#{path}" >& /dev/null`
if File.exists?("#{$printing_tmp_folder}/#{file}.qlpreview/Preview.html")
a = File.read("#{$printing_tmp_folder}/#{file}.qlpreview/Preview.html")
b = a.gsub('<div class="PageStyle">',"").gsub('{background:#ACB2BB;}','')
File.open("#{$printing_tmp_folder}/qlmanage-doc2pdf-tmp.html","w") {|f| f << b}
output_filename = "#{$printing_tmp_folder}/#{file}.pdf"
`/System/Library/Printers/Libraries/convert -f #{$printing_tmp_folder}/qlmanage-doc2pdf-tmp.html -o \"#{output_filename}\" >& /dev/null`
return output_filename
else
$dz.error("PDF Creation Error", "The generated preview file #{$printing_tmp_folder}/#{file}.qlpreview/Preview.html could not be found.")
end
end