Automator array variable cannot be parsed by Automator actions

I’m wondering if anyone can explain this behavior of array formatting inside an Automator workflow. Hope this question is allowed on this forum.


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

I’m passing in variables via the variables property. I’m passing both a String and an Array value variable in the variables dictionary.

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:

IMAGE: ht//tps://i.stack.imgur.com/mDmfd.png


In the Mac app the Swift code looks like this:

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:

IMAGE: ht//tps://i.stack.imgur.com/1njN4.png

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"
)

My control is to write a “normal” workflow, rather than variables, and use a Get Specified Finder Items action + View Results action:

The array/list format is exactly the same as when I use the variable.


When I run the 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).

Thank you for any help you might offer!

The Open Finder Items action is expecting files as input, not paths. Try passing NSURLs.

@ShaneStanley do you have any more info about the type system in Automator? I’m surprised it would differentiate between paths and URLs.

When I instead add NSURL objects as the variables, I get the error:

-[AMApplicationRegistry _loadDefinitionsAtURLS:]: Failed to load definition at URL Automator/SIUDefinition.definition/
-- file:///Applications/System/Library/CoreServices/Applications/System%20Image%20Utility.app/Contents/Library/

I’m also interested if there’s anything I can type into the variable’s value field directly that will open multiple files.

This would remove the Swift / NSUserAutomatorTask setup from the equation and possible problem.

Newlines via ctrl+enter do not work. Using the parenthesis + quoted strings format does not work.

Screen Shot 2020-04-19 at 11.07.46 AM

Why? Have you tried passing a string to a function that expects a URL?

I’m afraid I still don’t understand what you are trying to achieve. The normal way to pass values to a workflow is via the input parameter, where you pass an array.

@ShaneStanley What I mean is that I didn’t think Automator had a robust type system that differentiated between URL objects and string paths. From my experience the values generated by Get Specified Finder Items and other actions are an array of strings… not URLs. If they are URLs, I don’t know how to see that or find any documentation about the format they are using. It looks like everything is using paths, but maybe I’m wrong.

As for what I’m trying to achieve… I want to pass an array of files (paths or URLs or ???) into a variable that can then be opened by an Open Finder Items action or other similar stock Automator actions.

I’m using variables currently and I’d like to keep it that way, for now. I will explore using this input parameter as well, but it seems like this should work with variables.

Make a workflow, add Get Specified Finder Items and then add Run AppleScript. Add this script to the latter:

on run {input, parameters}
	display dialog (class of item 1 of input)
	return input
end run

Add some files and run it, and you will see alis, which is the AppleScript alias class – that’s what NSURLs get converted into for AppleScript.

If you want to pass input to standard actions, you need to provide the input parameter. The name is a fairly serious clue.

@ShaneStanley

Passing data via variables works just fine for the String variable. I pass a path String and am able to open a file. No input is required.

In Automator, Get Value of Variable > Open Finder Items opens that file.

The question is why that doesn’t work for array variables.


That’s interesting about alis and I’m going to troubleshoot that more. But passing NSURLs rather than strings in the array gives the error above.


The reason I want to set variables rather than pass input is because I’m passing 3 distinct sets of information to every launched Automator workflow. I’d prefer to set variables if avaiable, then have the Workflow developer Get Value of Variable for any/all of the data that they want. Rather than requiring them to parse the NSSecurceCoding input.

Did you use Storage variables?

@ShaneStanley Yes, I believe so, although I would not have known to call them “Storage” variables.

Images of this are in my original post, although I had to escape them due to the age of my account.

Make sure the Value field of Variable Options is empty.

@ShaneStanley The value is definitely empty in the Automator file itself. The value is supplied by the Mac app invoking it.

Sorry, I’m still not following your reasoning, and I’m out of ideas.

@ShaneStanley why is my reasoning hard to understand? I’m setting an array variable and then want to use it. Very simple problem.

That’s not working due to some type mismatch or something breaking the variable’s value and/or making it unreadable by the next action.

I heard back from Developer Technical Support:

I’m sorry to say DTS does not support Automator. You may want to ask your question in the Scripting and Automation section of the Apple Developer Forums.

This also certainly seems like it may be a bug. It appears you may have already filed a bug about this a while back; I’d suggest updating it with relevant information.

I’ve also pinged the associated engineers assigned to the bug to see if they can suggest a workaround (if one exists).

and then:

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.

Thanks very much for posting the follow-up. Alas, the situation doesn’t sound very hopeful.