(lisppad applescript)exports procedures for invoking Automator workflows and AppleScript scripts and subroutines from Scheme code. Since LispPad runs in a sandbox and scripts and subroutines are executed outside of the sandbox, this will enable direct integrations with other macOS applications supporting AppleScript or Automator such as Mail, Safari, Music, etc.
The script authorization mechanism of macOS is unfortunately a bit cumbersome, requiring the installation of the Automator and AppleScript files in a particular directory specifically for LispPad.
(system-directory 'application-scripts)returns a list of directories in which scripts are accessible by LispPad. This includes typically the directory:
This directory can be opened on macOS's Finder via:
(open-file (car (system-directory 'application-scripts)))
Scripts need to be copied to this directory.
As an example, the following script defines two AppleScript subroutines
setSafariFrontURL. The AppleScript code also displays an error if the script is run overall as its only role is to make subroutines accessible to LispPad. Such scripts are written using Apple's Script Editor application and need to be stored in a directory accesible by LispPad as explained above.
tell application "Safari" to return URL of front document
tell application "Safari" to set URL of front document to newUrl
display alert "Do not run this script. It provides AppleScript sub-routines to LispPad."
Assuming that the script was saved in a file at path:
it is now possible to load the script via procedure
applescriptinto an AppleScript object from which the various subroutines can be accessed:
(import (lisppad applescript))
(define script (applescript "AccessSafari.scpt"))
It is possible to run the whole script via procedure
The execution of scripts is always synchronous, so the procedure call to
execute-applescriptterminates only when the execution of the script terminates. When executed, the script above will always display an alert since it was not made to be executed.
It is not possible to pass parameters via
execute-applescriptor receive results. This can be achieved by calling subroutines with procedure
apply-applescript-proc. The following code will invoke subroutine
safariFrontURLfrom the script above and return the URL of the current frontmost Safari window:
(apply-applescript-proc script "safariFrontURL" '())
The third argument of procedure
apply-applescript-procis a list of parameters for the subroutine. The following code will set the URL of the frontmost Safari window to "http://lisppad.objecthub.net".
(apply-applescript-proc script "setSafariFrontURL" '("http://lisppad.objecthub.net"))
(lisppad applescript)provides a means to quickly create Scheme functions matching AppleScript subroutines. This is shown in the following code:
(define safari-front-url (applescript-proc script "safariFrontURL"))
(define set-safari-front-url! (applescript-proc script "setSafariFrontURL"))
This is how library
(lisppad applescript)is marshaling/unmarshaling data when data is exchanged between Scheme and AppleScript:
If other data is attempted to be exchanged, it might lead to failure.
#tif obj is an AppleScript object,
Loads and compiles the AppleScript file at path returning an AppleScript object that can be used to execute the script or subroutines defined by the script.
Returns the file path from which the AppleScript object script was created.
Executes the given AppleScript script. The execution is synchronous and
execute-applescriptwill only return once script has been executed.
(apply-applescript-proc script name args)
Invokes the subroutine name defined by AppleScript script with the arguments args. name is a string, script is an AppleScript object, and args is a list of arguments passed on to the subroutine.
apply-applescript-procreturns the result returned by the subroutine, i.e. the execution of the subroutine is synchronous.
(applescript-proc script name)
Returns a Scheme procedure for subroutine name defined in AppleScript script. name is a string and script is an AppleScript object.
applescript-procis defined in the following way:
(define (applescript-proc script name)
(apply apply-applescript-proc script name args)))