Jump to content

Set Default Printer Toggle


 Share

Recommended Posts

Hey People,

I've been looking at several ways to try and do this but none have seemed to work so far, basically I'm looking to make a really simple toggle script which when ran once will change the default printer to xxxx and if ran again it will set default printer to xxx OR even better would be a really simple gui to select the printer of choice.

The hard part for me is finding something what works to actually change the default printer...

Any help would be much appreciated.

Regards

- Hyflex

Link to comment
Share on other sites

And the AutoIt Snippets wiki. There are plenty of examples at your disposal on this topic.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I wrote this a while ago to try the same thing via a right click tray menu. I also had another program that changed my default printer based on my IP address for different offices.

** NOTE: 1. It won't reflect changes made by going to Control panel and setting the default printer that way

2. It doesn't update the default printer in programs you already had started, eg start Word, Change Default printer, Word still shows original printer

NiVZ

#include <Array.au3>
#include <Constants.au3>
; Turn off default tray menu options
Opt("TrayMenuMode", 1)
; Set some program constants
Const $ProgramName   = "Default Printer Selector"
Const $ProgramVersion = "1.00"
Const $WindowTitle  = $ProgramName & " " & $ProgramVersion
; Change the tray icon
TraySetIcon(@SystemDir & "SHELL32.dll", 17)
; Set the tray program name
TraySetToolTip($WindowTitle)
; Store result of _GetPrinters() function
$arrPrinters = _GetPrinters()
; Check if any printers were found
If UBound($arrPrinters) = 1  Then
   MsgBox(16, $WindowTitle, "Error - No Printers Found for user " & @Username)
Else
     
   ; Get the default printer name
   $strDefaultPrinter = _GetDefaultPrinter()
  
   ; Create the menu
   For $i = 1 to UBound($arrPrinters)-1
  
   ; Store the menu handle in the 3rd column
   $arrPrinters[$i][2] = TrayCreateItem($arrPrinters[$i][0])
  
   ; Set the tick mark if this is the current default printer
   If $arrPrinters[$i][0] = $strDefaultPrinter Then
   TrayItemSetState(-1, $TRAY_CHECKED)
   Else
   TrayItemSetState(-1, $TRAY_UNCHECKED)
   EndIf
  
   Next
   ; Create a tray menu seperator
   TrayCreateItem("")
  
   ; Create an Exit option
   $mnuExit = TrayCreateItem("Exit")
  
   While 1
   ; Get any tray menu messages
   $tMsg = TrayGetMsg()
  
   ; Quit if user clicks exit
   If $tMsg = $mnuExit Then Exit
  
   ; Check the tray message is one of our printer tray menu item handles
   If $tMsg >= $arrPrinters[1][2] AND $tMsg <= $arrPrinters[UBound($arrPrinters)-1][2] Then
  
   ; Search the array to find the index of the tray item handle that was clicked
   $intClickItem = _ArraySearch($arrPrinters, $tMsg)
  
   ; If the index is found in the array
   If $intClickItem > -1 Then
   
   ; Loop through all the menu items
   For $i = 1 to UBound($arrPrinters)-1
     
      If $i = $intClickItem Then
      ; Tick the item that was clicked and update registry
      TrayItemSetState($arrPrinters[$i][2], $TRAY_CHECKED)
      RegWrite("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionWindows", "Device", "REG_SZ", $arrPrinters[$i][0] & "," & $arrPrinters[$i][1])
      Else
      ; Untick the menu item
      TrayItemSetState($arrPrinters[$i][2], $TRAY_UNCHECKED)
      EndIf
     
   Next
   
   EndIf
   EndIf
   
   WEnd
EndIf


Func _GetDefaultPrinter()
  
   ; Read the default printer name from the registry
   Local $strDefaultPrinter = RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionWindows", "Device")
  
   ; Only need the part up to the first comma (rest of line is port details)
   $strDefaultPrinter = StringLeft($strDefaultPrinter, StringInStr($strDefaultPrinter, ",")-1)
  
   ; Return the printer name
   Return $strDefaultPrinter
  
EndFunc

Func _GetPrinters()
   ; Local array to hold one row of printer details - printer name, port details, tray menu handle
   Local $arrLocalPrinters[1][3]
   ; Counter for looping through registry
   Local $i = 1
   ; While there are still registry entries to read
   While 1
   ; String location of registry to read
   Local $strREGKEY = "HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts"
  
   ; Get the next printer name from the registry value
   Local $strPrinter = RegEnumVal($strRegKey, $i)
  
   ; Exit if there are no more registry entries
   If @error Then ExitLoop
   ; Read the printer port details for this printer
   Local $strPort = RegRead("HKEY_CURRENT_USERSoftwareMicrosoftWindows NTCurrentVersionPrinterPorts", $strPrinter)
  
   ; Only need the part from start of string to first colon
   $strPort = StringLeft($strPort, StringInStr($strPort, ":"))
   ; Add this printer to the array
   Local $intMax = UBound($arrLocalPrinters)
  
   ; Add an new row to the array
   ReDim $arrLocalPrinters[$intMax+1][3]
   ; Store the printer name in column 1
   $arrLocalPrinters[$intMax][0] = $strPrinter
  
   ; Store the printer port details in column 2
   $arrLocalPrinters[$intMax][1] = $strPort
   ; Increase the loop counter to get the next registry value
   $i += 1
   WEnd
   ; Sort the printer array alphabetically
   _ArraySort($arrLocalPrinters)
   ; Return the array
   Return $arrLocalPrinters
     
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...