AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Printerfunctions
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
eagle00789



Joined: 27 Nov 2006
Posts: 61
Location: Heerlen Country: Netherlands

PostPosted: Tue May 29, 2007 2:52 pm    Post subject: Printerfunctions Reply with quote

I have collected several usefull printerfunctions and have them combined in a single ahk-file.

  • GetInstalledPrinters(Delimiter="|",Default=True) returns a list of all installed printers followed by the delimiter.
    • The delimiter can be changed by setting the delimiter parameter.
    • The Default parameter is to get the default selected printer with a double delimiter behind it.
  • GetDefaultPrinter() is just to get the currently selected default printer
  • SetDefaultPrinter(sPrinter) is to change the default printer.
    • The parameter must be the COMPLETE name of the printer.


you can find them here: http://www.autohotkey.net/~eagle00789/Functions/printerfunctions.ahk
Current Version: 1.0.1

Version History
1.0.1: Trimmed of the trailing delimiter character when using GetInstalledPrinters (thanks to Titan)
1.0.0: Initial Release


Last edited by eagle00789 on Wed May 30, 2007 2:44 pm; edited 1 time in total
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5244
Location: UK

PostPosted: Wed May 30, 2007 9:28 am    Post subject: Reply with quote

Thanks for sharing. A couple suggestions: use StringTrimRight, printerlist, printerlist, 1 before you return the list in GetInstalledPrinters() to remove the trailing delimiter, DllCalls can be simplified to just winspool.drv\... since it's automatically assumed to be in the system's PATH.
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
eagle00789



Joined: 27 Nov 2006
Posts: 61
Location: Heerlen Country: Netherlands

PostPosted: Wed May 30, 2007 2:43 pm    Post subject: Reply with quote

i updated it to use the following line:
Code:
   StringTrimRight, printerlist, printerlist, StrLen(Delimiter)
because if someone uses a `n delimiter, you have to trim 2 characters off
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Wed May 30, 2007 2:50 pm    Post subject: Reply with quote

Acctually No. `n is a single character.But you are save if the user specifies a string of charcaters as delimiters. Which might be strange but possible.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ahklerner



Joined: 26 Jun 2006
Posts: 1381
Location: USA

PostPosted: Wed May 30, 2007 4:53 pm    Post subject: Reply with quote

Should it not be
Code:
   StringTrimRight, printerlist, printerlist, % StrLen(Delimiter)
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5244
Location: UK

PostPosted: Wed May 30, 2007 5:36 pm    Post subject: Reply with quote

That parameter accepts expressions, so there's no need for %
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dave Campbell



Joined: 09 Jun 2005
Posts: 27
Location: Clearwater, Florida

PostPosted: Fri Sep 07, 2007 9:31 pm    Post subject: Re: Printerfunctions Reply with quote

eagle00789 wrote:
I have collected several usefull printerfunctions and have them combined in a single ahk-file.

  • GetInstalledPrinters(Delimiter="|",Default=True) returns a list of all installed printers followed by the delimiter.
    • The delimiter can be changed by setting the delimiter parameter.
    • The Default parameter is to get the default selected printer with a double delimiter behind it.
  • GetDefaultPrinter() is just to get the currently selected default printer
  • SetDefaultPrinter(sPrinter) is to change the default printer.
    • The parameter must be the COMPLETE name of the printer.


you can find them here: http://www.autohotkey.net/~eagle00789/Functions/printerfunctions.ahk
Current Version: 1.0.1

Version History
1.0.1: Trimmed of the trailing delimiter character when using GetInstalledPrinters (thanks to Titan)
1.0.0: Initial Release


This is just the routine I need to set my default printer. The only problem is it doesn't set it. I tried running the list of installed printers but I don't know where I'm supposed to see the list.

I'm running WinXP Pro SP2 and did find the default printer listed in the registry. Am I missing something?
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Fri Sep 07, 2007 9:39 pm    Post subject: Reply with quote

Code:

list:=GetInstalledPrinters()
msgbox, % list

_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2485

PostPosted: Sat Sep 08, 2007 2:49 am    Post subject: Reply with quote

Cool Smile

Request: functions to print text, rtf
Back to top
View user's profile Send private message Visit poster's website
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Sat Sep 08, 2007 3:01 am    Post subject: Reply with quote

without saving to a file first? If you don't mind a temp file, you can do this with Run. if you delete it fast enough, it will never be written to disk.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
corrupt



Joined: 29 Dec 2004
Posts: 2485

PostPosted: Sat Sep 08, 2007 4:00 am    Post subject: Reply with quote

engunneer wrote:
without saving to a file first? If you don't mind a temp file, you can do this with Run. if you delete it fast enough, it will never be written to disk.
Yes, without saving to a file first would be great. I typically only use temp files when other methods aren't available or when in a hurry or Embarassed when being lazy...
Back to top
View user's profile Send private message Visit poster's website
Dave Campbell



Joined: 09 Jun 2005
Posts: 27
Location: Clearwater, Florida

PostPosted: Sat Sep 08, 2007 11:02 am    Post subject: Reply with quote

engunneer wrote:
Code:

list:=GetInstalledPrinters()
msgbox, % list


