Zax
Zax

Reputation: 199

Get Automator app result in external Applescript?

Is there a way to retrieve result of an Automator app script in an external Applescript app (not the Applescript lines in Automator)?

Something like:

tell application "My_Automator_App"
   -- suppose My_Automator_App checks the Calendar to see if there some events today
   -- "Show Result" in Automator will display a list      

   get the_Result -- list returned by Automator
end tell

Upvotes: 0

Views: 636

Answers (1)

CJK
CJK

Reputation: 6102

I looked into this a little bit and didn't find a natural means by which AppleScript and Automator applets can communicate, although this doesn't mean one definitely doesn't exist.

In the meantime, you could implement one of a couple of workarounds/hacks that, although a little unseemly in their methods, do achieve the desired result without creating any side issues that would affect the functionality of an applet itself.

1. Use The Clipboard

Append a Copy to Clipboard action at the end of the applet's workflow, or following the action whose result you would wish to be reported.

Retrieving the clipboard from AppleScript is simple:

get the clipboard

This will probably suit return values that are simple text strings or a number. Passing an array of items from an Automator action to the clipboard isn't very reliable, sometimes only allowing access to the first item. However, this can be resolved with a small AppleScript within the workflow to process results arrays properly and convert them into an accessible format, e.g. a comma-delimited string.

However, the clipboard is also capable of storing image data, file references, and other data types, so it will be possible (if not always straightforward) to send those to be retrieved in an AppleScript.

Where possible, strings and numbers are the safest storage types.

2. Write Out To A Temporary File

To avoid using the clipboard as an intermediary, or if you wish the applet to report multiple variables without too much work, then writing the data to a temporary file is a fairly common practice, such as is done in shell scripts when persistant values are needed between multiple executions of the same script.

There's actually a special directory that gets periodically purged so that temporary data files don't accumulate: /tmp. It's hidden in Finder, but you can still create files and delete them as you would any other directory. Files that aren't access for 3 days get purged by the system.

There is a New Text File action that can write text to a file:

New Text File action in Automator on macOS

Specifying the /tmp directory is most easily done by creating a variable whose value is "/tmp" (without the quotes), and dragging that variable onto the appropriate field.

Creating a variable in Automator on macOS

But my inclination would be to insert an AppleScript, or more suitably, a shell script into the workflow, with which file manipulation becomes easy and more capable.

Calendar Events Example

Using a similar example to the scenario you described, a simple applet that retrieves calendar events might have a workflow that looks like this:

Automator Workflow

where you can calibrate the first action to isolate the events you want, such as today's events. That action returns a type of object that isn't easily processed by AppleScript, but the second action extracts the relevant data in text format, summarising the list of events that the first action returned.

This is where a temporary file is useful to write out the data to a text file, which can then be retrieved in an AppleScript.

Given this Automator applet saved under the named "CalEvents", this AppleScript makes use of that applet and its result:

    property tidEvents : [linefeed, linefeed, "EVENT", space] as text
    property tidDetails : {tab, " to "}
    property tid : a reference to my text item delimiters
    
    
    run application id "com.apple.automator.CalEvents"

    set tid's contents to tidEvents     
    set EventsSummary to read POSIX file "/tmp/EventsSummary.txt"
    set EventsList to the EventsSummary's text items
    
    set [[n], EventsList] to [it, rest] of EventsList
    set n to n's last word as number
    
    EventsList -- The final list of events from first to last
    

Upon its first run, the applet requires consent to access your calendar information, which only needs to be done once and will make the above script appear to fail. Once authorised, you can run the script as often as you like to get the most up-to-date content of the /tmp/EventsSummary.txt file.

Each item in the list variable EventsList is a block of text that looks like this (asterisks are my redactions for privacy, as are the address items in curly braces):

4 OF 8
Summary:    GP Appointment
Status:     none
Date:       07/12/2017 to 07/12/2017
Time:       14:45:00 to 15:45:00
Location:   ******** Medical Centre
{Address Line 1}
{Address Line 2}
{County}
{Post Code}
United Kingdom
Notes:      01*** *****9

Each value is separated from the preceding colon by a tab character, which won't be obvious here. Also, as you can tell from the date format and address, these are British-formatted values, but yours will, of course, be whatever they are set as in Calendar.

But since each list item is much the same, extracting details for a particular event will be simple in AppleScript, first by splitting a particular event item into paragraphs, and then splitting a particular paragraph by either a tab or space character (or both) or some preposition that naturally delimits useful bits of text:

    set |Event| to some item in the EventsList
    
    set tid's contents to tidDetails
    set EventDetails to {title:text item 2 of paragraph 2 ¬
        , startTime:text item 2 of paragraph 5 ¬
        , EndTime:text item 3 of paragraph 5} of the |Event|

which places the important event details, such as its name and start/end times, in an AppleScript record:

{title:"GP Appointment", startTime:"15:45:00", EndTime:"16:00:00"}

Upvotes: 1

Related Questions