AutoHotkey Community

It is currently May 25th, 2012, 7:14 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Printerfunctions
PostPosted: May 29th, 2007, 3:52 pm 
Offline

Joined: November 27th, 2006, 1:44 pm
Posts: 61
Location: Heerlen Country: Netherlands
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 May 30th, 2007, 3:44 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 10:28 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 3:43 pm 
Offline

Joined: November 27th, 2006, 1:44 pm
Posts: 61
Location: Heerlen Country: Netherlands
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 3:50 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 5:53 pm 
Offline

Joined: June 26th, 2006, 6:14 pm
Posts: 1379
Location: USA
Should it not be
Code:
   StringTrimRight, printerlist, printerlist, % StrLen(Delimiter)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 30th, 2007, 6:36 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
That parameter accepts expressions, so there's no need for %

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printerfunctions
PostPosted: September 7th, 2007, 10:31 pm 
Offline

Joined: June 9th, 2005, 3:35 am
Posts: 29
Location: Clearwater, Florida
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 7th, 2007, 10:39 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
Code:
list:=GetInstalledPrinters()
msgbox, % list

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2007, 3:49 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2541
Cool :)

Request: functions to print text, rtf


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2007, 4:01 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
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.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2007, 5:00 am 
Offline
User avatar

Joined: December 29th, 2004, 1:28 pm
Posts: 2541
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 :oops: when being lazy...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 8th, 2007, 12:02 pm 
Offline

Joined: June 9th, 2005, 3:35 am
Posts: 29
Location: Clearwater, Florida
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2007, 2:18 am 
Offline

Joined: June 9th, 2005, 3:35 am
Posts: 29
Location: Clearwater, Florida
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2007, 6:20 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
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.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 10th, 2007, 7:43 pm 
Offline

Joined: June 9th, 2005, 3:35 am
Posts: 29
Location: Clearwater, Florida
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google Feedfetcher, S0und, tkmmkt and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group