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"
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?
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
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
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.
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.
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.