After I figured out where to put that sniplet, it worked great. At first I included it as part of the routine, then I discovered it was designed to CALL the routine, not be a part of it. My in depth understanding of the mechanics of getting this to work is pretty weak, so I appreciate your help teaching me. I have the list of printers working now but I still don't understand how to apply the code for setting the default printer.

Do you have a corresponding idea or sniplet for calling that routine?
Back to top
View user's profile Send private message
Dave Campbell



Joined: 09 Jun 2005
Posts: 27
Location: Clearwater, Florida

PostPosted: Mon Sep 10, 2007 1:18 am    Post subject: Reply with quote

Quote:
After I figured out where to put that sniplet, it worked great. At first I included it as part of the routine, then I discovered it was designed to CALL the routine, not be a part of it. My in depth understanding of the mechanics of getting this to work is pretty weak, so I appreciate your help teaching me. I have the list of printers working now but I still don't understand how to apply the code for setting the default printer.

Do you have a corresponding idea or sniplet for calling that routine?


I'm attempting to write a script that will let me set the default printer from a list of installed printers. Ideally, I'd like to have an InputBox with a numbered list of all installed printers and choose a printer number to be set as default.

I've gotten the script to write out an ordered and numbered list of installed printers to a text file and am able to read from that list to get the printer name and to actually set the default printer. I manually typed in the list of printer names and order number into the InputBox which works like a charm. The only problem is that as soon as I delete a printer or add one, the list is obsolete.

I just can't figure out how to dynamically get the contents of that file into the InputBox. I could even settled with a MsbBox.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Mon Sep 10, 2007 5:20 pm    Post subject: Reply with quote

please post your code. We can easily convert it to use a GUI dropdownlist, or a bunch of radio buttons, and it will be much easier to use.

you can also post in in the ask for help section if you want, and then post the result back here.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Dave Campbell



Joined: 09 Jun 2005
Posts: 27
Location: Clearwater, Florida

PostPosted: Mon Sep 10, 2007 6:43 pm    Post subject: Reply with quote

engunneer wrote:
please post your code. We can easily convert it to use a GUI dropdownlist, or a bunch of radio buttons, and it will be much easier to use.

you can also post in in the ask for help section if you want, and then post the result back here.


Code:


FileDelete D:\MyDocuments\AutoHotKey\printers.txt
FileDelete D:\MyDocuments\AutoHotKey\printer names.txt
;
; Get list of installed printers
;
list:=GetInstalledPrinters()
;
; Prompt for default printer
;
; msgbox, % list
SetDefaultPrinter(sPrinter)
{      
         DllCall(A_WinDir . "\system\winspool.drv\SetDefaultPrinterA", "str", sPrinter)
}

InputBox, ItemNumber,Select a Default Printer, Pick a printer you want to be set to default:`n`n1. 1 page Brother`n2. 2 page Brother`n3. 2 page top2top Brother`n4. Adobe PDF`n5. ClickBook Printer`n6. Dell Photo AIO Printer 926`n7. EPSON Stylus Photo R220 Series`n8. Microsoft Office Document Image Writer`n9. PDF Check`n`nEnter a single digit representing the printer you want.,,340,305,,,,,4
ArrayCount = 0
Loop, Read, D:\MyDocuments\AutoHotKey\printers.txt   ; This loop retrieves each line from the file, one at a time.
{
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Array%ArrayCount% := A_LoopReadLine  ; Store in next array element.
      If ArrayCount = %ItemNumber%
      {
         var2=%A_LoopReadLine%
; msgbox A_LoopReadLine = x%A_LoopReadLine%x  var2=y%var2%y
;          result := SetDefaultPrinter(A_LoopReadLine)
         result := SetDefaultPrinter(var2)
         msgbox ,1,Set Default Printer,The default printer has been set to %A_LoopReadLine%.,10
         return
      }
}

GetInstalledPrinters(Delimiter="|",Default=True)
{
   if (Default = True)
   {
      regread,defaultPrinter,HKCU,Software\Microsoft\Windows NT\CurrentVersion\Windows,device
      stringsplit,defaultName,defaultPrinter,`,
      defaultName := defaultName1
      printerlist =
      PrinterCount=0
      loop,HKCU,Software\Microsoft\Windows NT\CurrentVersion\devices
      {
         if (A_LoopRegName = defaultname)
         printerlist = %printerlist%%A_loopRegName%%Delimiter%%Delimiter%
         else printerlist = %printerlist%%A_loopRegName%%Delimiter%
         PrinterCount += 1
;          msgbox %PrinterCount%.  %A_loopRegName%
         FileAppend %PrinterCount%. %A_loopRegName%  `n,D:\MyDocuments\AutoHotKey\printer names.txt
         FileAppend %A_loopRegName%  `n,D:\MyDocuments\AutoHotKey\printers.txt
      }
   }
   else
   {
      printerlist =
      loop,HKCU,Software\Microsoft\Windows NT\CurrentVersion\devices
      {
         printerlist = %printerlist%%A_loopRegName%%Delimiter%
      }
   }
   StringTrimRight, printerlist, printerlist, StrLen(Delimiter)
   return %printerlist%
}
return


WOW ! ! ! That's the best offer I've had all day. Thank you very much.

I'm looking for the simplest results, user friendliness wise. I think the idea of radio buttons would be swift, or maybe to be able to double click on the desired printer from a text window.

I really look forward to seeing the code.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group