Sign in to follow this  
Followers 0
Dutchman

Print script

17 posts in this topic

Hi,

I was hoping someone could give me some general guidance on building a script that would execute when a file was dropped on an icon. For example: If I take a .doc file and drag it and drop it on my icon, I want my script to open Word, minimized, print the file I dropped on it, and then when it has been sent to the printer, close Word. I would like to do the same with Notepad, Excel, Acrobat, and Paint. I could associate various file types to run various programs and still change those associations as I see fit. Is this doable?

But being rather new to this scripting thing, I'm not sure how I would go about getting the file name, path and type into the script so it could be executed. I imagine the drop function would be accomplished with the release of the mouse button, over the icon(x,y coordinate pair?) Am I on the right track? Are there a few examples of doing such a thing?

Thanks!

Share this post


Link to post
Share on other sites



This is just the way i'd go about it...

i would use a FileSelectDialog ratehr than drag and drop. but i've never used any drag'n'drop type stuff so im probably not the person to ask

--my two cents

~cdkid


AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!

Share this post


Link to post
Share on other sites

Hi,

I was hoping someone could give me some general guidance on building a script that would execute when a file was dropped on an icon. For example: If I take a .doc file and drag it and drop it on my icon, I want my script to open Word, minimized, print the file I dropped on it, and then when it has been sent to the printer, close Word. I would like to do the same with Notepad, Excel, Acrobat, and Paint. I could associate various file types to run various programs and still change those associations as I see fit. Is this doable?

An au3 compiled script implements the dragdrop method - kudos to whoever implemented that in the compiled executable - build a test script w/ a msgbox to show you the output, I believe that it is a pipe-delimited list, if more than one file is dragged on the target. For each string passed, you'll want to test fileexists() and then decide what to do with it - i.e. if StringRight($cmdline[$i],4) = ".xls" then ... etc.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

heres an example from one of my scripts

#include <GuiConstants.au3>
;#include <Array.au3>
opt("TrayIconDebug", 1)
;************ NOTES START ************
;************ NOTES END **************
GUICreate("", 540, 200, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION), BitOR($WS_EX_ACCEPTFILES, $WS_EX_TOPMOST))
GUISetBkColor(0xD4FF2A)
GUISetFont(13, 690)
$LABEL_1 = GUICtrlCreateLabel("Drag and Drop File to get path", 20, 24, 500, 25)
$LABEL_2 = GUICtrlCreateLabel("Here:", 20, 75, 48, 25)
$INPUT_1 = GUICtrlCreateInput("", 70, 75, 100, 25)
GUICtrlSetState(-1, $GUI_ACCEPTFILES)
GUISetState()
While 1
    $MSG = GUIGetMsg()
    Select
        Case $MSG = $GUI_EVENT_CLOSE
            Exit
        Case GUICtrlRead($INPUT_1) <> ""
            $PATHOLDNAME = GUICtrlRead($INPUT_1)
            GUIDelete()
            MsgBox(0, "", $PATHOLDNAME)
            Exit
    EndSelect
WEnd

phil

edit: removed +'s in styles, taken from old script, added bitor()

Edited by pecloe

Share this post


Link to post
Share on other sites

Sample code to test - compile and drop onto the resultant icon.

For $i = 1 To $cmdline[0]
    $fn = $cmdline[$i]
        If FileExists($fn) Then 
           MsgBox(0,$i,$fn,0)
         Select
             case StringRight($fn,4) == '.txt'
               ;do notepad stufff 
             case StringRight($fn,4) == '.xls'
;assume excel
;ad nauseum
             case else
         Endselect
        EndIf
Next

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Sample code to test - compile and drop onto the resultant icon.

For $i = 1 To $cmdline[0]
    $fn = $cmdline[$i]
        If FileExists($fn) Then 
           MsgBox(0,$i,$fn,0)
         Select
             case StringRight($fn,4) == '.txt'
    ;do notepad stufff 
             case StringRight($fn,4) == '.xls'
;assume excel
;ad nauseum
             case else
         Endselect
        EndIf
Next
I took your idea and made it happen... now all we need to do is print

