Tuesday, May 11, 2021

#1 2021-03-06 02:01:59 pm

Registered: 2021-03-02
Posts: 1

AppleScript to pass variables to external application

Hello, hoping you can help.

I'm trying to automate a workflow and have created an applescript 'application' which creates calendar events in iCal.

From the customers CRM database it allows scripts which can call the applescript 'application' I have created. I'm struggling to pass variables from the script to the application it's calling to run (hope that makes sense).

If the 'application' for creating the calendar appointments was in fact saved as a 'script'. I have found I can pass variables between 2 scripts easily with an 'on run' and:


set appPath to (path to desktop as text) & "testhandlerApp.app"
run script file appPath with parameters {"Hi"}

So you might say just use a script then. I've found I have to create it as a standalone app because of OSX privacy permissions. If a 'script' is called from the database, it will give error permissions. If an 'application' is called from the database it will invoke apple's 'app' is trying to access calendar and it can be allowed.

From what I've read I may have to do this with defining application 'properties'. Or is there an alternative such as system global variables, or write them to text file and retrieve.

Thanks in advance.

Filed under: applescript



#2 2021-04-07 06:01:36 am

From:: Massachusetts
Registered: 2006-01-03
Posts: 377

Re: AppleScript to pass variables to external application

Ways of passing a value or values to an application at launch time were discussed here. In that post, I showed a way to pass an argument directly. Nigel Garvey also described a more traditional approach using an intermediary file, and Shane Stanley described a hybrid approach.

The direct approach involves serializing the value to be passed into its text string representation, passing that text string as the single argument of the application launch command, receiving the passed text string in the launched application via ASObjC's NSProcess class, and deserializing the text string back to its original value via AppleScript's run script command. In order to pass multiple arguments, package the arguments into a single AppleScript list or record. The following is an example of passing multiple arguments packaged into an AppleScript list directly to an application at launch time:

In the launching script:


set valueToBePassed to {1, 2.2, "three", true, {4, 5, 6}, {aa:7, bb:8, cc:9}, current date, missing value, null}
set serializedValueToBePassed to my serializeValue(valueToBePassed)
do shell script ("open -a '[name of application to be launched]' --args " & serializedValueToBePassed's quoted form)

on serializeValue(theValue)
   -- Returns the text representation of the input value
   set tid to AppleScript's text item delimiters
       || of {theValue} -- embedding the object in a list allows proper handling of certain object types (e.g., script objects)
   on error m
           set AppleScript's text item delimiters to "{"
           set m to m's text items 2 thru -1 as text
           set AppleScript's text item delimiters to "}"
           set serializedValue to m's text items 1 thru -2 as text
       on error
           error "The input value could not be serialized."
       end try
   end try
   set AppleScript's text item delimiters to tid
   return serializedValue
end serializeValue

In the launched application:


use framework "Foundation"
use scripting additions

set valueToBePassed to run script ((current application's NSProcessInfo's processInfo's arguments() as list)'s second item) --> {1, 2.2, "three", true, {4, 5, 6}, {aa:7, bb:8, cc:9}, date "Wednesday, April 7, 2021 at 8:01:07 AM", missing value, null}

Last edited by bmose (2021-04-07 06:31:30 am)



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)