Count AppleScript command, application command
The Count command can function as an AppleScript command or an application command. The AppleScript command counts the number of elements of a particular class in a list, record, or string. The application command counts the number of elements of a particular class in an object
or objects.APPLESCRIPT COMMAND SYNTAX
count [ [ each | every ] className ( in | of ) ] compoundValue number of [ pluralClassName ( in | of ) ] compoundValueAPPLICATION COMMAND SYNTAX
count [ each | every ] className [ ( in | of ) referenceToObject ] number of className [ ( in | of ) referenceToObject ]PARAMETERS
- className
- The class name of the elements to be counted. If you use the term each or every, you can use only the singular form of the class name. The elements of lists, records, and strings are listed in the value class definitions in Chapter 3, "Values." The elements of application objects are listed in their object class definitions in the application dictionary.
Class: Class identifier
Default value: Item for lists, records, and application objects; Character for strings (see "Notes")- compoundValue
- An expression that evaluates to a compound value whose elements are to be counted.
Class: List, record, reference, or string- pluralClassName
- The plural class name of the elements to be counted. The elements of lists, records, and strings are listed in the value
class definitions in Chapter 3, "Values."
Class: Class identifier
Default value: Item for lists, records, and application objects; Character for strings (see "Notes")- referenceToObject
- A reference to the object or objects whose elements are to be counted. If you do not specify this parameter, the application counts the elements in the default target of the Tell statement.
Class: List, record, reference, or stringRESULT
The result of the AppleScript command is an integer that specifies the number of elements of a specified class in a compound value.The result of the application command is either an integer or a list of integers. See "Notes" for details.
Class: Integer or list of integers
EXAMPLES
In the following example, compoundValue is a list. The command does not explicitly specify a class of elements to count, so AppleScript counts all the items in the list.
count {"Yes", "No", "Maybe", 4, 5, 6} --result: 6In this example, className isintegers
and referenceToObject is a list of strings and integers. AppleScript counts the integers in the list.
count integers in {"Yes", "No", "Maybe", 4, 5, 6} --result: 3This example shows another way to count the integers in the list:
count each integer in {"Yes", "No", "Maybe", 4, 5, 6} --result: 3In the following example, every word of document "simple" consists of a list of words. The Scriptable Text Editor counts the words in the list.
tell application "Scriptable Text Editor" count every word of document "simple" end tell --result: 12The following statement is equivalent to the previous example:
tell application "Scriptable Text Editor" count words of document "simple" end tellIn the following example, referenceToObject isdocuments of application "Scriptable Text Editor"
, which is a list of documents. The Scriptable Text Editor counts the documents in the list.
tell application "Scriptable Text Editor" repeat with i from 1 to (count of documents) set the style of paragraph 1 of document i to ÿ {outline, bold} end repeat end tellNOTES
If you use the Count command on a string without specifying the class to be counted, AppleScript counts the characters; for example,
count "This is a string" --result: 16The result of the Count command depends on how you specify the range of objects to be counted. For example, consider the following statement, given the Scriptable Text Editor document in Figure 4-2:
tell document "simple" of app "Scriptable Text Editor" count words from paragraph 2 to paragraph 3 end tell --result: 8Figure 4-2 The Scriptable Text Editor document "simple"
The reference words from paragraph 2 to paragraph 3 specifies a list of the words in the second and third paragraphs:
{"This", "is", "paragraph", "two.", ÿ "This", "is", "paragraph", "three."}Each item in the list is a word. The Scriptable Text Editor counts the items in the list and returns the result8
.Sometimes the Count command returns a list of integers. Consider the following statement:
tell document "simple" of app "Scriptable Text Editor" count words of paragraphs 2 thru 3 end tell --result: {4, 4}The counting in this example requires several steps, beginning with the reference to the outermost container. The referenceparagraphs 2 thru 3
specifies a list of two items, each of which is a paragraph:
{"This is paragraph two.", "This is paragraph three."}On the basis of this list, the Scriptable Text Editor evaluates the referencewords of paragraphs 2 thru 3
as a list of two items, each of which is
a list of the words in one paragraph:
{{"This", "is", "paragraph", "two."}, � "This, "is", "paragraph", "three."}}Finally, the Scriptable Text Editor counts the items in each list and returns a list of two items, each of which specifies the number of words in one paragraph:{4, 4}
.References to nested containers are always evaluated before counting takes place, beginning with the outermost container. Here's another example:
tell document "simple" of app "Scriptable Text Editor" count characters of words of paragraphs 2 thru 3 end tell --result: {{4, 2, 9, 3}, {4, 2, 9, 5}}The previous example demonstrated that the referencewords of paragraphs 2 thru 3
specifies a list of two items, each of which is a
list of the words in one paragraph:
{{"This", "is", "paragraph", "two."}, � {"This, "is", "paragraph", "three."}}The Scriptable Text Editor counts the items in each list and returns a list of two items, each of which is a list of the number of characters in each of the words in one paragraph:
{{4, 2, 9, 3}, {4, 2, 9, 5}}