opt("TrayIconDebug", 1)
opt("WinTitleMatchMode", 2)

If $cmdline[0] = 0 Then
    $fn = FileOpenDialog("Print File", @MyDocumentsDir, "All (*.*)")
ElseIf $cmdline[0] = 1 Then
    $fn = $cmdline[1]
EndIf

If FileExists($fn) Then
    $fn = StringReplace($fn, StringRight(StringLeft($fn, 3), 2), ':\"') & '"'
    RunWait(@ComSpec & " /C " & "start " & $fn, "", @SW_HIDE)
EndIf

$count = 0;
$title = StringSplit($fn, "\")
$title = StringTrimRight($title[$title[0]], 6)
While $count < 100
    If WinGetState($title) == "16" Then
        ExitLoop
    EndIf
    WinSetState($title, "", @SW_MINIMIZE)
    Sleep(51)
WEnd

This will open up any file btw

Edited by mikeytown2

Share this post


Link to post
Share on other sites

printing should be the easy part

from help

_FilePrint

--------------------------------------------------------------------------------

Prints a plain text file.

#include <File.au3>

_FilePrint ( $s_File [, $i_Show])

Parameters

$s_File The file to print.

$i_Show The state of the window. (default = @SW_HIDE)

8)

NEWHeader1.png

Share this post


Link to post
Share on other sites

Wow, I was still working on the dummy code, trying to figure out what needs to happen and in what order. I appreciate all the work that has been done to achieve this. I'm still trying to decipher what each of the steps accomplish in the overall effort. I want to get it done, but I also want to learn to write the code to script it. As I'm learning this stuff I want to be able to write a setup so anyone can simply associate the file type with the program it is to open. Later, when thats done, I want to be able to attach the file, after compiling, to an avedesk icon. Each icon represents one of two printers that I own; one laser, one inkjet. Depending upon the printer that I drag and drop the file to, it automatically directs it to the right one...while still opening and closing the appropriate program to facilitate the actual print job.

If anyone has any comments that they'd like to add to the code to help me decipher whats going on in the process, I'd appreciate it. Thanks.

Share this post


Link to post
Share on other sites

The code that i gave you has 3 basic functions...

Part1

The If block that has $cmdline[0] will have the open dialog box popup if you run the program with out dropping anything on the icon. If you do drop a file on the icon then it will grab it and store the file location in the variable $fn. so from a command line it would look something like this...

c:\autoitPrint.exe "c:\my docs\doc.xls"

the $fn is then set to c:\my docs\doc.xls

Part 2

This opens the file using the start command, but it doesn't work if you have a space anywhere in the file name, like we do above. So what the StringReplace part does is it turns

c:\my docs\doc.xls Into

c:\"my docs\doc.xls"

and then the run command does this

start c:\"my docs\doc.xls"

Part 3

The While loop here is looking for the open window. i use the last part of the file name doc.xls and trim the string down so it says do. I made it trim off the last letter of the name of the document because if you open up a html file then this program doesn't work. if your only going to open files that have a 3 letter extension then change the 6 to a 5 in this line...

$title = StringTrimRight($title[$title[0]], 6)

So once it has the name of the file it looks for any window that has that string in it. once it found it, the program then minimizes it. the loop terminates once that window has the state of minimized.

One thing that will help you out greatly is too look up every command that i used in the program in the help file. i use the help file non stop when i code autoit. i usually have 2 open actually. the autoit help file is your best friend, especially the index... that is a life saver. :o

Share this post


Link to post
Share on other sites

O.K. I have a pretty good grasp on the provided code, and have added a logging function to it and the printing function too. Works pretty slick! The code provided works well. I couldn't have done that so quickly. Now I'm trying to figure out how to get the current default printer, save it, set the second printer as default, go ahead and print, and when done, reset the default printer back to what it was. This is so I can print to a given printer just by dragging it to the appropriate icon. Is there a better way of doing this?

Also is it possible to use a .png image as an input device...in much the same way as a widget is used? Also can a simple input box have an image background instead of color? I'm trying to figure out what the next phase should be? :o)

Share this post


Link to post
Share on other sites

