1

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
4

1 回答 1

1

除非您在 #3 中选择“忽略此操作的输入”,否则它只会将所选文件附加到脚本 #2 的输出作为其输出,因此脚本 #4 将获取它们。

问题是你得到一个隐含的“从文本到文件/文件夹的转换”,将#2 输入到#3。因此,如果 #2 的输出是一个看起来像路径名但不是的字符串列表,它们将被转换为不存在文件的路径名,这些文件将被转换为对不存在文件的引用,这将当#3 的输出转换回#4 的文本时,就会被丢弃。


解决这个问题的简单方法(概念上很简单;这确实意味着一些额外的代码......)是让脚本 #2 将列表存储在一个临时文件中,并打印出文件名。该文件名将通过转换并很好地出现在另一端,因此脚本#4 可以打开并读取文件以取回列表。

举一个愚蠢的例子,假设你正在这样做:

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

相反,请执行以下操作:

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

然后,在脚本 #4 中,而不是这样:

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

… 做这个:

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

当然,在一个更现实的例子中,您可能希望使用,例如,pickle.dump而不是 just '\n'.join,但这应该足以说明这个想法。

于 2013-09-23T19:46:01.410 回答