Firefox open URL only if not exist - firefox

I have made this script so it check for new content, it works fine, my only problem is that I would like to only open the URL if its not being loaded. and my script is this one
On Error Resume Next
Dim WebPage(2)
WebPage(0) = "http://domain/page.aspx?page=1"
WebPage(1) = "http://domain/page.aspx?page=2"
waitTime= 60000 'wait period in milliseconds
Do
Set o = CreateObject("WScript.Shell")
o.Run "firefox.exe " & WebPage(i)
WScript.Sleep waitTime
Set o = Nothing
' check our loop
i = i + 1
' reset no
If i = 2 Then
i = 0
End If
Loop

Related

Silently run EXE with VBScript

I have created a script which checks for the idle time using a program MousePos.exe and pushes the response back to itself to work out whether there has been any mouse movement. This all works fine, however everytime the MousePos.exe runs it flashes on the screen and I want to hide this. I have no idea how to do this, I expected it would be quite simple.
I was advised I need to Use the WshShell object's Run method instead of the WshScriptExec object but I have no idea how to do this and still read in the response from the EXE.
Function execStdOut(cmd)
Dim goWSH : Set goWSH = CreateObject("WScript.Shell")
Dim aRet: Set aRet = goWSH.Exec(cmd)
execStdOut = aRet.StdOut.ReadAll()
End Function
'wait 2 seconds for things to calm now
WScript.Sleep 2000
'get initial Mouse XY
MPOS = execStdOut("cmd /c C:\Windows\MousePos.exe")
WScript.Sleep 10000 '10 seconds
'set initial idle counter
Idle = 0
Do While forever = 0
OLD = MPOS
MPOS = execStdOut("cmd /c C:\Windows\MousePos.exe")
If OLD = MPOS Then
Idle = Idle + 1
If idle = 12 Then '12 x 10 seconds for 2 minute timeout
idle = 0
Set objShell = WScript.CreateObject("WScript.Shell")
objshell.Run "C:\Windows\IERestart_countdown.hta", 1, True
End If
Else
idle = 0
End If
WScript.Sleep 10000 ' 10 seconds
Loop

'Cannot find file error' from SAP VBScript

I'm running a script that extracts from SAP a number of N archives(based on a input file that contains the name of each file). The scripts reads the data for each file from a input txt file and writes the status after each run to an Excel file.
After each run the file is generated but before being saved it gives this
error.
Windows cannot find 'filename.zip'. Make sure you typed the name correctly and then try again
I have disabled the Read Only option of the folder where I'm saving the report, I've run sfc /scannow in case I have any corrupted files in my system and I have also checked the Path variables in case any were missing but to no avail.
Here is the code
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
Set filesysobj = CreateObject("Scripting.FileSystemObject")
Set list = CreateObject("Scripting.Dictionary")
Set file = filesysobj.OpenTextFile("fullpath\to\input\file\.txt", 1)
' wb - workbook(the file), ws-worksheet(the first sheet of the excel file)
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("fullpath\to\excel\output\file")
objExcel.Visible = False
Set sheet = objWorkbook.sheets("Sheet1")
' Reading from the input file
nr = 0
Do until file.AtEndOfStream
request = file.ReadLine
list.Add nr, request
nr = nr + 1
Loop
file.Close
' Indexes for the lines and rows in the excel file
i = 2 ' Second Row
j = 5
For Each line in list.Items
On Error Resume Next
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "controlpanel\path"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[2]").press
' line - name from file
session.findById("wnd[0]/usr/ctxtZQT1D-REQ").text = line
session.findById("wnd[0]/usr/ctxtZQT1D-REQ").caretPosition = 9
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsREQUEST_OBJECTS/tabpREQUEST_OBJECTS_FC6/ssubREQUEST_OBJECTS_SCA:/SYMSOFT/CP_ZQTMS010_NEW:0917/cntlCONTAINER/shellcont/shell").currentCellColumn = "DATE"
session.findById("wnd[0]/usr/tabsREQUEST_OBJECTS/tabpREQUEST_OBJECTS_FC6/ssubREQUEST_OBJECTS_SCA:/SYMSOFT/CP_ZQTMS010_NEW:0917/cntlCONTAINER/shellcont/shell").selectedRows = "0"
session.findById("wnd[0]/usr/tabsREQUEST_OBJECTS/tabpREQUEST_OBJECTS_FC6/ssubREQUEST_OBJECTS_SCA:/SYMSOFT/CP_ZQTMS010_NEW:0917/cntlCONTAINER/shellcont/shell").pressToolbarButton "ATT_DISP"
' define of the output path for the file & the filename
session.findById("wnd[1]/usr/ctxtDY_PATH").text = "fullpath\saving\folder"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = line & ".zip"
session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 26
session.findById("wnd[1]/tbar[0]/btn[0]").press
' -------Writing the errors
' End If
If Err.Number <> 0 Then
sheet.Cells(i, j).Value = "Error " & Err.Description
i = i + 1
Else
sheet.Cells(i, j - 1) = line
sheet.Cells(i, j) = "has been completed"
i = i + 1
End If
next
objExcel.ActiveWorkbook.Save
objExcel.Quit
The archives are saved correctly but without clicking OK on the error the script won't get past 1 iteration nor it will save the archive.

