Lisp HUG Maillist Archive

Calling AppleScript from LispWorks macOS

Hello,

Are there any examples of calling AppleScript on macOS using LispWorks?

Burton

_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: Calling AppleScript from LispWorks macOS

> On May 14, 2017, at 11:08 PM, BusFactor1 Inc. <busfactor1@gmail.com> wrote:
> 
> Are there any examples of calling AppleScript on macOS using LispWorks?


The easiest way is probably system:call-system-showing-output. Build your AppleScript command line using osascript.  Type "man osascript" in Terminal to see the options.

John DeSoi, Ph.D.




_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: Calling AppleScript from LispWorks macOS

Hi Burton,

I would recommend using the Objective-C class NSAppleScript. It should be pretty easy to interface with your LispWorks code, and spares you from text encoding issues when calling external programs.


Best,
Cam


> On 15 May 2017, at 06:08, BusFactor1 Inc. <busfactor1@gmail.com> wrote:
> 
> Hello,
> 
> Are there any examples of calling AppleScript on macOS using LispWorks?
> 
> Burton
> 
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: Calling AppleScript from LispWorks macOS

Thank you, that sounds like good advice to follow. I’ll look into it.

Thanks everyone else for your suggestions and help as well.

Burton

> On Jun 5, 2017, at 3:24 PM, Camille Troillard <camille.troillard@icloud.com> wrote:
> 
> Hi Burton,
> 
> I would recommend using the Objective-C class NSAppleScript. It should be pretty easy to interface with your LispWorks code, and spares you from text encoding issues when calling external programs.
> 
> 
> Best,
> Cam
> 
> 
>> On 15 May 2017, at 06:08, BusFactor1 Inc. <busfactor1@gmail.com> wrote:
>> 
>> Hello,
>> 
>> Are there any examples of calling AppleScript on macOS using LispWorks?
>> 
>> Burton
>> 
>> _______________________________________________
>> Lisp Hug - the mailing list for LispWorks users
>> lisp-hug@lispworks.com
>> http://www.lispworks.com/support/lisp-hug.html
> 


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: Calling AppleScript from LispWorks macOS

And a little googling turned up this old post from 3 years ago with the following working code!

(defun applescript (applescript-code)
  "Calls APPLESCRIPT-CODE via LW objc-bridge. TODO: handle error message"
  (let ((err-ptr (fli:allocate-foreign-object :type 'objc:objc-object-pointer))
        (nsas (objc:invoke
               (objc:invoke "NSAppleScript" "alloc")
               "initWithSource:" applescript-code)))
    (when (fli:null-pointer-p (objc:invoke nsas "executeAndReturnError:" err-ptr))
      (let ((dict (fli:dereference err-ptr)))
        (unwind-protect
            (loop for a across (objc:invoke-into 'objc::array dict "allValues") ;<==== the returned object is not a lisp array but an NSArray so you have to ask lw to convert it
                  do (print a))
          (objc:release dict))))
    (objc:release nsas)
    (fli:free err-ptr)))

Calling (applescript “say \”This works!\”) does the right thing.  Not sure how to handle the return values yet, but this is a good start. 

Burton Samograd

> On Jun 5, 2017, at 3:24 PM, Camille Troillard <camille.troillard@icloud.com> wrote:
> 
> Hi Burton,
> 
> I would recommend using the Objective-C class NSAppleScript. It should be pretty easy to interface with your LispWorks code, and spares you from text encoding issues when calling external programs.
> 
> 
> Best,
> Cam
> 
> 
>> On 15 May 2017, at 06:08, BusFactor1 Inc. <busfactor1@gmail.com> wrote:
>> 
>> Hello,
>> 
>> Are there any examples of calling AppleScript on macOS using LispWorks?
>> 
>> Burton
>> 
>> _______________________________________________
>> Lisp Hug - the mailing list for LispWorks users
>> lisp-hug@lispworks.com
>> http://www.lispworks.com/support/lisp-hug.html
> 


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Updated at: 2020-12-10 08:31 UTC