Name
contains
Syntax
set mybool to ({"apples","oranges","peaches"} contains "peaches") (* returns true *)
Return value
boolean
; true
or
false
Description
The
contains
operator can take lists, records, or
strings as operands. You can use this operator to search for an item
in a list
, a record
, or a part
of the string
. If the operand to the right of the
contains
operator is of a different type than the
left operand, then AppleScript attempts to coerce the second operand
to the class of the first one. This is an operator to get to know
well. A lot of commands return lists, strings, and records;
contains
is a very useful tool for finding certain
values within these value types.
You cannot use contains
directly to search the
contents of a folder, but there is a workaround for this task. The
example in this section illustrates returning a
list
with a command and then using the
contains
operator to search the list. You can use
contains
to search a record
too:
{name:"Bruce W.", state:"MA"} contains {name:"Bruce W."}
Examples
tell application "Finder" tell folder "new images" set fJpgs to files whose name contains ".jpg" (* returns list of jpeg files, if any *) if length of fJpgs > 0 then (* if the list is not empty then display count of jpegs *) display dialog ((length of fJpgs) as string) end if end tell end tell
Get AppleScript in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.