From f35ad0519e883012f0f9e0dd76a4698236c0d1b0 Mon Sep 17 00:00:00 2001 From: Pradeep Gowda Date: Wed, 27 May 2015 21:59:57 -0400 Subject: [PATCH 1/4] Application now compiles on Mac OSX (Yosemite). Added `internalAppIsRunning` procedure for Darwin. Uses `pgrep` instead of `ps` as MacOSX's ps lacks the `-C` option. Instead we will use `pgrep` to query for the applications pid. If the application is running we will get a positive integer (as a string) in return which is returned as the `Result` after casting to `Int`. Also: my first ever ObjectPascal code ;) Hooray! --- src/ce_common.pas | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/ce_common.pas b/src/ce_common.pas index 35ed9f47..d423c203 100644 --- a/src/ce_common.pas +++ b/src/ce_common.pas @@ -6,7 +6,7 @@ interface uses - Classes, SysUtils, + Classes, SysUtils, StrUtils, {$IFDEF WINDOWS} Windows, JwaTlHelp32, {$ENDIF} @@ -911,6 +911,39 @@ begin end; {$ENDIF} +{$IFDEF DARWIN} +function internalAppIsRunning(const ExeName: string): integer; +var + proc: TProcess; + lst: TStringList; + stripChars: TSysCharSet; + var lstText: AnsiString; +begin + Result := 0; + stripChars := ['"']; + proc := tprocess.Create(nil); + proc.Executable := 'pgrep'; + proc.Parameters.Add(ExeName); + proc.Options := [poUsePipes, poWaitonexit]; + try + proc.Execute; + lst := TStringList.Create; + try + lst.LoadFromStream(proc.Output); + lstText := lst.Text; + RemoveLeadingChars(lstText, stripChars); + Result := StrToInt(lstText); + finally + lst.Free; + end; + finally + proc.Free; + end; +end; +{$ENDIF} + + + function AppIsRunning(const ExeName: string):Boolean; begin Result:= internalAppIsRunning(ExeName) > 0; From 30b8a74929bb1584d79218f2fd832e45476257da Mon Sep 17 00:00:00 2001 From: Pradeep Gowda Date: Wed, 27 May 2015 22:18:06 -0400 Subject: [PATCH 2/4] Remove redundant `var` declaration. --- src/ce_common.pas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ce_common.pas b/src/ce_common.pas index d423c203..6d901702 100644 --- a/src/ce_common.pas +++ b/src/ce_common.pas @@ -917,7 +917,7 @@ var proc: TProcess; lst: TStringList; stripChars: TSysCharSet; - var lstText: AnsiString; + lstText: AnsiString; begin Result := 0; stripChars := ['"']; From 42aa1e67ebba3eb5b43d07fae7b4134865ad669c Mon Sep 17 00:00:00 2001 From: Pradeep Gowda Date: Wed, 27 May 2015 22:55:57 -0400 Subject: [PATCH 3/4] Add `Open` function. Trim will do the trick of obtaining the pid. No need for RemoveLeadingChars. --- src/ce_common.pas | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/ce_common.pas b/src/ce_common.pas index 6d901702..b8ef4c47 100644 --- a/src/ce_common.pas +++ b/src/ce_common.pas @@ -659,6 +659,18 @@ begin Free; end; {$ENDIF} + {$IFDEF DARWIN} + with TProcess.Create(nil) do + try + Executable := 'open'; + Parameters.Add(aFilename); + Execute; + finally + result := true; + Free; + end; + {$ENDIF} + end; function exeInSysPath(anExeName: string): boolean; @@ -916,11 +928,8 @@ function internalAppIsRunning(const ExeName: string): integer; var proc: TProcess; lst: TStringList; - stripChars: TSysCharSet; - lstText: AnsiString; begin Result := 0; - stripChars := ['"']; proc := tprocess.Create(nil); proc.Executable := 'pgrep'; proc.Parameters.Add(ExeName); @@ -930,9 +939,7 @@ begin lst := TStringList.Create; try lst.LoadFromStream(proc.Output); - lstText := lst.Text; - RemoveLeadingChars(lstText, stripChars); - Result := StrToInt(lstText); + Result := StrToInt(Trim(lst.Text)); finally lst.Free; end; From a0ff81dc4a1672a03a0d798430ac50d9d2d19241 Mon Sep 17 00:00:00 2001 From: Pradeep Gowda Date: Wed, 27 May 2015 23:18:30 -0400 Subject: [PATCH 4/4] use StrToIntDef to return a default value of 0 incase pgrep returns an invalid Integer --- src/ce_common.pas | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ce_common.pas b/src/ce_common.pas index b8ef4c47..e362ddc7 100644 --- a/src/ce_common.pas +++ b/src/ce_common.pas @@ -6,7 +6,7 @@ interface uses - Classes, SysUtils, StrUtils, + Classes, SysUtils, {$IFDEF WINDOWS} Windows, JwaTlHelp32, {$ENDIF} @@ -939,7 +939,7 @@ begin lst := TStringList.Create; try lst.LoadFromStream(proc.Output); - Result := StrToInt(Trim(lst.Text)); + Result := StrToIntDef(Trim(lst.Text), 0); finally lst.Free; end;