well uhhh not to ruin your ingenioius idea... :o

I believe windows does this already. :geek:

try dragging a file onto a printer ;)

not that using auto it is a bad idea

Share this post


Link to post
Share on other sites

Well, if you assign an icon to the desktop it will do it. But there are a lot of drawbacks to doing it. Doing it this way will allow me to customize the process and I'll learn something new too that I can use in future processes. If you have some useful ideas as to how to facilitate this customizable drag-n-drop printing procedure, I'd like to hear from you again. Thanks.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Now I'm trying to figure out how to get the current default printer, save it, set the second printer as default, go ahead and print, and when done, reset the default printer back to what it was.

This might help. Here is a simple vbs script that will change the default printer from one to another. Perhaps you can write from AutoIT to a file script.vbs and change the contents based on which printer you want to be the default. Then have AutoIT run the vbs script to do the change:

vbs-vbs-vbs-vbs!!!

Option Explicit 
Dim oNetwork, sPrintPath, sLocate 
Set oNetwork = CreateObject("WScript.Network") 
sLocate = InputBox("Where are you? Type 'Lab', or 'SSBack'.") 
Select Case sLocate 
   Case "Lab"
        sPrintPath = "\\staff\Student Services - Front Office" 
   Case "SSBack" 
        sPrintPath = "\\staff\Student Services - Back Hall" 
   Case "Main" 
        sPrintPath = "\\staff\Main Office" 
   Case Else 
        Wscript.Echo "That is not a valid choice." :Wscript.Quit 
End Select 
oNetwork.AddWindowsPrinterConnection sPrintPath 
oNetwork.SetDefaultPrinter sPrintPath

Or, maybe an AutoIT guru can convert the above vbs to AutoIT? :o

Edited by jefhal

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

I can try and help with some vbs.. but not the AutoIT code, This is my first post... the last company I worked for had old netware systems that needed to remove old printers from the old servers and map a printer on the new print servers, and we could go desk to desk logging off users and doing it the long way round, so I cam up with the following script to add a new printer and check for a old one then delete the old one and set the new one as default I hope it helps. :o

CODE

------------------------------------------------------------------------------------------------------------------------------

strPrinter="\\ukkinprt01\ukkinprt066"

Set WshNetwork = CreateObject("WScript.Network")

Set WshShell = CreateObject("WScript.Shell")

WshNetwork.AddWindowsPrinterConnection strPrinter

WshNetwork.SetDefaultPrinter strPrinter

MsgBox strPrinter & " Printer connected and set as the default printer"

Set WshNetwork = WScript.CreateObject("WScript.Network")

PrinterPath = "\\ukkinfap02\ukkinprt066"

On Error Resume Next

WshNetwork.RemovePrinterConnection PrinterPath, true, true

If Err.Number = 0 Then

MsgBox PrinterPath & " Printer Removed"

MsgBox "Default printer is now " & strPrinter

End If

On Error Goto 0

-------------------------------------------------------------------------------------------------------------------------------


You only need two tools in life: WD40 and Duct Tape.If it moves and it shouldn't, use Duct Tape.If it should move but doesnt, use WD40.

Share this post


Link to post
Share on other sites

Thanks for the help. I can kinda make out what the VBS script is doing...Let me cogitate on it for a little while. I've kinda taken a diversion from this aspect to chase down the GUI portion of it. I'm trying to figure out how the concept of the recycle bin works, cause I want to use that as the model for my user interface...only with printers. I have a clear mind of what I want it all to look like...I'm still just trying to figure out how to bring it all together. The donations in script however has been wonderful. I really think this will work well.

Share this post


Link to post
Share on other sites

well let me know if you beed anymore help and how your peoject turns out..


You only need two tools in life: WD40 and Duct Tape.If it moves and it shouldn't, use Duct Tape.If it should move but doesnt, use WD40.

Share this post


Link to post
Share on other sites

The biggest thing you could do is decipher what the visual basic script does line by line, function by function, in a quasi psuedo code format. That would allow me to recode it by using autoit(if all functions are supported) and then I could learn something in the process. That would be a huge help. Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0