Search code examples
macoscocoaapplescriptworkflowautomator

NSUserAutomatorTask variables array/list values cannot be parsed by Automator actions


I am using NSUserAutomatorTask to launch an Automator workflow from a macOS app.

I'm passing in variables via the variables property: https://developer.apple.com/documentation/foundation/nsuserautomatortask/1418099-variables

Inside the Automator workflow I will then use Get Value of Variable actions to grab the variables that were set on the NSUserAutomatorTask and pass the variables to subsequent Automator actions:

Get Value of Variable


Code looks like this: (simplified; I also check that the variables exist in the workflow)

let workflow = try! NSUserAutomatorTask(url: url)
workflow.variables = [
    "singleFilePath": "/image1.png",
    "multipleFilePaths": ["/image1.png", "/image2.png"],
]
workflow.execute(withInput: nil)

I'm able to print out the variable values in an alert via an Ask for Confirmation action:

variable string values.

The String variable's value is a simple string: /image1.png

The [String] array variable's value is wrapped in parentheses and each item is quoted:

(
  "/image1.png",
  "/image2.png"
)

I now run the pictured Automator workflow that first gets a variable's value and then attempts to open those Finder items.

The singleFilePath var works. It's passed on to the Open Finder Items action, and that file is opened by the default application.

Passing multipleFilePaths in the same manner does not work. No files are opened. Automator displays the error:

The action "Open Finder Items" was not supplied with the required data.


The action is "Open Finder Items", so there must be some way to pass multiple file paths to this action.

My questions and solutions in order of preference are:

  1. Why is the default variable array/list format not working when passed to the subsequent action? Is there any way to pass or parse the array variable in a compatible format?

  2. Can we use a single Run AppleScript action to reformat the array variable into a format that can be passed to subsequent Automator actions? (I'd like to continue to chain Automator actions rather than run pure AppleScript).


Solution

  • Open Finder Items actions can open an array of items if used in a normal non-variables workflow. And it's seemingly in the exact same format.

    As a control to test this, rather than the variable, I'm using the Get Specified Finder Items action.

    I then use a View Results action to inspect what is sent to Open Finder Items.

    The results are seemingly exactly the same as when I parse the variable:

    (
      "/image1.png",
      "/image2.png"
    )
    

    Get Specified Finder Items

    This workflow does correctly run the final action and open the files.

    So Open Finder Items should work with the data I'm setting in the variable. I'm unsure why the variable data cannot be opened but manually-picked files can.


    I opened a DTS ticket for this issue and received the following response:

    I’m sorry to say the engineers working on this have confirmed that yes, it’s a bug, and no, there isn’t a workaround for sandboxed apps. They’ve also noted this isn’t a regression in behavior from previous releases.