Printing all the cards that contain a word

This lesson demonstrates how to print all the cards that contain a given string.

The printCardsWith command

The printCardsWith command takes 2 parameters

pStringToFind - the string to search for

pFieldName - the name of the field to search in (optional)

The syntax of the command is:

printCardsWith pString, [pFieldName]

The command is used with a statement such as the following:

printCardsWith "Hello"
printCardsWith "Smith","Last Name"

Typically, you will call it in a button's mouseUp handler: clicking the button prints the cards.

The mark command

The heart of this handler is the mark command. To print cards containing a string, we first need to somehow select those cards, then print that selected set. The mark command selects a subset of the cards in a stack, based on whatever criteria you choose, and sets the mark property of those cards to true. Once the cards we want are marked, we can use the print command to print just those cards whose mark property is true.

In this case, first we use the unmark command to get rid of any marks that may have been set previously. You should always use the statement unmark all cards before using the mark command, unless you know which cards have already been marked. Otherwise, cards you haven't specified end up as part of the set of marked cards, if they were marked before.

unmark all cards

Marking cards

Next, we check whether a field name was provided. The printCardsWith handler takes two parameters, the string to look for and the name of a field. You'll notice that you can call the handler with just one parameter (the string to find). It's possible to do this because we've used an if structure to check whether there's a second parameter. You can use this trick in any handler to make a parameter optional:  just check whether the parameter is empty. If it is, then no value has been passed for it. In this case, the handler simply checks all the fields for a match.

This example handler uses the mark cards by finding form of the mark command. The command works the same way (and has the same options) as the find command, but instead of taking you to the first found card, it marks all the found cards. Once the cards we want are marked, we can print just the marked cards using the print marked cards form of the print command. The sort and go commands also have forms that work only on the marked cards.

if pFieldName is empty then 
	## search all fields
	mark cards by finding pStringToFind
	## search the specified field:
	mark cards by finding pStringToFind in field pFieldName
end if
print marked cards

The printCardsWith command code

on printCardsWith pStringToFind, pFieldName
	unmark all cards
	if pFieldName is empty then 
		## search all fields
		mark cards by finding pStringToFind
		## search the specified field:
		mark cards by finding pStringToFind in field pFieldName
	end if
	print marked cards
end printCardsWith

A variation - searching for more that one word

A different find form would change the way the search is done. For example, if a string containing more than one word is specified, the printCardsWith handler will find all the cards that contain each of the words (either as an entire word, or as the beginning of a word on the card). To mark only cards that contain the exact phrase, you change the find form to find string:

mark cards by finding string pStringToFind in field pFieldName

Other forms of the mark command

The mark command can be used with any attribute of a card, not just with the mark cards by finding form. Finding is convenient if you want to mark cards based on the text in their fields, but you can also mark cards based on the highlight state of radio buttons, the name of the card, custom properties of the card, or anything else:

mark cards where the hilite of button "Student" is true
mark cards where "Help" is in the name of this card
mark cards where the storedStatus of this card is "On Duty"

Printing several cards per page

This example handler prints one card per page. But what if your cards are small and you want to print several on a page? Use the open printing command:

open printing
print marked cards
close printing

Every print command between the open printing command and the close printing command is placed in a single print job.

Using the set of marked cards

The print, sort, and go commands are the only ones with special forms for marked cards, but you can do any action at all on the set of marked cards by using a repeat control structure and performing the operation on each marked card:

repeat with x = 1 to the number of marked cards
	go to marked card x
	put field 2 of marked card x after myData
	## or do anything you want on the marked card
end repeat

To use this handler on a stack that is not the current stack, first set the defaultStack to the name of the stack that has the cards you want to select and print:

set the defaultStack to "My Database Stack"
printCardsWith "Jones","Last Name"


Add your comment

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