I have 2 HTA scripts that I am trying to put into one. They both do the 75% exact same thing but one looks nicer and the other has a little better functionality. Why not have the best of both?
Script 1 - Has the look and feel of what I want (Shows every object on server (users, groups and printers (Only want printers)
Script 2 - Has the multi-select I want, only shows printers and groups (Only want printers)
How would I combine the two to do what I am asking?
HTA 1 - From "JourneyMan" (http:/
APPLICATIONNAME="AddPrinteromatic"
SCROLL="no"
SINGLEINSTANCE="yes"
WINDOWSTATE="normal"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
SELECTION="no"
>
.button
{
font-family: Helvetica;
font-size: 8pt;
width: 50px;
}
.wmibutton
{
font-family: Helvetica;
font-size: 8pt;
width: 70px;
}
.bigger_button
{
font-family: Helvetica;
font-size: 8pt;
width: 110px;
}
textarea
{
font-family: Arial;
font-size: 8pt;
margin-left: 3px;
margin-right: 3px;
}
select
{
font-family: Arial;
font-size: 8pt;
margin-left: 0px;
}
'********************************************************************
'* BEGIN_SCRIPT
'********************************************************************
'************************
'* Global State Variables
'************************
'********************************************************************
'* g_strPrintServerList
'* This is the list of print server we'll check for the search
'* string. The list needs to be in the following format:
'* "name1,name2,name3"
'*
'* The names need to be the NETBIOS names that the end users can
'* connect to without the leading \\'s.
'********************************************************************
g_strPrintServerList = "PrintServer1,PrinterServer2"
'********************************************************************
'* g_strPrinterFilter
'* This is the filter that we will use to search the list of print
'* servers. Anything that matches this string is returned.
'*
'* ***NOTE***
'* Everything returned is assumed to be a printer. This may not
'* be the case in all circumstances.
'********************************************************************
g_strPrinterFilter = "StringFilter"
'********************************************************************
'* Window_Onload
'* This is the first script that runs when the window loads.
'* We set the focus on it, resize it to fit our needs, and then
'* start adding the printers based on the Global variables
'* defined above.
'********************************************************************
Sub Window_Onload
self.Focus()
self.ResizeTo 520,275
AddPrinterButton.Disabled = True
RemovePrinterButton.Disabled = True
DefaultPrinterButton.Disabled = True
PrintTestButton.Disabled = True
add_printers.InnerHTML = "Waiting for network resources...<" & "/font>"
PleaseWait.style.visibility = "hidden"
self.setTimeout "BuildPrinterList", .1
End Sub
'********************************************************************
'* BuildPrinterList
'*
'* This is the script that builds the select boxes for current
'* network printers and the printers that are on the print server.
'* We set the focus on it, resize it to fit our needs, and then
'* start adding the printers based on the Global variables
'* defined above.
'********************************************************************
sub BuildPrinterList
GetPrinters
strHTML = "
PrinterServers = Split(g_strPrintServerList, ",")
strPrintersToAdd = ""
for each PrintServer in PrinterServers
Set objComputer = GetObject("WinNT://" & PrintServer)
For Each objPrinter in objComputer
If Instr(objPrinter.Name, g_strPrinterFilter) then
'**************************************************************************
'* Originally there was only one print server and the printers were
'* returned in reverse order. So we add the next one in the list to
'* the beginning of the string. This list is sorted in a few more lines
'* but the code works so why change it?
'**************************************************************************
strPrintersToAdd = "
strHTML = strHTML & strPrintersToAdd & ""
add_printers.InnerHTML = strHTML
sortlist("AddPrintersSelect")
AddPrinterButton.Disabled = False
end Sub
'********************************************************************
'* QuitScript
'*
'* When the user presses the Quit button, the file where we've
'* been storing the scripts gets deleted and the main window
'* closes.
'********************************************************************
Sub QuitScript
self.Close()
End Sub
'********************************************************************
'* GetPrinters
'*
'* This adds the network printers already connected to the
'* workstation to the Current Network Printers select box. Once
'* it has completed it enables the buttons that pertain to it.
'********************************************************************
Sub GetPrinters
strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = TRUE")
strPrintersLoaded = ""
do while Printers.Length > 0
Printers.Remove(0)
loop
For Each objPrinter in colPrinters
Set objOption = Document.createElement("OPTION")
objOption.Value = objPrinter.Name
if objPrinter.ShareName <> "" then
objOption.Text = objPrinter.ShareName
else
objOption.Text = objPrinter.Name
end if
if objPrinter.Default = True then
objOption.Text = objOption.Text & " Default"
objOption.Selected = True
end if
Printers.Add(objOption)
Next
RemovePrinterButton.Disabled = False
DefaultPrinterButton.Disabled = False
PrintTestButton.Disabled = False
end Sub
'********************************************************************
'* ShowWaitScript
'*
'* When the user presses the any button, we check the variable
'* to see we display the please wait block and then run the
'* appropriate script.
'********************************************************************
Sub ShowWaitScript(ActionToPerform)
PleaseWait.style.visibility = "visible"
Select Case ActionToPerform
CASE "Add"
self.setTimeout "RunScript", .1
CASE "Remove"
self.setTimeout "RemoveScript", .1
CASE "Default"
self.setTimeout "DefaultPrinterScript", .1
CASE "Test"
self.setTimeout "PrintTestScript", .1
CASE Else
PleaseWait.style.Visibility = "hidden"
end select
End Sub
'********************************************************************
'* RunScript
'*
'* When the user presses the Add Printer button, we check the
'* textbox to see what Printer is being added and whether it is
'* being set to the default.
'********************************************************************
Sub RunScript
if AddPrintersSelect.SelectedIndex = 0 then
msgbox "You have to select a printer from list before one can be added.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
strPrinterToAdd = AddPrintersSelect.Value
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
if (msgbox("Do you want to set this printer as your default?",292, "Set Default")) = 6 then
objNetwork.SetDefaultPrinter strPrinterToAdd
end if
GetPrinters
AddPrintersSelect.SelectedIndex = 0
PleaseWait.style.visibility = "hidden"
End Sub
'********************************************************************
'* RemoveScript
'*
'* When the user presses the Remove Printer(s) button, we check
'* the select box to see what Printer(s) is being removed and
'* whether it is set to the default. If it is the default, set
'* another.
'********************************************************************
Sub RemoveScript
if Printers.SelectedIndex = -1 then
msgBox "You have to select a printer from list before one can be removed.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
if Printers.Length = 1 then
if (msgbox("This is the only printer you have. Are you sure you want to remove it?", 36, "Remove Printer")) = 7 then
PleaseWait.style.visibility = "hidden"
Exit Sub
end if
end if
i = 0
do while i < Printers.Length
if Printers.Item(i).Selected then
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where Name = '" & Replace(Printers.Item(i).Value , "\", "\\") & "'")
For Each objPrinter in objPrinters
if objPrinter.Default then
if (msgbox("This is your default printer. Are you sure you want to remove it?", 36, "Remove Printer")) = 7 then
PleaseWait.style.visibility = "hidden"
Exit Sub
end if
end if
if objPrinter.Default and Printers.Length > 1 then
j = 0
do while Printers.Item(j).Selected and j < Printers.Length
j = j + 1
loop
if j < Printers.Length then
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter Printers.Item(j).Value
end if
end if
objPrinter.Delete_
Next
end if
i = i + 1
loop
GetPrinters
PleaseWait.style.visibility = "hidden"
End Sub
'********************************************************************
'* DefaultPrinterScript
'*
'* When the user presses the Set as Default button, we check the
'* select box to see what Printer(s) is being set and make it the
'* default. Then reorganize the list.
'********************************************************************
sub DefaultPrinterScript
If Printers.SelectedIndex = -1 then
msgbox "You have to select a printer to make default before one can be set.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter Printers.Item(Printers.SelectedIndex).Value
GetPrinters
PleaseWait.style.visibility = "hidden"
end sub
'********************************************************************
'* PrintTestScript
'*
'* When the user presses the Print Test Page, we check the
'* select box to see what Printer(s) is being tested and then
'* send a test page to it. This does not do multiple tests.
'********************************************************************
sub PrintTestScript
if Printers.SelectedIndex = -1 then
msgBox "You have to select a printer from list before a test page can be sent.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where DeviceID = '" & Replace(Printers.Item(Printers.SelectedIndex).Value, "\", "\\") & "'")
For Each objPrinter in colPrinters
errReturn = objPrinter.PrintTestPage
If errReturn = 0 Then
msgbox "The test page was printed successfully to" & Printers.Item(Printers.SelectedIndex).Value & "."
Else
msgbox "The test page could not be printed to" & Printers.Item(Printers.SelectedIndex).Value & "."
End If
Next
PleaseWait.style.visibility = "hidden"
end sub
/*********************************************************************
* sortlist(ddlPrinters)
*
* This utilizes the built in Array.sort function of javascript
* to sort the list of printers so they will be displayed in an
* ascending order. Otherwise the printers are filled in wherever
* and difficult to find. The ddlPrinters variable must be a
* selectbox.
*********************************************************************/
function sortList(ddlPrinters) {
var selectBox = document.getElementById(ddlPrinters);
selectArray = new Array();
if(selectBox.length < 2)
{
return;
}
for (i = 1; i < selectBox.length; i++) {
selectArray[i] = new Array();
selectArray[i][0] = selectBox.options[i].text;
selectArray[i][1] = selectBox.options[i].value;
}
selectArray.sort();
for (j = 0; j < selectBox.length - 1; j++) {
selectBox.options[j+1].text = selectArray[j][0];
selectBox.options[j+1].value = selectArray[j][1];
}
}
| |||||||
|
Script 2 modified version of above by "SuperSanJ" (http:/
APPLICATIONNAME="AddPrinteromatic"
SCROLL="no"
SINGLEINSTANCE="no"
WINDOWSTATE="normal"
MAXIMIZEBUTTON="no"
MINIMIZEBUTTON="no"
SELECTION="yes"
>
.button
{
font-family: Helvetica;
font-size: 8pt;
width: 50px;
}
.wmibutton
{
font-family: Helvetica;
font-size: 8pt;
width: 70px;
}
.bigger_button
{
font-family: Helvetica;
font-size: 8pt;
width: 110px;
}
textarea
{
font-family: Arial;
font-size: 8pt;
margin-left: 3px;
margin-right: 3px;
}
select
{
font-family: Arial;
font-size: 8pt;
margin-left: 0px;
}
'********************************************************************
'* BEGIN_SCRIPT
'********************************************************************
'************************
'* Global State Variables
'************************
'********************************************************************
'* g_strPrintServerList
'* This is the print server we'll check for the search
'* string.
'*
'* The name needs to be the NETBIOS name that the end users can
'* connect to without the leading \\'s.
'********************************************************************
g_strPrintServerList = "print server goes here"
g_strPrinterFilter = "" 'no default prefix, it will be set by getPrinterPrefix function
'********************************************************************
'* Window_Onload
'* This is the first script that runs when the window loads.
'* We set the focus on it, resize it to fit our needs, and then
'* start adding the printers based on the Global variables
'* defined above.
'********************************************************************
Sub getPrinterFilter
Dim fs, ts
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists("currentFilter.txt") then
Set ts = fs.OpenTextFile("currentFilter.txt")
g_strPrinterFilter = Trim(ts.ReadLine) 'read prefix into variable
ts.Close
Else
'do nothing, g_strPrinterPrefix will remain blank
End if
End Sub
Sub Window_Onload
self.Focus()
self.ResizeTo 520,475
AddPrinterButton.Disabled = True
RemovePrinterButton.Disabled = True
DefaultPrinterButton.Disabled = True
PrintTestButton.Disabled = True
add_printers.InnerHTML = "Waiting for Printer List...<" & "/font>"
PleaseWait.style.visibility = "hidden"
self.setTimeout "BuildPrinterList", .1
End Sub
'********************************************************************
'* BuildPrinterList
'*
'* This is the script that builds the select boxes for current
'* network printers and the printers that are on the print server.
'********************************************************************
sub BuildPrinterList
GetPrinters
strHTML = "
PrinterServers = Split(g_strPrintServerList, ",")
strPrintersToAdd = ""
'********************************************************************
'* g_strPrinterFilter
'* This is the filter that we will use to search the print
'* server. Anything that matches this string is returned.
'********************************************************************
getPrinterFilter 'get printer filter and put it in g_strPrinterFilter variable
g_strPrinterFilter = UCase(g_strPrinterFilter)
for each PrintServer in PrinterServers
Set objComputer = GetObject("WinNT://" & PrintServer)
For Each objPrinter in objComputer
If InStr(objPrinter.Name, g_strPrinterFilter) AND Right(objPrinter.name,1) <> "$" then
strPrintersToAdd = "
End If
Next
Next
strHTML = strHTML & strPrintersToAdd & ""
add_printers.InnerHTML = strHTML
sortlist("AddPrintersSelect")
AddPrinterButton.Disabled = False
end Sub
'********************************************************************
'* QuitScript
'*
'* When the user presses the Quit button, the file where we've
'* been storing the scripts gets deleted and the main window
'* closes.
'********************************************************************
Sub QuitScript
self.Close()
End Sub
'********************************************************************
'* GetPrinters
'*
'* This adds the network printers already connected to the
'* workstation to the Current Network Printers select box. Once
'* it has completed it enables the buttons that pertain to it.
'********************************************************************
Sub GetPrinters
strComputer = "."
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Network = TRUE")
strPrintersLoaded = ""
do while Printers.Length > 0
Printers.Remove(0)
loop
For Each objPrinter in colPrinters
Set objOption = Document.createElement("OPTION")
objOption.Value = objPrinter.Name
if objPrinter.ShareName <> "" then
objOption.Text = objPrinter.ShareName
else
objOption.Text = objPrinter.Name
end if
if objPrinter.Default = True then
objOption.Text = objOption.Text & " Default"
objOption.Selected = True
end if
Printers.Add(objOption)
Next
RemovePrinterButton.Disabled = False
DefaultPrinterButton.Disabled = False
PrintTestButton.Disabled = False
end Sub
'********************************************************************
'* ShowWaitScript
'*
'* When the user presses any button, we check the variable
'* to see we display the please wait block and then run the
'* appropriate script.
'********************************************************************
Sub ShowWaitScript(ActionToPerform)
PleaseWait.style.visibility = "visible"
Select Case ActionToPerform
CASE "Add"
self.setTimeout "RunScript", .1
CASE "Remove"
self.setTimeout "RemoveScript", .1
CASE "Default"
self.setTimeout "DefaultPrinterScript", .1
CASE "Test"
self.setTimeout "PrintTestScript", .1
CASE Else
PleaseWait.style.Visibility = "hidden"
end select
End Sub
'********************************************************************
'* RunScript
'*
'* When the user presses the Add Printer button, we check the
'* textbox to see what Printer is being added and add that printer
'********************************************************************
Sub RunScript
if AddPrintersSelect.SelectedIndex = 0 then
msgbox "You have to select a printer from list before one can be added.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
strPrinterToAdd = AddPrintersSelect.Value
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
strPrinterToAdd = AddPrintersSelect.Value & "_mono_simplex$"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
On Error Resume Next
strPrinterToAdd = AddPrintersSelect.Value & "_colour_simplex$"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
On Error Resume Next
strPrinterToAdd = AddPrintersSelect.Value & "_colour_duplex$"
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection strPrinterToAdd
GetPrinters
AddPrintersSelect.SelectedIndex = 0
PleaseWait.style.visibility = "hidden"
End Sub
'********************************************************************
'* RemoveScript
'*
'* When the user presses the Remove Printer(s) button, we check
'* the select box to see what Printer(s) is being removed and
'* remove it.
'********************************************************************
Sub RemoveScript
if Printers.SelectedIndex = -1 then
msgBox "You have to select a printer from list before one can be removed.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
i = 0
do while i < Printers.Length
if Printers.Item(i).Selected then
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where Name = '" & Replace(Printers.Item(i).Value , "\", "\\") & "'")
For Each objPrinter in objPrinters
objPrinter.Delete_
Next
end if
i = i + 1
loop
GetPrinters
PleaseWait.style.visibility = "hidden"
End Sub
'********************************************************************
'* DefaultPrinterScript
'*
'* When the user presses the Set as Default button, we check the
'* select box to see what Printer(s) is being set and make it the
'* default. Then reorganize the list.
'********************************************************************
sub DefaultPrinterScript
If Printers.SelectedIndex = -1 then
msgbox "You have to select a printer to make default before one can be set.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
Set objNetwork = CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter Printers.Item(Printers.SelectedIndex).Value
GetPrinters
PleaseWait.style.visibility = "hidden"
end sub
'********************************************************************
'* PrintTestScript
'*
'* When the user presses the Print Test Page, we check the
'* select box to see what Printer(s) is being tested and then
'* send a test page to it. This does not do multiple tests.
'********************************************************************
sub PrintTestScript
if Printers.SelectedIndex = -1 then
msgBox "You have to select a printer from list before a test page can be sent.", 48, "Select Printer"
PleaseWait.style.visibility = "hidden"
exit sub
end if
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery("Select * from Win32_Printer Where DeviceID = '" & Replace(Printers.Item(Printers.SelectedIndex).Value, "\", "\\") & "'")
For Each objPrinter in colPrinters
errReturn = objPrinter.PrintTestPage
If errReturn = 0 Then
msgbox "The test page was printed successfully to" & Printers.Item(Printers.SelectedIndex).Value & "."
Else
msgbox "The test page could not be printed to" & Printers.Item(Printers.SelectedIndex).Value & "."
End If
Next
PleaseWait.style.visibility = "hidden"
end sub
/*********************************************************************
* sortlist(ddlPrinters)
*
* This utilizes the built in Array.sort function of javascript
* to sort the list of printers so they will be displayed in an
* ascending order. Otherwise the printers are filled in wherever
* and difficult to find. The ddlPrinters variable must be a
* selectbox.
*********************************************************************/
function sortList(ddlPrinters) {
var selectBox = document.getElementById(ddlPrinters);
selectArray = new Array();
if(selectBox.length < 2)
{
return;
}
for (i = 1; i < selectBox.length; i++) {
selectArray = new Array();
selectArray[0] = selectBox.options.text;
selectArray[1] = selectBox.options.value;
}
selectArray.sort();
for (j = 0; j < selectBox.length - 1; j++) {
selectBox.options[j+1].text = selectArray[j][0];
selectBox.options[j+1].value = selectArray[j][1];
}
}
| |||||||
|