How can I use an already open IE with VB Script? GetObject is not working

Below is the code block i used to open a new window. I tried using GetObject in place of Create Object as below. But did not work.
Set IE = GetObject("","InternetExplorer.Application")
'
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate "https://msn.com"
Do While IE.ReadyState <> 4
WScript.Sleep 1000
Loop
Set ElementCol = IE.Document.getElementsByTagName("a")
For Each Link In ElementCol
If Link.innerHTML = "News" Then
Link.Click
WScript.Sleep(6000)
Exit For
End If
Next
IE.navigate "https://msn.com"
This uses Explorer which used to do the internet.
Set objShell = CreateObject("Shell.Application")
Do
Set AllWindows = objShell.Windows
Count = 0
For Each window in AllWindows
msgbox window.locationname
' window.refresh2 3
Count = Count + 1
Next
If Count = 0 then Exit Do
Wscript.sleep 5000
Loop

VBS Object required error, 800A01A8

Hello I'm trying to debug this script that I inherited below. The error is on line 71 char 6. Object required 'oFile'
I don't have any vbs experience so please be gentle. This script takes a scan and uploads it to a doc archive server, gives it unique filename etc. I haven't figured out what 'ofile' is yet :/
'Feb 18, 2005
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
hiddenCount = 0
'Wscript.Echo Wscript.Arguments(0)
If Wscript.Arguments.Count = 1 And oFSO.FolderExists(Wscript.Arguments(0)) Then
Set scanFiles = oFSO.GetFolder(Wscript.Arguments(0)).Files
For Each oFile In scanFiles
If oFile.Attributes and 2 Then
hiddenCount = hiddenCount + 1
End If
Next
Else
Set scanFiles = WScript.Arguments
End If
fileCount = scanFiles.Count - hiddenCount
'Wscript.Echo hiddenCount
'WScript.Echo fileCount
'WScript.Quit()
Set oIE = WScript.CreateObject("InternetExplorer.Application")
oIE.left=50 ' window position
oIE.top = 100 ' and other properties
oIE.height = 300
oIE.width = 350
oIE.menubar = 0 ' no menu
oIE.toolbar = 0
oIE.statusbar = 1
oIE.navigate "http://gisweb/apps/doc_archive/scan_login.php?file_count="&fileCount
'WScript.Echo fileCount
oIE.visible = 1
' Important: wait till MSIE is ready
Do While (oIE.Busy)
Loop
' Wait till the user clicks the OK button
' Use the CheckVal function
' Attention: Thanks to a note from M. Harris, we can make
' the script a bit more fool proof. We need to catch the case
' that the user closes the form without clicking the OK button.
On Error Resume Next
Do ' Wait till OK button is clicked
WScript.Sleep 400
Loop While (oIE.document.script.CheckVal()=0)
' If an error occur, because the form is closed, quit the
' script
If err <> 0 Then
WScript.Echo "Sorry, a run-time error occured during checking" & _
" the OK button " & vbCRLF & _
"Error: " & err.number & " " & _
"I guess the form was getting closed..."
WScript.Quit ' end script
End if
On Error Goto 0 ' switch error handling off
' User has clicked the OK button, retrieve the values
docList = oIE.Document.ValidForm.doc_id_list.Value
'MsgBox doc_id
For i = 0 To 100000
x = 1
Next
oIE.Quit() ' close Internet Explorer
Set oIE = Nothing ' reset object variable
docArray = Split(docList,",")
i = 0
For Each oFile In scanFiles
If Not oFile.Attributes And 2 Then **ERROR HERE**
ext = oFSO.GetExtensionName(oFile)
filename = "p"&right("000000"&docArray(i),6)&"."&ext
base = Int(docArray(i) / 500) * 500
subdir = "d"&right("000000"&base,6)
oFSO.CopyFile oFile, "\\ditgis02\Enterprise_GIS\doc_archive\raw\"&subdir&"\"&filename, True
i = i + 1
End If
Next
If Wscript.Arguments.Count = 1 And oFSO.FolderExists(Wscript.Arguments(0)) Then
Set WshShell = WScript.CreateObject("WScript.Shell")
intButton = WshShell.Popup (fileCount&" file(s) logged and copied! Do you want to delete temporary scan files?",0,"Done!",4)
If intButton = 6 Then
For Each oFile In scanFiles
oFile.Delete
Next
End If
Else
WScript.Echo(fileCount&" file(s) logged and copied!")
End If
WScript.Quit() ' Ready
' End
It looks the problem may arise if your initial test fails:
If Wscript.Arguments.Count = 1 And oFSO.FolderExists(Wscript.Arguments(0)) Then
...
Else
Set scanFiles = WScript.Arguments
End If
You're setting the scanFiles variable to a collection of arguments, not files. Later on, near line 71 (where your error occurs), you're treating scanFiles as if it's a Files collection and attempting to access the Attributes property of one of its File objects:
For Each oFile In scanFiles
If Not oFile.Attributes And 2 Then **ERROR HERE**
...
Next
This won't be possible since scanFiles is an Arguments collection instead. So I think you need to fix your initial Else clause to either terminate your script or provide some kind of "default" Files collection.

