Detecting how an application was launched lazarus / 21-General ---------------------------------------------------------------------- geraldholdsworth · 2024-06-16 18:19 I was idly wondering if there is a way of determining, in Lazarus, if an application is launched from the Explorer/Filer or via Command Line/Terminal in Windows, macOS and Linux. So, I begun looking to see if there was a way, and could only find a possible Windows route to determine this. Is it possible in macOS or Linux? ---------------------------------------------------------------------- MarkMLl · 2024-06-16 18:46 Can't comment on Windows. In the case of the others get the PPID from /proc and see what it tells you, but be aware that Linux and a BSD derivative such as a Mac might have subtle differences. I've looked at this to some extent on Linux and SunOS in the context of logging FPC build commands, see https://github.com/MarkMLl/fpclaz_build_scripts which might have something useful. I might also have looked at it more recently in the case of Linux, but can't remember what I was working on (i.e. where I put it) or precisely what level of detail I was looking for. MarkMLl ---------------------------------------------------------------------- TRon · 2024-06-17 03:01 The following is a (simple) example of MarkMLI's suggestion that works for Linux. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program parent; {$mode objfpc}{$H+} uses  sysutils, baseunix; function GetParentProcessName: string;begin  GetParentProcessName := fpReadLink('/proc/' + fpGetppid.ToString + '/exe');end; begin  writeln('name of parent process = ', GetParentPRocessName);end. Do note that this still does not tell anything about how the program was actually launched as it might very well be done at the desktop that uses a menu-entry that invokes a shell. The heritage can be a very long path and you can't make any assumptions about when to stop following as in Linux there is no consistency regarding launchers whatsoever. ---------------------------------------------------------------------- Zvoni · 2024-06-17 05:59 Have you checked if there is anything in ParamStr? EDIT: Just checked on Windows. Nope, nothing in there, either ---------------------------------------------------------------------- MarkMLl · 2024-06-17 07:47 Quote from: Zvoni on June 17, 2024, 07:59:07 amHave you checked if there is anything in ParamStr? EDIT: Just checked on Windows. Nope, nothing in there, either The sole possibility there would be if the... let's say "extended OS" i.e. kernel, GUI etc. that underlay the application program put the name of a .desktop file into ParamStr(0) if one had been used to start the binary. IME it never does: nothing to see, move along. However one other thing would be to inspect shell/environment variables looking for e.g. XDG_something. In principle this tells one a lot and might allow other information to be mined via e.g. D-Bus, in practice it is at best erratic at least on Debian. So the only way that I've found useful is to look at least one layer deep on the PPID sequence. MarkMLl ---------------------------------------------------------------------- finchley · 2024-06-17 08:33 I've always used the following code for console programs. If you launched the program via a double click it will pause and wait for the enter key to be pressed before terminating. If run from the command line the program will just terminate. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program foo; uses  windows; var  ProcessIDs : array[0..255] of DWORD; begin  { The last line of code... }  if (GetConsoleProcessList(ProcessIDs, high(ProcessIDs)) = 1) then ReadLn();end. ---------------------------------------------------------------------- marcov · 2024-06-17 08:55 Logically it would mean walking parent processes till you find a name you recognize. But that assumes the process parent-child bonds are intact and unmodified (daemonization etc) ---------------------------------------------------------------------- Fibonacci · 2024-06-17 10:06 Another one for Windows Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses Windows; function is_run_from_console: boolean;var  pid: dword;begin  GetWindowThreadProcessId(GetConsoleWindow, @pid);  result := GetCurrentProcessId <> pid;end; begin  writeln('run from console = ', is_run_from_console);  readln;end. ---------------------------------------------------------------------- Zvoni · 2024-06-17 10:22 Quote from: MarkMLl on June 17, 2024, 09:47:18 amThe sole possibility there would be if the... let's say "extended OS" i.e. kernel, GUI etc. that underlay the application program put the name of a .desktop file into ParamStr(0) if one had been used to start the binary. IME it never does: nothing to see, move along. Would have to disagree, at least for Windows. Just did a small test in Windows. Created a Desktop-Link pointing to my exe, right-click on Desktop- Link, Properties, and then in Target, where the fully qualified path incl. exe-name is, i just appended a " -g" (arbitrary value, g=GUI) It returns, that the ParamCount is >0, writeln-ing (ParamStr(1)) returns the "-g" But a No-No for doubleclick-ing in Explorer ---------------------------------------------------------------------- geraldholdsworth · 2024-06-17 10:29 Quote from: TRon on June 17, 2024, 05:01:41 amThe following is a (simple) example of MarkMLI's suggestion that works for Linux. Do note that this still does not tell anything about how the program was actually launched as it might very well be done at the desktop that uses a menu-entry that invokes a shell. The heritage can be a very long path and you can't make any assumptions about when to stop following as in Linux there is no consistency regarding launchers whatsoever. It's a start, thank you. Looks like there's plenty of Windows solutions though. ---------------------------------------------------------------------- MarkMLl · 2024-06-17 10:53 Quote from: Zvoni on June 17, 2024, 12:22:16 pmQuote from: MarkMLl on June 17, 2024, 09:47:18 amThe sole possibility there would be if the... let's say "extended OS" i.e. kernel, GUI etc. that underlay the application program put the name of a .desktop file into ParamStr(0) if one had been used to start the binary. IME it never does: nothing to see, move along. Would have to disagree, at least for Windows. Windows does not TTBOMK use .desktop files. https://specifications.freedesktop.org/desktop-entry-spec/desktop- entry-spec-latest.html MarkMLl ---------------------------------------------------------------------- Zvoni · 2024-06-17 12:30 Quote from: MarkMLl on June 17, 2024, 12:53:12 pmQuote from: Zvoni on June 17, 2024, 12:22:16 pmQuote from: MarkMLl on June 17, 2024, 09:47:18 amThe sole possibility there would be if the... let's say "extended OS" i.e. kernel, GUI etc. that underlay the application program put the name of a .desktop file into ParamStr(0) if one had been used to start the binary. IME it never does: nothing to see, move along. Would have to disagree, at least for Windows. Windows does not TTBOMK use .desktop files. https://specifications.freedesktop.org/desktop-entry-spec/desktop- entry-spec-latest.html MarkMLl OC, i used the Windows-"equivalent" to ".desktop" --> .lnk For linux .desktop files there is the EXEC Key-Word inside, which acc. to different Sites (Ubuntu) contains the executable name optionally followed by arguments EDIT: Or the link you posted. QuoteThe Exec key must contain a command line. A command line consists of an executable program optionally followed by one or more arguments. ---------------------------------------------------------------------- MarkMLl · 2024-06-17 12:48 Quote from: Zvoni on June 17, 2024, 02:30:20 pmFor linux .desktop files there is the EXEC Key-Word inside, which acc. to different Sites (Ubuntu) contains the executable name optionally followed by arguments Yes, but the .desktop file does not appear in ParamStr(0), it's always resolved by the OS to the binary executable. In practice my experience is that there are things in that file which are at best handled haphazardly, but these are not germane to this particular thread. I suppose that if we're mentioning .desktop and .lnk files then we have to also mention the OS/2 WPS shadow files which- if memory serves correctly- were (in unix terms) hard filesystem links rather than being anything special. We can probably say safely that ParamStr(0) refers to a binary, where there might be multiple hardlinks pointing to the same storage. If it was invoked via a symlink, the situation is implementation-specific. .desktop and .lnk files are resolved at an earlier stage of the GUI's handling of program startup. None of which really helps OP: on unix he has to look at the PPID tree, and the structure of that will vary between Linux and BSD derivatives. MarkMLl ---------------------------------------------------------------------- BildatBoffin · 2024-06-17 13:01 Since not already mentioned, on posix-compliant systems you can use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function isatty(fd: integer = 0): integer; cdecl; The running program (if fd is 0, i.e the file des of the output stream, as set as default) can call this to determine if it's called from a term. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-17 13:03 What I've got so far, collating the code from above, is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses SysUtils {$IFDEF Linux} , BaseUnix {$ENDIF} {$IFDEF Windows} , Windows {$ENDIF} ; function IsRunFromConsole: Boolean;var                                     {$IFDEF Windows} pid: dword;{$ENDIF}{$IFDEF Linux} ParentProcess: String;{$ENDIF}{$IFDEF Darwin} path: String;{$ENDIF}begin{$IFDEF Windows} GetWindowThreadProcessId(GetConso leWindow,@pid); Result:=GetCurrentProcessId<>pid;{$ENDIF}{$IFDEF Linux } ParentProcess:=fpReadLink('/proc/'+fpGetppid.ToString+'/exe'); {Shou ld return nothing for launching from Filer; '/usr/bin/lxterminal' when launched from Filer and "Execute in Terminal"; and '/usr/bin/bash' when launched from bash console.} if ParentProcess<>'' then Result:=True else Result:=False;{$ENDIF}{$IFDEF Darwin} path:=ParamStr(0); {This is the command used to run the executable. From bash/Terminal you need to do './project'. This dot will be missing when run from the Filer.} if path[1]='.' then Result:=True else Result:=False;{$ENDIF}end;Appears to work on my Raspberry Pi OS and my Mac. I haven't tried it on Windows yet - I need to fire that beast up and try it out later on. ---------------------------------------------------------------------- MarkMLl · 2024-06-17 13:13 Quote from: BildatBoffin on June 17, 2024, 03:01:26 pmSince not already mentioned, on posix-compliant systems you can use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function isatty(fd: integer = 0): integer; cdecl; The running program (if fd is 0, i.e the file des of the output stream, as set as default) can call this to determine if it's called from a term. Not reliable IME. There's lots of other things one has to take into account: redirected files and so on. MarkMLl ---------------------------------------------------------------------- BildatBoffin · 2024-06-18 13:48 Quote from: MarkMLl on June 17, 2024, 03:13:10 pmQuote from: BildatBoffin on June 17, 2024, 03:01:26 pmSince not already mentioned, on posix-compliant systems you can use Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function isatty(fd: integer = 0): integer; cdecl; The running program (if fd is 0, i.e the file des of the output stream, as set as default) can call this to determine if it's called from a term. Not reliable IME. There's lots of other things one has to take into account: redirected files and so on. MarkMLl Yes when you fork with inherited file descriptors that does not always work. ---------------------------------------------------------------------- MarkMLl · 2024-06-18 14:45 I can't remember the details (and I'm supposed to be doing company tax stuff, so would prefer not to investigate) but a few years ago I had a cross-platform program (Windows/Solaris/Linux) that tried to handle all possibilities of being started from the desktop GUI (possibly with a parameter), from a script, or from a shell with or without a parameter and/or redireaction. I think I allowed for multiple parameters which were implicitly concatenated, and for a directory/folder being passed as the parameter. I remember that isatty() was useful for some cases, but it's definitely not comprehensive. I know I was looking at the "how was it started?" question more recently, but as I think I've said I can't remember the context. However even without being able to go back to that specific project I still with my position, which is that one needs to look at PPID and then be cautious because of the difference between Linux and BSD- derivatives. MarkMLl ---------------------------------------------------------------------- geraldholdsworth · 2024-06-20 19:29 I finally got to run this on a Windows machine...and it didn't work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ProcessCoun t:=0; ProcessCount:=GetConsoleProcessList(ProcessIDs,high(ProcessIDs)) ;Returns 0 when running from either Explorer or Command Prompt Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GetConsoleWindow()Returns 0 when running from either Explorer or Command Prompt Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GetWindowThreadProcessId(GetConsoleWindow, @pid);Returns 0 when running from either Explorer or Command Prompt So, I went off investigating other Windows API calls and found: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GetCommandLineAThis returns the command as run from the command prompt. As it happens, when you double click on an exe in Explorer, this contains the complete path enclosed in quotes. But, when you type in the exe name in command prompt, what you type in is contained here, without quotes (unless you enter the quotes). So, this appears to work: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsRunFromConsole: Boolean;{$IFDEF Windows}var cmdLine: String;{$ENDIF}{$IFDEF Linux}var ParentProcess: String;{$ENDIF}{$IFDEF Darwin}var path: String;{$ENDIF}begin Result:=False;//Default, if not covered by Windows, Linux or Darwin{$IFDEF Windows} cmdLine:=GetComman dLineA; Result:=(cmdLine[1]<>'"')AND(cmdLine[Length(cmdLine)]<>'"');{$ ENDIF}{$IFDEF Linux} ParentProcess:=fpReadLink('/proc/'+fpGetppid.ToSt ring+'/exe'); if ParentProcess<>'' then Result:=True else Result:=False;{$ENDIF}{$IFDEF Darwin} path:=ParamStr(0); if path[1]='.' then Result:=True else Result:=False;{$ENDIF}end; However, I'm not entirely happy about either the Windows method or the macOS method. There must be another, more confident, way of determining this. Thoughts anyone? ---------------------------------------------------------------------- MarkMLl · 2024-06-20 20:19 Apart from Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF Lin ux} ParentProcess:=fpReadLink('/proc/'+fpGetppid.ToString+'/exe'); res ult := ParentProcess <> ''{$ENDIF}{$IFDEF Darwin} path:=ParamStr(0); result := path[1] = '.' // Needs check that path is not in fact empty{$ENDIF}  I'm a little surprised that you have to treat Linux and Mac quite so differently and disappointed that you automatically exclude other BSD derivatives (of which I believe MacOS is but one). When I was looking at shebang handling in relation to some other thread a few days ago I came across a succinct explanation of the reason why the pid/ppid structure of Linux and BSD differs. My recollection from working with SunOS (hence Solaris) is that /proc contains similar per-process stuff to Linux, but omits a whole lot of non-process stuff which Linux belatedly tried to move into /sys. Looking at https://github.com/MarkMLl/fpclaz_build_scripts/blob/main/fpc-filter- vt I was certainly able to use Perl to get ppid, even if I parsed ps output rather than walking the /proc tree myself. MarkMLl ---------------------------------------------------------------------- Fibonacci · 2024-06-20 21:28 Quote from: geraldholdsworth on June 20, 2024, 09:29:23 pmReturns 0 when running from either Explorer or Command Prompt Is the application a console application? ---------------------------------------------------------------------- Remy Lebeau · 2024-06-21 03:09 Quote from: Zvoni on June 17, 2024, 12:22:16 pmJust did a small test in Windows. Created a Desktop-Link pointing to my exe, right-click on Desktop- Link, Properties, and then in Target, where the fully qualified path incl. exe-name is, i just appended a " -g" (arbitrary value, g=GUI) It returns, that the ParamCount is >0, writeln-ing (ParamStr(1)) returns the "-g" Another way to detect if your app is launched via a shortcut is to use GetStartupInfo() and check if the STARTUPINFO.dwFlags field contains the STARTF_TITLEISLINKNAME flag (if so, the STARTUPINFO.lpTitle field contains the full path to the shortcut file). ---------------------------------------------------------------------- geraldholdsworth · 2024-06-21 06:40 Quote from: MarkMLl on June 20, 2024, 10:19:06 pmI'm a little surprised that you have to treat Linux and Mac quite so differently and disappointed that you automatically exclude other BSD derivatives (of which I believe MacOS is but one).Me too. But the Unix solution didn't work on my Mac, which is running Sonoma, so had to find another. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-21 06:42 Quote from: Fibonacci on June 20, 2024, 11:28:28 pmIs the application a console application? It isn't defined as such. This is why I'm wondering if there was a way of seeing how one is launched. I thought of the possibility that if it were launched from Explorer/Filer then it would open a window, otherwise it would open a console. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-21 17:14 Quote from: Remy Lebeau on June 21, 2024, 05:09:40 amAnother way to detect if your app is launched via a shortcut is to use GetStartupInfo() and check if the STARTUPINFO.dwFlags field contains the STARTF_TITLEISLINKNAME flag (if so, the STARTUPINFO.lpTitle field contains the full path to the shortcut file). I did see that, and I've now had a play with it. The results are as follows: When launched direct from Explorer: stdOutput is set to 0x0000000000010001 dwFlags is set to 0x00000401 When launched from Explorer, via shortcut: stdOutput is set to 0x0000000000010001 dwFlags is set to 0x00000C01 and, finally, when launched from command prompt: stdOutput is set to 0xFFFFFFFFFFFFFFFF dwFlags is set to 0x00000000 From what I can see, dwFlags is: 0x00000001 : STARTF_USESHOWWINDOW - look at wShowWindow 0x00000400 : could be STARTF_USESTDHANDLES, as this affects stdOutput - but the spec says this is 0x100. 0x00000800 : STARTF_TITLEISLINKNAME So, we now have: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsRunFromConsole: Boolean;{$IFDEF Windows}var StartUp: StartUpInfoA;{$ENDIF}{$IFDEF Linux}var ParentProcess: String;{$ENDIF}{$IFDEF Darwin}var path: String;{$ENDIF}begin Result:=False;//Default, if not covered by Windows, Linux or Darwin{$IFDEF Windows} GetStartupInfo(StartUp); Result:=not(((StartUp.dwFlags AND 1)=1)AND((Startup.dwFlags AND$400)=$400));{$ENDIF}{$IFDEF Linux} Paren tProcess:=fpReadLink('/proc/'+fpGetppid.ToString+'/exe'); Result:=Pare ntProcess<>'';{$ENDIF}{$IFDEF Darwin} path:=ParamStr(0); Result:=path[1]='.';{$ENDIF}end; Which seems more reliable way for Windows. ---------------------------------------------------------------------- Remy Lebeau · 2024-06-21 21:26 Quote from: geraldholdsworth on June 21, 2024, 07:14:06 pm0x00000400 : could be STARTF_USESTDHANDLES, as this affects stdOutput - but the spec says this is 0x100. Unfortunately, that is an incorrect assumption. 0x400 is an undocumented flag that is used to specify the target monitor which the new process should display its UI on.  According to Undocumented CreateProcess: QuoteSpecify which monitor a process should start on The next undocumented tip uses another unknown STARTUPINFO flag. This flag has the value 0x400, and I have given it the name STARTF_MONITOR. When this flag is specified in the dwFlags member, the STARTUPINFO's hStdOutput member is used to specify a handle to a monitor, on which to start the new process. This monitor handle can be obtained by any of the multiple-monitor display functions (i.e. EnumDisplayMonitors, MonitorFromPoint, MonitorFromWindow etc). ... There are certain restrictions which should be noted here. You may be asking yourself why the above example works, when you know full well that the hStdOutput member should be used for a standard-output pipe. The answer to this question is simple - when the undocumented STARTF_MONITOR flag is specified, the STARTF_USESTDHANDLES flag is ignored. This means that these two flags cannot be used together, and the hStdInput, hStdOutput and hStdError handles act differently to how they are described in the documentation. ... Note that the ShellExecuteEx API call makes use of this CreateProcess feature in order to implement it's own monitor settings. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-22 08:58 Quote from: Remy Lebeau on June 21, 2024, 11:26:45 pmUnfortunately, that is an incorrect assumption. 0x400 is an undocumented flag that is used to specify the target monitor which the new process should display its UI on.  According to Undocumented CreateProcess: Ah...thank you. However, as the flag is set means it is starting on a monitor (as the application is set as a GUI initially), just which monitor depends on hStdOutput (which was 0x10001 when I tried it). But, if the application is configured as a console application, then I guess this won't be set? Need some more playing with, me thinks. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-22 10:45 OK, I've had a wee play with StartUpInfo again, and here are the results: GUI appGUI appGUI appConsole appConsole appConsole appVariableExplorer CMDShortcutExplorerCMDShortcutdwFlags0x000004010x000000000x00000C010x0 00000010x000000000x00000801wShowWindow0x00010x00010x00010x00010x00010x 0001hStdOutput0x00000000000100010xFFFFFFFFFFFFFFFF0x00000000000100010x FFFFFFFFFFFFFFFF0xFFFFFFFFFFFFFFFF0xFFFFFFFFFFFFFFFF Notes All tests done on Windows 10 via Remote Desktop.Ran as a shortcut from Explorer, via command prompt, and pinned to the Taskbar - results were the same.wShowWindow can be changed by changing if the application starts up as Normal, Minimised (0x0007), or Maximised (0x0003). So, in conclusion, looks like we can use dwFlags bit 0 to test for whether the app started from Explorer or Command Prompt. ---------------------------------------------------------------------- MarkMLl · 2024-06-22 11:53 Quote from: geraldholdsworth on June 22, 2024, 12:45:14 pmAll tests done on Windows 10 via Remote Desktop. I'd advise you to check locally as well. I'm reminded of all the old WindowStation stuff. MarkMLl ---------------------------------------------------------------------- geraldholdsworth · 2024-06-22 13:55 Quote from: MarkMLl on June 22, 2024, 01:53:58 pmI'd advise you to check locally as well. I'm reminded of all the old WindowStation stuff. No difference to the results. ---------------------------------------------------------------------- Red_prig · 2024-06-22 13:58 Alternatively you can do it like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Handle:=Ge tStdHandle(STD_OUTPUT_HANDLE); GetFileType(Handle)=FILE_TYPE_CHAR; ---------------------------------------------------------------------- Thaddy · 2024-06-22 14:10 You are not reading this thread. https://forum.lazarus.freepascal.org/index.php/topic,67600.msg520684.h tml#msg520684 ---------------------------------------------------------------------- Red_prig · 2024-06-22 14:16 So what's the problem? If stdout is FILE_TYPE_CHAR then an output terminal is connected to the application ---------------------------------------------------------------------- geraldholdsworth · 2024-06-22 14:44 Quote from: Red_prig on June 22, 2024, 04:16:28 pmSo what's the problem? If stdout is FILE_TYPE_CHAR then an output terminal is connected to the application Unfortunately, that doesn't appear to yield the required result. It only tells me whether it is opening into a console or a GUI, not where it is being launched from (both are zero when opening into a GUI and non-zero when into a console). Either way, the result certainly does not equal FILE_TYPE_CHAR. Sorry. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-22 20:12 Regarding macOS - I think this might have some mileage in it...if I can work out how to access it from Lazarus. It should be a simple case of including a unit...just which one? Foundation, according to my install, can't be found (but I know it's there because I opened it). ---------------------------------------------------------------------- MarkMLl · 2024-06-22 20:17 Quote from: geraldholdsworth on June 22, 2024, 10:12:04 pmRegarding macOS - I think this might have some mileage in it...if I can work out how to access it from Lazarus. It should be a simple case of including a unit...just which one? Foundation, according to my install, can't be found (but I know it's there because I opened it). Assuming that MacOS is a BSD derivative... does it allow you to get at /proc which should be a standard filesystem? MarkMLl ---------------------------------------------------------------------- geraldholdsworth · 2024-06-22 20:19 Quote from: MarkMLl on June 22, 2024, 10:17:19 pmAssuming that MacOS is a BSD derivative... does it allow you to get at /proc which should be a standard filesystem? No - it just returned an empty string. The folder 'proc' doesn't exist either, where in Linux is does. ---------------------------------------------------------------------- MarkMLl · 2024-06-22 20:31 Quote from: geraldholdsworth on June 22, 2024, 10:19:42 pmQuote from: MarkMLl on June 22, 2024, 10:17:19 pmAssuming that MacOS is a BSD derivative... does it allow you to get at /proc which should be a standard filesystem? No - it just returned an empty string. The folder 'proc' doesn't exist either, where in Linux is does. That's weird, it certainly exists on SunOS. Look, most definitely not trying to offend but you /were/ treating it as a directory containing files, weren't you? MarkMLl ---------------------------------------------------------------------- duralast · 2024-06-22 21:10 MacOS does not have /proc, but you can use sudo fs_usage In Linux, if it was launched by the desktop, e.g., Gnome, it will have a .desktop file and search for Exec= that will return something like gnome-control-center or cinnamon-settings-users. ---------------------------------------------------------------------- geraldholdsworth · 2024-06-23 10:14 I have found the answer to my later question, about processInfo on macOS. The answer lies in this wiki page. And it doesn't yeald the information I was after. Back to the drawing board. Quote from: MarkMLl on June 22, 2024, 10:31:11 pmThat's weird, it certainly exists on SunOS. Look, most definitely not trying to offend but you /were/ treating it as a directory containing files, weren't you? Don't worry, you're not offending me - I value your input Mark. And, yes, I was (I used the UNIX code given on the first page of this post). Quote from: duralast on June 22, 2024, 11:10:15 pmMacOS does not have /proc, but you can use sudo fs_usage Sorry for being inexperienced in this area, but how would I do that in Lazarus? ---------------------------------------------------------------------- geraldholdsworth · 2024-06-23 10:29 Ah...well, actually I may have jumped the gun a bit. The results from processInfo.environment are: Run via app bundle: Code: [Select]{     "COMMAND_MODE" = unix2003;     HOME = "/Users/geraldholdsworth";     LOGNAME = geraldholdsworth;     PATH = "/usr/bin:/bin:/usr/sbin:/sbin";     SHELL = "/bin/zsh";     "SSH_AUTH_SOCK" = "/private/tmp/com.apple.launchd.35SDrFixwG/Listeners";     TMPDIR = "/var/folders/sp/r7dyc78d19j36ftwtmckmr3w0000gn/T/";     USER = geraldholdsworth;     "XPC_FLAGS" = 0x0;     "XPC_SERVICE_NAME" = "application.com.company.project1.36549144.37180253";     "__CFBundleIdentifier" = "com.company.project1";     "__CF_USER_TEXT_ENCODING" = "0x1F5:0:2"; } Run via Terminal, or Run via Filer (direct to binary): Code: [Select]{     HOME = "/Users/geraldholdsworth";     "HOMEBREW_CELLAR" = "/opt/homebrew/Cellar";     "HOMEBREW_PREFIX" = "/opt/homebrew";     "HOMEBREW_REPOSITORY" = "/opt/homebrew";     INFOPATH = "/opt/homebrew/share/info:";     LANG = "en_GB.UTF-8";     LOGNAME = geraldholdsworth;     MANPATH = "/opt/homebrew/share/man::";     OLDPWD = "/Users/geraldholdsworth/Library/Mobile Documents/com~apple~CloudDocs/Programming/Lazarus/Old and Test Projects/Console and GUI/lib";     PATH = "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/Syste m/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.app le.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com .apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.a pple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/L ibrary/Apple/usr/bin:/Applications/VMware Fusion.app/Contents/Public";     PWD = "/Users/geraldholdsworth/Library/Mobile Documents/com~apple~CloudDocs/Programming/Lazarus/Old and Test Projects/Console and GUI/lib/x86_64-darwin";     SHELL = "/bin/zsh";     SHLVL = 1;     "SSH_AUTH_SOCK" = "/private/tmp/com.apple.launchd.35SDrFixwG/Listeners";     TERM = "xterm-256color";     "TERM_PROGRAM" = "Apple_Terminal";     "TERM_PROGRAM_VERSION" = 453;     "TERM_SESSION_ID" = "1CDE8358-C3EF-492A-955D-6A36DDECAC31";     TMPDIR = "/var/folders/sp/r7dyc78d19j36ftwtmckmr3w0000gn/T/";     USER = geraldholdsworth;     "XPC_FLAGS" = 0x0;     "XPC_SERVICE_NAME" = 0;     "_" = "/Users/geraldholdsworth/Library/Mobile Documents/com~apple~CloudDocs/Programming/Lazarus/Old and Test Projects/Console and GUI/lib/x86_64-darwin/./project1";     "__CFBundleIdentifier" = "com.apple.Terminal";     "__CF_USER_TEXT_ENCODING" = "0x1F5:0:2"; } So, it looks like there is something we can use here. ---------------------------------------------------------------------- MarkMLl · 2024-06-23 12:46 OK, but couldn't you get that from running  env  in a terminal? I think that's a standard unix program rather than a Linuxism. And from a terminal, what happens if you do  ls /proc  ? MarkMLl ---------------------------------------------------------------------- geraldholdsworth · 2024-06-23 14:37 Quote from: MarkMLl on June 23, 2024, 02:46:36 pmOK, but couldn't you get that from running  env  in a terminal? I think that's a standard unix program rather than a Linuxism. Oh yeah - displays a whole load of other info. But that is info that is more relevant to the running Terminal instance, rather than the Lazarus project that I got the info for. Plus, using NSProcessInfo.ProcessInfo.environment gives it to you in way that you can manipulate. I'm now using: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$IFDEF Darwin} i:=0; while(i'XPC_SERVICE_NAME')do inc(i); Result:=(NSProcessInfo.Process Info.environment.allKeys.objectAtIndex(i).UTF8String='XPC_SERVICE_NAME ')      and(NSProcessInfo.ProcessInfo.environment.allValues.objectAtIn dex(i).UTF8String='0');{$ENDIF}To determine if the application was launched from Filer or Terminal (I'm sure that there is a better way to access the key/value, but I just couldn't get objectForKey('XPC_SERVICE_NAME') to work). Quote from: MarkMLl on June 23, 2024, 02:46:36 pmAnd from a terminal, what happens if you do  ls /proc  ? Code: [Select]zsh: no such file or directory: /proc ---------------------------------------------------------------------- geraldholdsworth · 2024-06-23 16:02 Worked it out. Code now looks like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; {$mode objfpc}{$H+}{$IFDEF Darwin}{$modeswitch objectivec1}{$ENDIF}  uses {$IFDEF Windows}Windows{$ENDIF} {$IFDEF Linux}BaseUnix{$ENDIF} {$IFDEF Darwin}typinfo,CocoaAll{$ENDIF}; function IsRunFromConsole: Boolean;{$IFDEF Windows}var StartUp: StartUpInfoA;{$ENDIF}begin Result:=False;//Default, if not covered by Windows, Linux or Darwin{$IFDEF Windows} StartUp.dwFlags:=0;//Prevents 'variable not initialised' message GetStartupInfo(StartUp); Result:=(StartUp.dwFlags AND 1)<>1;{$ENDIF}{$IFDEF Linux} Result:=fpReadLink('/proc/'+fpGetppid.ToS tring+'/exe')<>'';{$ENDIF}{$IFDEF Darwin} Result:=NSProcessInfo.Proces sInfo.environment.objectForKey(NSStr('XPC_SERVICE_NAME')).UTF8String=' 0';{$ENDIF}end; begin if IsRunFromConsole then WriteLn('Run from console') else WriteLn('Run from Filer/Explorer');end. I've encapsulated it inside a program as you'd need the units and the modeswitch. Thank you all for your assistance, it has been invaluable. I've certainly learnt a lot from this excercise. Of course, like all programs, it's never finished and probably can be improved upon.