Frage

enter image description here

So i'm creating a workflow in Apple's Automator that uses Python scripts in the shell

This is what each step is doing right now:

1 . I paste a column of names from excel

2 . Organizes them in a list ... e.g. ['a','b','c'] . . . this is where it gets weird

3 . I select a text file on the finder item which is an input in the 2nd python script (#4) but the problem is that I also need the list generated from #2 in my script

4 . This script is supposed to use the list from #2 and the file selected by #3

When I didn't have #3 in there it was working fine because I used the sys.argv1 to get the variable to transfer but i don't know how to skip feeding that into "Ask for Finder Item" and straight into #4

basically i'm having trouble inputing the list from the workflow into the script WHILE selecting a file for another variable so i can have:

my_list = sys.argv[1] #from step 2
my_file = sys.argv[2] #selected from step 3
War es hilfreich?

Lösung

Unless you select "Ignore this action's input" in #3, it will just append the selected file(s) to output from script #2 as its output, so script #4 will get them.

The problem is that you get an implicit "Conversion from Text to Files/Folders" feeding #2 into #3. So, if #2's output is a list of strings that look like pathnames but aren't, they will get converted into pathnames to files that don't exist, which will get converted into references to files that don't exist, which will just get dropped when #3's output gets converted back to text for #4.


The easy way around this (conceptually easy; it does mean a bit of extra code…) is to have script #2 store the list in a temporary file, and print out the filename. That filename will pass through the conversions and come out the other end just fine, so script #4 can open and read the file to get the list back.

To take a silly example, let's say you were doing this:

import sys
new_list = sorted(sys.argv[1:])
print '\n'.join(new_list)

Instead, do this:

import sys
import tempfile
new_list = sorted(sys.argv[1:])
with tempfile.NamedTemporaryFile('w', delete=False) as f:
    f.write('\n'.join(new_list))
    print f.name

Then, in script #4, instead of this:

import sys
new_list = sys.argv[1:-1]
step3 = sys.argv[-1]

… do this:

import sys
with open(sys.argv[1]) as f:
    new_list = list(f)
os.remove(sys.argv[1])
step3 = sys.argv[2]

Of course in a more realistic example, you'd probably want to use, e.g., pickle.dump instead of just '\n'.join, but this should be enough to show the idea.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top