I had a need for this today, and could not find a solution in either my own library, or by extensive Internet searching, so I thought I’d share with everyone here my solution.
I hope that:
- Others with find this useful
- Maybe one of you smart gurus (like @NigelGarvey) might find some ways to improve on it.
Of course, I won’t be shocked if this already exists, I just couldn’t find it.
Handler
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on doesTextContainAnyListItem(pText, pList) -- @List @String @Search @Contains
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(* VER: 1.0 2017-12-23
PURPOSE: Determine if string (text) contains any item in the List
PARAMETERS:
• pText | text | Text to search
• pList | list | List of text items to search for
RETURNS: true IF any item in the list was found in the text
false IF NOT
AUTHOR: JMichaelTX
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
set foundItemBool to false
repeat with aItem in pList
if pText contains (contents of aItem) then
set foundItemBool to true
exit repeat
end if
end repeat
return foundItemBool
end doesTextContainAnyListItem
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here’s a real-world use case:
tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
set fileAlias to item 1 of finderSelectionList
set oFile to info for fileAlias
tell oFile
set fileName to name
set fileSize to size
set fileKind to kind
end tell
set fileKindKWList to {"movie", "audio", "image", "video", "gif", "jpg", "png"}
set isKWFound to my doesTextContainAnyListItem(fileKind, fileKindKWList)
{fileKind, isKWFound}
-->{"MP3 audio", true}
BTW, as an aside, you might notice what is in common with my KW list – they are all audio/video/image media. Maybe I missed it, but I could not find any other common attribute/keyword that I could use. For example, a GIF file returns this:
kind: "Graphics Interchange Format (GIF)"
type identifier: "com.compuserve.gif"
Note the obviously missing keyword: “image”
So, if anyone should know how to identify any file that is either audio, video, or image, I’d love to know how.