MacOS - sending AppleEvents (AppleScript.OSAScript)

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

MacOS - sending AppleEvents (AppleScript.OSAScript)

Post by cormullion »

How can I send some AppleScript from newLISP?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

I am not familiar with AppleScript, but if there is any possibility to initiate it from the Mac OSX command shell (terminal window) then you could look into the newLISP functions '!', 'process' and 'exec'.

If AppleScript is an application, which can be started from a terminal window and accepts input from standard (STDIN), then you could look into the function 'process' with the option to open I/O pipes. This would be the thightest coupling of AppleScript and newLISP. newLISP would tan be able to feed AppleScript commands directly into it and accept output from AppleScript as well.

I switched to Mac OSX in July of this year and I am interested in doing the same thing myself, using AppleScript from newLISP, but haven't had the time to pick up a book about AppleScript, perhaps both of us together can solve this problem ;). "How would I start a script written in AppleScript from a terminal window?" would be my first question.

Lutz

ps: welcome to the newLISP discussion forum

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I've used the 'osascript' command to run AppleScripts before (eg from Ruby or Perl), but was wondering whether there was any more basic communication (eg an OSA interface that generates raw Apple Events).

I'll try (process) now:
--
#!/usr/bin/newlisp
(process "osascript -e 'tell app \"Finder\" to display dialog \"hello world\"'")
--

oh cool, it works! As ever, managing the quotes is the hard part... :-)

I've only just discovered newLISP so haven't even started the manual. :-) So much to learn... :-) The only two languages I know are Lisp (from the 1980s when I used Ultrix and Sun workstations) and AppleScript (from the 1990s when I went Mac) so combining the two is the next step.

I'm glad to have discovered newLisp - I haven't been enjoying my encounters with Common Lisp installations this week. newLISP looks like a great idea!

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You can use the {,} braces in newLISP to make it easier with the quotes mess:

Code: Select all

(! {osascript -e 'tell app "Finder" to display dialog "hello world"'})

; or

(process {osascript -e 'tell app "Finder" to display dialog "hello world"'})

The difference between 'process' and '!' is, that '!' blocks until osascript is completed, while 'process' will return right away.

Instead of the {,} delimiters you could also use the [text], [/text] tags; this allows you to embed bigger multiline portions of osascript.

Lutz

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Here is a simple way to get output back from osascript:

Code: Select all

> (exec {osascript -e 'tell app "Finder" to display dialog "hello world"'})
("button returned:OK") 
> 
There should be a way to do OSA stuff directly the way Perl/Python/TclTk do it, using 'import' in newLISP and writing an interface module, but I haven't found out yet which library etc.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Lutz wrote:Here is a simple way to get output back from osascript:

Code: Select all

> (exec {osascript -e 'tell app "Finder" to display dialog "hello world"'})
("button returned:OK") 
> 
Lutz
That's very nice - no quoting problems and AppleScript responses too!

Thanks. This is looking pretty good!

Locked