0

How can I run a terminal command via Delphi xe6-7 OSX application ? I am wanting to run a script that returns all track names from iTunes playlist for example.

I see for VCL and WINDOWS I can run a ShellExecute() function, but I can't seem to find the equivalent for OSX in FMX

ThisGuy
  • 1,405
  • 1
  • 24
  • 51

1 Answers1

5

ShellExecute is a Windows OS function, not a VCL function. For reasons I am unsure of, BIBCE (Borland/Inprise/Borland/CodeGear/Embarcadero) never bothered to implement a function to launch a command in VCL/FMX, although many other cross-platform tools (Qt, Python, even FreePascal) have this. As Stephen Ball related in a blog post, if your app is cross-platform you'll need to use IFDEFs and the native function for each platform. :-(

While I don't have access to OS X to test it on, Embarcadero's Ball gave the following code to illustrate launching a file on Windows and OS X, using "_system" for OS X:

unit FileLauncher;

interface

uses
{$IFDEF MSWINDOWS}
Winapi.ShellAPI, Winapi.Windows;
{$ENDIF MSWINDOWS}
{$IFDEF MACOS}
Posix.Stdlib;
{$ENDIF MACOS}

type
TFileLauncher = class
class procedure Open(FilePath: string);
end;

implementation

class procedure TFileLauncher.Open(const FilePath: string);
begin
{$IFDEF MSWINDOWS}
ShellExecute(0, 'OPEN', PChar(FilePath), '', '', SW_SHOWNORMAL);
{$ENDIF MSWINDOWS}

{$IFDEF MACOS}
_system(PAnsiChar('open ' + '"' + AnsiString(FilePath) + '"'));
{$ENDIF MACOS}
end;
Gerry Coll
  • 5,867
  • 1
  • 27
  • 36
alcalde
  • 1,288
  • 13
  • 10
  • 1
    2 years earlier than Stephens blog post http://stackoverflow.com/questions/7443264/how-to-open-an-url-with-the-default-browser-with-firemonkey-cross-platform-appli/7443440#7443440 :o) – Sir Rufo Sep 04 '14 at 08:24