Searching for an open window by title

This lesson demonstrates how to search for an open stack by label, rather than by name.

The titledWindowIsOpen function

The titledWindowIsOpen function checks whether a stack window is open, but using its label property instead of its name. (Usually, you use the name of a stack instead, because it's unique--only one stack with a particular name can be open at a time, while several open stacks might have the same label.) If there is an open stack window with the specified title, the function returns true; if there is no such window, it returns false.

get titledWindowIsOpen("Untitled 2")
put titledWindowIsOpen(the selectedText) into tOpen
set the hilite of button "Open" to titledWindowIsOpen(it)

Getting a list of stacks

First, the handler gets a list of all open stacks, using the openStacks function, and puts it into a variable called tOpenStacksList.

put the openStacks into tOpenStacksList

Finding the labels of the open stacks

The openStacks function returns the name of stack windows, not their label so to find out whether there is an open window with a certain title, we need to check the label property of each stack returned by the openStacks. This is done in a repeat loop. The loop goes through each of the lines in the tOpenStacksList variable and checks each stack's label property. If the label matches the window title we're looking for, the function returns true.

repeat for each line tStack in tOpenStacksList
	if the label of stack tStack is pTitle  then return true
end repeat

The return control structure halts the handler it's in, so as soon as we find a stack with the right title, the handler ends. If the handler checks all the open stacks and doesn't find one with the right title, it returns false.

return false

The titledWindowIsOpen function code

function titledWindowIsOpen pTitle
	local tOpenStacksList
   
	## get the list of open stack names
	put the openStacks into tOpenStacksList
   
	## check whether the title we want matches the label
	## of any open stack
	repeat for each line tStack in tOpenStacksList
		if the label of stack tStack is pTitle  then return true
	end repeat
   
	## if we reach this point, the function hasn't returned yet,
	## so the title wasn't found to belong to any open stack
	return false
end titledWindowIsOpen

A note on efficiency

We've done a couple of things in this handler to speed it up. One is to place the list returned by the openStacks function in a variable. We could have instead checked the openStacks directly by omitting the line put the openStacks into openStacksList and changing the first line of the repeat loop to this:

repeat for each line tStack in the openStacks

Done this way, the handler still works the same way. But the repeat loop must retrieve a stack name once for each open stack, and it's faster to check the contents of a variable than to check the value returned by a function.

The other thing we've done to make this handler faster is to use the repeat for each form of the repeat control structure. A more traditional way to write the repeat loop would be like this:

repeat with x = 1 to the number of lines in tOpenStacksList
	if the label of stack (line x of tOpenStacksList) is pTitle then return true
 end repeat

Many languages support this form of repeat loop, and Transcript does too: this form will work the same way as the one we used. However, the repeat for each form is much faster--depending on the circumstances, up to hundreds of times faster--than the repeat with x = start to end form. This handler normally has to check only a few open stacks, rather than repeating hundreds or thousands of times, so these speedups may not make much practical difference in this case. But these are good habits to learn for other handlers that are more time-consuming.

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.