CountChildren Sample

Description
This is a simple Phantom script that illustrates using user-defined function recursion to count the total number of child windows that belong to a particular window. The script defines a function that counts the number of children that belong to the input window. For each child, it also recursively calls the function to count the children of the child. The result is the total number of child windows that belong to the input. A second function is also defined that does not recursively call itself, and therefore only counts the direct children of the input window.

The script also shows how to control the execution speed of a script by using the SetDelay function. The SetDelay function determines the delay between Phantom window commands. By default, the delay is 200 milliseconds between Phantom window commands, and is often required to give Windows the chance to respond to a command (e.g., a button press) before continuing on. In this case, since there is no actual window interaction, the delay is set to 0 milliseconds, so the script will run as fast as possible.

Some of the Phantom features used are:

- User-defined functions
- Function recursion
- Local and global variables
- Flow control
- Window child/sibling functions
- MainWin function
- SetDelay function

To use this sample, simply copy and paste it into a new script and save it. Note that if Phantom is installed in a different directory than the default installation, the System command must be changed to use the installation directory.

[ Back to Samples ]

CountChildren
# Phantom Sample Script
# This is a simple script showing how to recursively call
# a phantom function to count the total number of children
# a window has.  It defines two functions, one that counts
# all children through recursive function calls, and one
# that counts only direct children.
#
# This sample illustrates the use of user-defined functions,
# recursion, SetDelay to control script speed, and child /
# sibling functions.

#########################################################
# This function counts the total number of child windows
# under the input window, recursively counting the
# children of each child of the input as well.
function int CountChildren(window Input){
  # Initialize the counter
  int iCount = 0;
  # Get the first child window, if any
  window Child = Input.GetChild();
  # While the Child window is not null, count
  while(Child){
    # Increment the counter for this child
    iCount++;
    # Recursively call this function to count the
    # child's children
    iCount = iCount + CountChildren(Child);
    # Cycle to the next child
    Child = Child.GetNextSibling();
  }
  # Return the total count
  return iCount;
}

############################################################
# This function only counts direct children of the
# input window.
function int CountDirectChildren(window Input){
  int iCount = 0;
  window Child = Input.GetChild();
  while(Child){
    iCount++;
    # Note this function is not called recursively,
    # since only the direct children are counted
    Child = Child.GetNextSibling();
  }
  return iCount;
}

#################################################################
# Begin test code

# Declare and define the PhantomTarget window variable
window PhantomTarget = MainWin("Phantom Target", "*");

# Start PhantomTarget (change this if Phantom is installed
# in a different directory.
System("c:\\Program Files\\Phantom\\PhantomTarget.exe");

# Set delay to 0 milliseconds to make the script run
# as fast as possible, since we aren't interacting with
# the window.
SetDelay(0);

# Call the function to count direct children only
int iCount = CountDirectChildren(PhantomTarget);

# Display the result
disp("Direct Children: " + string(iCount));

# Now count all the children
iCount = CountChildren(PhantomTarget);

# Display the result
disp("All Children: " + string(iCount));

# Close PhantomTarget
PhantomTarget.Close();

Copyright © 2000-2009 Phantom Automated Solutions, Inc.
[ Home ] [ Contact ] [ Privacy Policy ] [System Requirements]