Check for part of application name in processes?

Hello,

I have a script that runs different versions of an application, depending on the OS X version in which it is running. I want to use System Events to test whether any of the versions of the application is running. The names of the application might be MyApp, or MyApp-SnowLeopard or MyApp-Lion, or something else. Is it possible to write a variation of the following line of AppleScript that would look for a string that begins “MyApp”

tell application "System Events" to set appRunning to (name of processes) contains "MyApp"

Thanks for any help.

You obviously didn’t try it – it works as you’ve written it.

Hello! :slight_smile:

The code below is not tested, it just gives you an outline.
I guess both versions of your applications can be found running.


tell application "System Events"
	set apps to name of every process whose name contains "BBEdit"
end tell

set {stdVer, slVer, lionVer} to {false, false, false}

repeat with a in apps
	
	if a contains "Lion" then
		set lionVer to true
	else if a contains "SnowLeopard" then
		set slVer to true
	else
		set stdVer to true
	end if
end repeat

Hello Adam! It is social coding, isn't it? 


EDIT: This is a response to Adam Bell’s message (the second one in the thread):

But I did test it, which is why I posted.

Under Lion, my scripts runs an application named “DOSBox-patched-lion.” When it is running, this returns true:

tell application "System Events" to set appRunning to (name of processes) contains "DOSBox-patched-lion"

This returns false:

tell application "System Events" to set appRunning to (name of processes) contains "DOSBox"

Is there a way of rewriting this line so that it will return true if it finds a process the name of which contains the string “DOSBox”?

Your idea works. This gives the correct result:

tell application "System Events"
	set apps to name of every process whose name contains "DOSBox"
end tell
if apps is not {} then
	set appRunning to true
else
	set appRunning to false
end if

It works even if the process is named “dosbox”.

Thank you!

Sorry, I didn’t understand that you wanted part of a name; it works for a whole name but not part. This works in Lion – you have to compare them one at a time.

tell application "System Events" to set tNames to (name of processes)
set tClue to "Quick" -- looking for Quicksilver
set appRunning to {}
repeat with aName in tNames
	if contents of aName contains tClue then set end of appRunning to contents of aName
end repeat

appRunning → {“Quicksilver”}

Change it to:

tell application “System Events” to set appRunning to ((name of processes) as text) contains “DOSBox”

Works perfectly. Thank you!

Or with a shell command

every paragraph of (do shell script "ps -Ac -o comm= | grep -i '^Apple'")

Nice! Probably faster than anything else.

Hello.

I must say, I find it kind of odd, that this behaviour of System Events, has changed in Lion. :slight_smile:

Interesting. I would have thought that name of processes was a list of text. Why does the direct coercion make this work? (for me anyway).

Without the coercion, you’re doing something like this:

set x to {"one two","three four", "five six"}
"one" is in x


That’s asking if one of the items in x is a match for “one”, which it isn’t.

Coercing to a string means it’s using string containment rather than list containment to find the string.

Hello!

It seems to me that a level of implict coercion, has been shaved away with System events or Apple Script in Lion then, is this something that is a thumb of rule there? That AppleScript has become a more strongly typed language.

Coercions like that was, and is made automatically under Snow Leopard.

No. Containment comparisons in lists have always required an exact match – there is no change in behaviour here. What’s changed is that the poster wants to to search for a string that begins with another, and you can’t do that directly with a list of strings.

Emendelson is the only one who used it correctly, he used a filter.
so this :

tell application "System Events"
	name of every process whose name begins with "Apple"
end tell

returns the same as this


every paragraph of (do shell script "ps -Ac -o comm= | grep -i '^Apple'")


tell application "System Events"
	set apps to name of every process whose name begins with "BB"
end tell

When I ran the code above on Snow Leopard with Applescript version 2.1.2 I got a list containing BBEdit, and BBAutoComplete out as a result. I ran the code from within Script Debugger 4.5.7, but that shouldn’t really matter.

Right, but that code is unlike most of the code earlier in the thread. There has been no change of behaviour on this between OS versions.

Correct Shane, like my previous post I noticed nobody unlike the TS had used an filter expression. So your statements were correct, the example codes (you replied to) were wrong.