Sublime Forum

Equivalent of LPT1

#1

Hello. It should be possible to create a simple cross-platform print option, but I need a little assistance with Apple/Linux, etc…

In Windows I have to first share the printer, and note it’s name, and the computer name. Then I can issue the commands:

os.system('net use lpt1 \\\\ANDYCOMPAQ\\PrimoPDF /persistent:yes') os.system('type system_ex.txt > LPT1')

Can someone advise me of equivalent commands in other Oses? *

I’m hoping that I can just save the file to print (temporarily) with an ‘.txt’ extension. Otherwise I’ll have to investigate sending the *lines *of the file to the printer.

The output would be completely unformatted and there would be no preview option. As mentioned, it would just be a rough and ready print option. It would, however, be possible to just print the selection and perhaps to give an (approximate) page number or range.

[code]import sublime, sublime_plugin
import os

DEF_COMPUTER = sublime.load_settings(PACKAGE_SETTINGS).get(“comp_name”, \

False)

if not DEF_COMPUTER:

DEF_COMPUTER = os.environ’COMPUTERNAME’]

DEF_PRINTER = sublime.load_settings(PACKAGE_SETTINGS).get(“default_printer”, \

“PrimoPDF”)

Advanced, Print Processor… Text (from RAW) for PrimoPDF

DEF_COMPUTER = “ANDYCOMPAQ”
DEF_PRINTER = “PrimoPDF”

try:
init_printer = “net use lpt1 \\” + DEF_COMPUTER + “\” + DEF_PRINTER +
" /persistent:yes"
os.system(init_printer)
init_printer = True
except Exception:
sublime.status_message(‘Printer not configured correctly.’)
init_printer = False

class PrintDefault(sublime_plugin.WindowCommand):
def run(self):
if init_printer:
vw = self.window.active_view()
vw_str = vw.file_name()
#os.system(‘type system_ex.txt > LPT1’)
os.system(“type " + vw_str.replace(’\’, ‘\\’) + " > LPT1”)
else:
sublime.status_message(‘Printer not configured correctly.’)
[/code]

Actually, print-preview would be kind-of possible using another view, but it wouldn’t look anything like the printed (white) version.

Any advice, or suggestions, are welcome. Andy.

*

0 Likes

#2

If it helps, below is a version that works for Windows. You just need to:

Share your printer;
Edit the code below to reflect your computer and printers’ names;
Assign ‘print_default’ to a key-binding. [This could be added to the File menu.]

It creates a file with extension ‘.txt’ in your temporary folder, which is over-written if printed again.
If you are sending to a PDF generator you will need to modify its properties to accept text input and (if possible) set some margins.

I can easily modify it to print the selection or a range of line-numbers, and perhaps add a date-stamp, etc. But I still need some assistance with non-Windows OSes :question:

[code]import sublime, sublime_plugin
import os, tempfile

DEF_COMPUTER = sublime.load_settings(PACKAGE_SETTINGS).get(“comp_name”, \

False)

if not DEF_COMPUTER:

DEF_COMPUTER = os.environ’COMPUTERNAME’]

DEF_PRINTER = sublime.load_settings(PACKAGE_SETTINGS).get(“default_printer”, \

“PrimoPDF”)

Advanced, Print Processor… Text (from RAW) for PrimoPDF

DEF_COMPUTER = “ANDYCOMPAQ”
DEF_PRINTER = “PrimoPDF”

try:
init_printer = “net use lpt1 \\” + DEF_COMPUTER + “\” + DEF_PRINTER +
" /persistent:yes"
os.system(init_printer)
init_printer = True
except Exception:
sublime.status_message(‘Printer not configured correctly.’)
init_printer = False

class PrintDefault(sublime_plugin.WindowCommand):
def run(self):
if init_printer:
vw = self.window.active_view()
vw_filename = vw.file_name()
vw_base = os.path.basename(vw_filename)
tempd = tempfile.gettempdir()
vw_firstbase, vw_ext = os.path.splitext(vw_base)
if not vw_ext or vw_ext != ‘.txt’:
vw_filename = vw_firstbase + ‘.txt’
else:
vw_filename = vw_base
vw_filename = tempd + os.sep + vw_filename
tempf = open(vw_filename, ‘w’)
for line in vw.split_by_newlines(sublime.Region(0, vw.size())):
tempf.write(vw.substr(line) + ‘\n’)
tempf.close()

        #os.system('type system_ex.txt > LPT1')
        os.system("type " + vw_filename.replace('\\', '\\\\') + " > LPT1")
    else:
        sublime.status_message('Printer not configured correctly.')

[/code]

0 Likes