Recent

Author Topic: [SOLVED]Getting AppleScript output  (Read 4388 times)

bobix

  • Jr. Member
  • **
  • Posts: 71
    • http://rechnik-bg.com
[SOLVED]Getting AppleScript output
« on: April 21, 2017, 12:37:46 pm »
Does anyone knows how to get AppleScript's output? I am trying this way, but getting empty string:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,process;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.     { private declarations }
  19.   public
  20.     { public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   ss:string;
  35. begin
  36.   runcommand('osascript ''return "hello world"''',ss);
  37.   showmessage(ss);
  38. end;
  39.  
  40. end.
  41.  
  42.  
« Last Edit: April 26, 2017, 03:49:42 pm by bobix »
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Getting AppleScript output
« Reply #1 on: April 21, 2017, 03:30:30 pm »
Does anyone knows how to get AppleScript's output?

Perhaps RunCommand can't find the executable to run unless you include a path to it. See if what you're trying to do works with other command line programs, eg, ls.

osascript is probably in /usr/bin.

Note that the form of RunCommand you're using is deprecated. I usually just use TProcess instead of functions that attempt to simplify TProcess by wrapping it.

http://www.freepascal.org/docs-html/fcl/process/runcommand.html

bobix

  • Jr. Member
  • **
  • Posts: 71
    • http://rechnik-bg.com
Re: Getting AppleScript output
« Reply #2 on: April 21, 2017, 03:57:39 pm »
Thank you!
It think i have found a solution, but have to test it on Monday. I will give a feedback if it works :)
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64

bobix

  • Jr. Member
  • **
  • Posts: 71
    • http://rechnik-bg.com
Re: Getting AppleScript output
« Reply #3 on: April 26, 2017, 03:49:15 pm »
Working code. Same as Linux one :)
Code: Pascal  [Select][+][-]
  1.     procedure TForm1.Button1Click(Sender: TObject);
  2.     var
  3.       AProcess: TProcess;
  4.       AStringList: TStringList;
  5.     begin
  6.       AProcess := TProcess.Create(nil);
  7.       AProcess.Executable := '/bin/sh';
  8.       AProcess.Parameters.Add('-c');
  9.       AProcess.Parameters.Add('echo `ls`');
  10.       AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  11.       AProcess.Execute;
  12.       AStringList := TStringList.Create;
  13.       AStringList.LoadFromStream(AProcess.Output);
  14.       showmessage(astringlist.Text) ;
  15.       AStringList.Free;
  16.       AProcess.Free;
  17.     end;  
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018