Search list for item which you don't have the name of without loop

Is there a way to search a list for an item whose name starts with/ends with/contains/whatever without parsing the whole list and scanning each element?

A simple example:

if (every process as list) contains (process whose name starts with "Python") then return "Ya!"

I don’t want to use a repeat loop for every list item… but don’t know the full name of the element I’m searching for. For example, I want this one-liner to return “Ya!” if it finds Python 2.5, say… or Python 2.6… or any future releases of Python which may have some arbitrary ending.

Is this possible?

Thx in advance… I’ve been lurking MacScripter for over a year now but this is my first post. :smiley:


Don’t think so; I think you must identify one whole item in the list:

tell application "System Events" to set myList to (name of every process)
myList contains "loginwindow" --> true

but this doesn’t work even though loginwindow contains login:

tell application "System Events" to set myList to (name of every process)
myList contains "login" --> false

You have to iterate if you’re working with an AppleScript list, but many applications have filter references which will iterate through their items internally for you.

tell application "System Events"
	if ((some process whose name begins with "Python") exists) then
		return "Yah!"
		return "Bah!"
	end if
end tell

regardless of the syntax question I guess that python doesn’t appear in System Events’ process list at all

Something like this?

set theList to {"Terminal", "Finder", "Python 2.6", "Python 2.4", "Loginwindow", "the python"}

itemsbeginsWith("python", theList)

on itemsbeginsWith(searchString, theList)
	set AppleScript's text item delimiters to ASCII character 10
	set unixList to theList as string
	set AppleScript's text item delimiters to ""
	return every paragraph of (do shell script "/bin/echo -n " & quoted form of unixList & " | grep -i " & quoted form of ("^" & searchString))
end itemsbeginsWith

Wow! thanks for all the help guys! :smiley:

Nigel, that was exectly what I was looking for… and tried it over and over wondering why it doesn’t work. I had the exact same line as your ‘if’ statement line, but without the word ‘some’ and couldn’t figure out why it wasn’t working… btw, why is that word, ‘some’ so impotrant?

Adam, yes, this was my problem exactly… I couldn’t seem to search a list using the operator ‘contains’ as I can in every other list. For some reason a list containing the names of every running process doesn’t seem to be treated like every other list… or is it you can’t search a list for an item of which you don’t know the complete name? For some reason, the following code isn’t in recognized by the “Standard Suite” (although as Nigel points out, “System Events” can do this:

set myList to {"crap", "junk", "garbage", "puke", "debris", "trash"}
if myList contains (some item whose contents starts with "garb") then return "Ya!"

Bazzie, searching a pre-defined list as you have under ‘property’ won’t solve the problem of future releases, which may have random names, or apps with ‘lite’, ‘demo’ or something else on the end of their names. However, I understand you’re using it as a generic list example but, unless there’s something I’m not seeing, Adam’s reply spelled out my problem nicely… yes, I’ve been shell programming for many years longer than AppleScripting and could ‘ps | grep’ the desired result in a second. But I’m trying to learn AppleScript as it has many Mac-specific advantages over the shell, which segues right into my next question.

Using ‘process status’ in the shell generates a list of running apps, but in AppleScript, when trying to tell an app that doesn’t exist to do something, Script Debugger (I believe Script Editor does as well but it’s been ages since I’ve used it, don’t remember now) brings up a list of ALL apps it finds on your computer…

Question #1: is there a way to get AppleScript to store this list in a variable in a script? What does SD scan that takes so long to bring up this app list? Does it somehow interface with launchctl? I want to use this list in a script which will find and launch the specified program on the computer if it’s not in the standard “/Applications/*” directory.

Question #2: Related to everything above. I wish to set a list variable using Nigel’s solution with all matches. Although the exact process doesn’t matter, I want this:

tell application "System Events"
	if not ((some process whose name begins with "A Better Finder") exists) then launch (some process whose name begins with "A Better Finder")
	--> delay until process(es) appears...
	if ((process whose name begins with "A Better Finder") exists) then set ABFProcess to (some process whose name begins with "A Better Finder")
	return ABFProcess as list
	--> Returns: {"A Better Finder Rename 7","A Better Finder Rename 8","A Better Finder Attributes 4"}... perhaps
end tell

Thanks for all the help!


Hi, Aesthir.

The keyword ‘some’ isn’t particularly special in this case. A ‘whose’ filter has to specify which of what kind of element to find. In your case, the “what’” is a process. In a test to find out if any such element exists, the “whiches” which make sense are ‘first’ (the first of all such elements), ‘some’ (any such element, chosen at random), and ‘every’ (all such elements). I used ‘some’ merely because it looks good in English, but I suspect that ‘first’ might be slightly faster.

I don’t fully understand your numbered questions, but you can’t ‘launch’ processes, only applications. The best way I can think of off-hand to launch an application with a name which might change is to use its ‘bundle identifier’. You could find this out while writing the script by navigating to the application file with this script (I’ve used Finale, which I have and whose name contains the year number of the release):

bundle identifier of (info for (choose file))
--> "com.makemusic.Finale"

In a script to launch Finale, I’d write:

launch application id "com.makemusic.Finale"

This should (unless things have changed recently) work on the Finale version already open, if there is one, or on the most recently installed version otherwise. (But I’m not totally sure about this.)

Thanks for the help! with the bundle id, I can get exactly what I want… :cool:

As for my questions… in essence, I’m asking if there’s a way to search a list of all known applications for an incomplete name with AppleScript (and, perhaps get their bundle identifiers to boot :wink: ). I’ve whipped up a shell script to do exectly that:

Can AppleScript do this? it’s not as important now, as ‘do shell script’ is an absolutely wonderful command for shell scripters like me… but wish to get away from using this (all my “AppleScript” scripts contain that line 5+ times…) as it’s a bit slow.