measuring print job time programmatically

I need to measure print job time, which means time between 'send print command' and 'print job disappear from the print queue'
so I am trying to do these things by script
search all pdf files
print a file
get the print time (as above)
go to next file and do all above for all files
this is my work so far(i omit some parts)
For Each file In objFolder.Items
' check for the extension
if objFSO.GetExtensionName(file.name) = "pdf" then
' invoke to print
file.InvokeVerbEx( "Print" )
' select print jobs
Set Printers = objWMIService.ExecQuery _
("Select * from Win32_PrintJob")
For Each objPrinter in Printers
DateTime.Value = objPrinter.TimeSubmitted
TimeinQueue = DateDiff("n", actualTime, Now)
Wscript.Echo TimeinQueue
Next
end if
next
mainly i need to ask how can i get the time when print job disappear from the print queue.
And I need to keep next job till one print job ends.
any ideas ?
There's no simple way to obtain that information from inside your script, because when the job is removed from the print queue it's gone. You could set up an event monitor for the print spooler like this:
Set wmi = GetObject("winmgmts://./root/cimv2")
Set wbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
qry = "SELECT * FROM __InstanceOperationEvent WITHIN 1 " & _
"WHERE TargetInstance ISA 'Win32_PrintJob'"
Set mon = wmi.ExecNotificationQuery(qry)
Do
Set evt = mon.NextEvent
If evt.Path_.Class = "__InstanceDeletionEvent" Then
wbemDateTime.Value = evt.TargetInstance.TimeSubmitted
WScript.Echo evt.TargetInstance.Document & ": " & _
DateDiff("n", wbemDateTime.GetVarDate, Now)
End If
Loop
However, you'd have to run that from a different script, because VBScript doesn't support multi-threading (i.e. running things in parallel), so the event handler loop would block the rest of your script operations.
If you want a rough value from within your script, you could try something like this, but don't expect queue times to be very accurate:
'singleton SWbemDateTime instance for time conversions
Set wbemDateTime = CreateObject("WbemScripting.SWbemDateTime")
'global list to keep track of printing documents
'MUST NOT BE MODIFIED FROM ANYWHERE EXCEPT CheckPrintQueue!
Set printingDocs = CreateObject("Scripting.Dictionary")
Function CheckPrintQueue
Set printJobs = objWMIService.ExecQuery("SELECT * FROM Win32_PrintJob")
Set currentDocs = CreateObject("Scripting.Dictionary")
'get currently printing jobs from queue
For Each job In printJobs
currentDocs.Add job.Document, job.TimeSubmitted
Next
'compare global list to current list, print the queue time for documents
'that are no longer queued, and remove them from the global list
For Each doc In printingDocs.Keys
If Not currentDocs.Exists(doc) Then
wbemDateTime.Value = printingDocs(doc)
WScript.Echo doc & ": " & DateDiff("n", wbemDateTime.GetVarDate, Now)
printingDocs.Remove doc
End If
Next
'add new documents to global list
For Each doc In currentDocs.Keys
If Not printingDocs.Exists(doc) Then printingDocs.Add doc, currentDocs(doc)
Next
CheckPrintQueue = printJobs.Count
End Function
For Each file In objFolder.Items
If objFSO.GetExtensionName(file.name) = "pdf" Then
file.InvokeVerbEx "Print"
CheckPrintQueue
End If
Next
'wait until all jobs finished printing
Do While CheckPrintQueue > 0
WScript.Sleep 100
Loop
I was looking for exact thing and I simply summurized ansgar's script to get what I want.
its take every pdf files and print 5 times each(so i can get a avarage which better) while getting the time.finally print them in to a csv file. and untill one going to desapear from the print queue others wait waits. Not 100% acuurate but enough for the perfomance comparision
may be it may usefull (please correct my mistakes if any)
Dim objFSO, outFile
const forAppending = 8
const useDefault = -2
'Output File name
fileName = ".\output.csv"
Set objFSO = CreateObject("Scripting.FileSystemObject")
set outFile = objFSO.OpenTextFile(fileName, forAppending, true, useDefault)
'write heading to output file
outFile.WriteLine("Filename,1,2,3,4,5")
'get current Folder
strFolder = Replace(wscript.scriptfullname,wscript.scriptname,".\")
strFolder = objFSO.getFolder(strFolder)
'Open the Shell Folders object
Set objShell = CreateObject( "Shell.Application" )
'Create an object for the specified file's parent folder
Set objFolder = objShell.Namespace( strFolder )
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
For Each file In objFolder.Items
If objFSO.GetExtensionName(file.name) = "pdf" Then
'write in to out file
outFile.write(file.name)
outFile.writeLine ""
'outer loop for 5 times per each file
index = 0
do while index < 5
'if already doing a printing do not start a new one
if CheckPrintQueue() = 0 then
file.InvokeVerbEx "Print"
startTime = timer
waitTostart
'if first time on outer loop it should be empty queue
else
if index = 0 then
wscript.echo "queue doesnt empty"
finish
end if
end if
'wait until all jobs finished printing
waitToEnd
'count time
ellapsTime = timer - startTime
'write in to out file
outFile.write(",")
outFile.Write(ellapsTime)
index = index + 1
loop
End If
Next
outFile.Close
'----------------function CheckPrintQueue-----------------------
Function CheckPrintQueue()
Set printJobs = objWMIService.ExecQuery("SELECT * FROM Win32_PrintJob")
Set currentDocs = CreateObject("Scripting.Dictionary")
CheckPrintQueue = printJobs.Count
End Function
'----------------end of function-----------------
sub waitTostart
Do
WScript.Sleep 100
'check print queue
printJobCount = CheckPrintQueue()
Loop until printJobCount > 0
end sub
sub waitToEnd
Do
WScript.Sleep 100
'check print queue
printJobCount = CheckPrintQueue()
Loop until printJobCount = 0
end sub
sub finish
outFile.Close
objFSO = nothing
objFolder = nothing
objShell = nothing
objWMIService = nothing
wscript.quit 1
end sub

Resources