转自aardio论坛,原文由loop发表,链接地址:http://bbs.aardio.com/forum.php?mod=viewthread&tid=22591&page=1#pid86292
单位有一台打印机,win10无法安装驱动。只能通过XP打印文件。
根据这个需求,只需将XP的一个目录共享,然后写一个程序监控目录新增加的文档,来操作打印机自动打印。
这段代码用到了目录监控、com操作word接口、线程信号来完成需求。
path变量是监控的目录地址,我这里使用的是c:\printTemp,日志目录为c:\printLog,这个需要更改一下。
import win.ui;
/*DSG{{*/
var winform = win.form(text="打印";right=589;bottom=349;parent=...)
winform.add(
editChange={cls="edit";left=23;top=21;right=564;bottom=322;db=1;dl=1;dr=1;dt=1;edge=1;hscroll=1;multiline=1;vscroll=1;z=1}
)
/*}}*/
import process;
import fsys.dirWatcher;
import fsys.dlg;
import win.util.tray
//import console;;
import fsys.file;
import thread.event;
import fsys
import thread.semaphore
import thread.manage
import win.ui.menu;
path = "C:\printTemp";
semaphore = thread.semaphore(,1);
manage = thread.manage()
winform.popmenu = win.ui.popmenu(winform);//创建弹出菜单
winform.popmenu.add('退出',function(id){ winform.close()})
popup = win.util.tray(winform);
popup.tip = "打印监控";
popup.message = 0xACCF/*_WM_TRAYMESSAGE*/;
//console.open()
//创建监视线程
winform.thrdWatcher = fsys.dirWatcher.thread(
function(filename,action,actionText){
if(action === 1 && string.indexOf(filename,'.') && !string.startWith(filename,'~')){
var pos = string.lastIndexOf(filename,".")
var ext = string.sub(filename,pos+1)
if(ext === 'doc' || ext=== 'docx'){
printWord(path+""+filename)
winform.editChange.appendText("打印word:",filename,'\n')
}
if(ext === 'xls' || ext=== 'xlsx'){
printExcel(path+""+filename)
winform.editChange.appendText("打印excel:",filename,'\n')
}
if(ext=== 'pdf'){
printPdf(path+""+filename)
winform.editChange.appendText("打印pdf:",filename,'\n')
}
}
}, path);
//process.explore(path)
function printWord(file){
manage.create(semaphoreWord,semaphore,file)
}
function printExcel(file){
manage.create(semaphoreExcel,semaphore,file)
}
function printPdf(file){
manage.create(semaphorePdf,semaphore,file)
}
function semaphorePdf(semaphore,file){
//import console;
import com
import fsys
//console.log('我在等待机会\n') sleep(1000)
semaphore.wait();
//console.log('正在打印'+ file +'\n')
pdf = com.CreateObject("Wscript.Shell")
pdf.run("""C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe"" /p /h "+""""+file+"""",0,false)
sleep(15000)
pdf.Run("cmd.exe /C taskkill /f /im AcroRd32.exe",0,true)
sleep(500)
//console.log('完成打印.\n')
sleep(500)
fsys.delete(file)
semaphore.release()
}
function semaphoreExcel(semaphore,file){
//import console;
import com
import fsys
//console.log('我在等待机会\n') sleep(1000)
semaphore.wait();
//console.log('正在打印'+ file +'\n')
excel = com.CreateObject("Excel.Application")
excel.Workbooks.open(file)
excel.ActiveSheet.PrintOut()
sleep(15000)
excel.DisplayAlerts= false;
excel.application.quit();
//console.log('完成打印.\n')
sleep(500)
fsys.delete(file)
semaphore.release()
}
function semaphoreWord(semaphore,file){
//import console;
import com
import fsys
//console.log('我在等待机会\n') sleep(1000)
semaphore.wait();
//console.log('正在打印'+ file +'\n')
word = com.CreateObject("Word.Application")
word.Documents.Open(file)
word.printout()
sleep(15000)
word.quit(false)
//console.log('完成打印.\n')
sleep(500)
fsys.delete(file)
semaphore.release()
}
import fsys.ini;
import time.performance
function writelog(logfile,logs){
result="";
try{
tempfile = ..io.open(logfile,"a+") //文件名为空则创建可读写的临时文件
//获取当前时间
t_now=..time.now();
//tm = time();
t_now.format="%Y年%m月%d日 %H:%M:%S ";
t_now = tostring(t_now);
logs=t_now+'\n'+logs;
tempfile.write(logs,'-----------------------------------------------\n');
tempfile.close();
result=t_now;
}
catch(e){
result=e;
return result;
}
return result;
}
winform.wndproc = function(hwnd,message,wParam,lParam){
select(message) {//判断消息类型
case( 0xACCF/*_WM_TRAYMESSAGE*/ ) { //托盘图标消息
if( lParam = 0x205/*_WM_RBUTTONUP*/ ){
import mouse;
x,y = mouse.getPos();
//弹出托盘菜单以前,一定要前置主窗口中,不然不点击菜单不会消失
win.setForeground(winform.hwnd)
winform.popmenu.popup( x,y,true )
}
if( lParam = 0x203/*_WM_LBUTTONDBLCLK*/ ){
if(win.isVisible(winform.hwnd)){
winform.show(false)
}
else {
winform.show(true)
}
}
}
case( 0x112/*_WM_SYSCOMMAND*/ ){ //系统命令消息
if( wParam == 0xF060/*_SC_CLOSE*/ || wParam == 0xF020 ){ //用户点击了关闭按钮
winform.show(false); //隐藏窗口
return true;//阻击默认消息传递,取消最小化过程
}
}
}
}
//彻底退出,包括WEB
winform.onClose = function(hwnd,message,wParam,lParam){
popup.delete();
winform.thrdWatcher.close(); //停止监视文件
if(winform.editChange.text !==""){
t_now=..time.now();
t_now.format="%Y年%m月%d日";
t_now = tostring(t_now);
writelog("c:\printLog"+t_now+".txt",winform.editChange.text)
}
win.quitMessage();
}
//process.explore(path)
winform.show(false)
win.loopMessage();