The GW-Scripting list is a discussion list for information regarding the development and use of Window-Eyes scripts. Any subscriber of the GW-Scripting list has the ability to post on-topic messages.

From:

 Allison and Chip Orange

Subject:

 draft of examples for Sunday night scripting class

Date:

 Sat, Apr 2, 2011 5:09:17 pm
Hi all,

per request, below are the examples (draft version) for tomorrow's class
(4/3/2011). for those who attend via TeamTalk, they will be uploaded to the
channel no later than 6:50 EDT, so you should have time to download them
(and they may be different than these below), and have a look before class.

--------

' class #7 examples: (4/3/2011)


Below is some brief documentation for the scripting.dictionary object, which
is used frequently in VBScript:


Dictionary Object Properties and Methods:


..CompareMode sets or returns the type of comparison used for comparing key
values: vbTextCompare (default) or vbBinaryCompare
..Count Returns the number of key/item pairs in the Dictionary
..Item(key) Sets or returns the value of the item for the specified key
..Key(key) Sets or returns the value of a key
..Add (key, item) Adds the key/item pair to the Dictionary.
You can also add items with a simple assignment, and in fact, you
must use this syntax in order to store object references in a dictionary:
Set objDict("keyname") = objMyObject
..Exists (key) Returns true if the specified key exists or false if not.
..Items () Returns an array containing all the items in a Dictionary object.
..Keys () Returns an array containing all the keys in a Dictionary object.
..Remove (key) Removes a single key/item pair specified by key.
..RemoveAll () Removes all the key/item pairs.

An error will occur if you try to add a key/item pair when that key already
exists, remove a
key/item pair that doesn't exist, or change the CompareMode of a Dictionary
object that
already contains data.



' example 1
' shows a dictionary object being used to hold and retrieve data

' below, employees names are added to a dictionary, which uses an employee
ID number as the key:
dim objEmployees
set objEmployees = createObject("scripting.dictionary")
objEmployees.compareMode = vbTextCompare ' case insensitive

objEmployees.add "123-45-6789", "Chip Orange"

objEmployees.item("432-22-3344") = "Julie Smith"
' or
objEmployees("432-22-3344") = "Julie Smith"

' both of these forms work to add an employee; however, if an employee with
the given ID number was already in the dictionary, and the .add method was
used, it would yield an error because that key was already in use.
' the second form, where the key is specified in parrens after the object
variable's name, would simply overwrite any existing information with the
same key value.
' The second form itself has two different ways to use it; this is because
"item" is the default property of the dictionary object,
' and so you do not have to specify the name of the default property

' in fact, this second form would add an empty employee item even when you
didn't mean to if used like this:

dim name
name = objEmployees("123-45-5544")
' if the key value didn't exist, just referencing it in this form would
automatically create it with an empty item.
' to prevent this, use the .exists method:
if objEmployees.exists("123-45-5544") then
name = objEmployees("123-45-5544")
end if

' end of example 1


' example 2
' shows how to process all the items in a dictionary
' assume the variable objEmployees from the example 1 exists and is a
dictionary object which has been filled with employee names

' get all the employee names into a long string and display it
dim s
s = ""


dim objCur

for each objCur in objEmployees.items
' the loop control variable here holds an element from the items array (the
names placed into the dictionary)
s = s & objCur & vbCR
next

msgBox "Employees are: " & s

' here's a variation which does the same thing

for each objCur in objEmployees.keys
' the loop control variable here holds an element from the keys array (the
ID numbers)
s = s & objEmployees(objCur) & vbCR
next

msgBox "Employees are: " & s
' note that even though the dictionary object itself is a collection, when
you use the .keys or .items property in order to get access to all the keys
or their values at one time, what is returned is an array; not a collection.
' also note that the FOR EACH ... LOOP structure works with arrays as well
as collections.

' end of example 2


' example 3
' using an event handler to watch newly opened windows

option explicit
' this is a fully working script, which you could load on your system by:
' * copy it into a file in your window-eyes profile directory with a .vbs
extension
' * go into the app manager and set it to display global apps
' * use the load command button in the app manager to load the newly created
..vbs file

' it doesn't however meet all the standards for a proper script (help,
script menu, etc.)

dim n
dim NP
set NP = createObject("scripting.dictionary") ' this dictionary will hold
the titles of all NotePad windows

' now use the window object's onChildCreate event, and do it on the desktop
window itself, to look for new windows
n = connectEvent (desktopWindow, "myNewWindowHandler", "onChildCreate")

' end of main body


sub myNewWindowHandler(win)
' this is the event handler for the onChildCreate event of the desktop
window

' since we may say something, and we also need to pause slightly to allow
the application to have time enough to set the window title, we have to use
the queue method
queue "checkWin", win

end sub

sub checkWin(win)

sleep 500 ' give application half a second to initialize window

' below tests to see if we have a top level window (we haven't covered this
yet, just take my word)
if win.style.overlapped or win.style.MDIChild then
' it is an application top-level window
if win.className = "Notepad" then
' it's a notepad document window
if NP.exists(win.title) then
' found this window title in the dictionary
silence
sleep 1000 ' pause one second after going quiet
speak "Warning! " & win.title & " is opened more than once!"
else
' first time opening, just add it to the dictionary
NP.add win.title, win.title
end if ' NP.exists(win.title)
end if ' win.className = "Notepad"
end if ' win.style.overlapped or win.style.MDIChild

end sub

' end of example 3