Le 10 sept. 2014 à 11:41, Jean-Luc Arnaud a écrit:

> Here is what I coded:
> 
> f=LaunchServicesFindApplicationForInfoMBS("","com.apple.systemevents","")
>    f.Launch False
> 
>    p=new ProcessMBS
>    While p.Name<>"Événements système" ' Loop waiting for 
> SystemEvents to be launched ("Événements système" in French)
>      p.GetFirstProcess
>      Do ' Loop checking if SystemEvents is running
>        If p.Name="Événements système" Then exit
>      Loop until not p.GetNextProcess
>    Wend
> 
>    ae=new AppleEvent("prcs","kcod","com.apple.systemevents")
>    ae.IntegerParam("----")=53
>    call ae.Send
> 
> Thanks a lot.

You're welcome. Your code is good for your purpose, but I'd like to suggest 
some changes:
(1) After 
f=LaunchServicesFindApplicationForInfoMBS("","com.apple.systemevents",""), 
please check against nil (if System Events, for whatever reason, isn't 
installed, it's always better to not have your application crashing).
(2) Relying on the application's name isn't perfect. All an user has to do to 
make your app waiting forever is go to system preferences (“Language&Text” or 
“International” pane, given the OS version), put “English” on top and use your 
app ;-). Instead, I suggest you waiting for the BundleID (when 
p.BundleID="com.apple.systemevents"). BundleID has this purpose of being the 
same in every language.
(3) Perhaps a personal opinion, but the outer loop looks unreliable to me (you 
check for the content of “p” again). I'd suggest checking only once if the 
condition is met (e.g. setting a boolean in the inner loop).

Might I suggest you a rewritten code?

dim ae As AppleEvent
dim b as boolean
dim f As FolderItem
dim p As ProcessMBS

f=LaunchServicesFindApplicationForInfoMBS("","com.apple.systemevents","")
if f<>nil then
 f.Launch False

 p=new ProcessMBS
 b=false //Should you use “b” another time and forget it can be true in the 
beginning (happens to me regularly)
 while not b
   p.GetFirstProcess
   Do ' Loop checking if SystemEvents is running
     If p.BundleID="com.apple.systemevents" Then
       b=true
       exit
     end if
   Loop until not p.GetNextProcess
 Wend
end if

ae=new AppleEvent("prcs","kcod","com.apple.systemevents")
ae.IntegerParam("----")=53
call ae.Send

(sometimes, I tend to insert a delay after, in your code, “Loop until not 
p.GetNextProcess” of, say, one second, because it's not worth checking so many 
times per second if a process is running, but that's splitting hairs).
_______________________________________________
Mbsplugins_monkeybreadsoftware.info mailing list
[email protected]
https://ml01.ispgateway.de/mailman/listinfo/mbsplugins_monkeybreadsoftware.info

Reply via email to