2

从终端执行时,我有一个 python 脚本可以完美运行。但是当试图通过automator时,我只是得到“运行Shell脚本”错误。

目标:(请注意,在这里完成菜鸟,因此是“菜鸟”任务)在我的日常工作(不同的学科)中,我总是与 windows 和 mac 上的人合作。我在Mac上,因此尝试快速右键单击下拉服务以检索文件路径>查找/,替换为\以及服务器路径开头的差异>复制到剪贴板。这种方式给某人一个Windows版本的路径是一个点击和粘贴。脚本 101 我确定。

通过终端在我的 .py 文档上一切都很好......显然给了我我的文档的路径,到目前为止看起来很棒。为了调动这件事,我听说 Automator 是一个很好的方法。

设置时出现指定错误:Run Shell Script Shell: /usr/bin/python Pass Input: as arguments。代码:

import os

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

userlink = os.path.realpath(__file__)

reps = {'/':'\\','///':'\\', 'rr1mgsfile46':'gameshare\\share\\IEBDesign','smb:':''}

txt = replace_all(userlink, reps)


cmd = 'echo %r | tr -d "\n" | pbcopy' % txt
os.system(cmd)

我也简单地尝试使用 Shell: /bin/bash 并将其定向到 .py 并获得相同的结果。这里有很多猜测工作,所以我只能想象这一切都是错误的。但是,它确实在终端工作,所以我觉得我不能太远。

非常感谢所有帮助。

4

1 回答 1

1

无需赘述,这对我在 Snow Leopard 上有效。你可以测试它,如果你在前面放一个 Finder 'Ask for Finder Item' 对话。

import sys
import os

def replace_all(text, dic):
    for i, j in dic.iteritems():
        text = text.replace(i, j)
    return text

for userlink in sys.argv[1:]:
    # print('pbcopy ' + userlink) # this is for testing
    os.system('pbcopy ' + f)
    reps = {'/':'\\','///':'\\', 'rr1mgsfile46':'gameshare\\share\\IEBDesign','smb:':''}
    txt = replace_all(userlink, reps)

    cmd = 'echo %r | tr -d "\n" | pbcopy' % txt
    os.system(cmd)
于 2013-09-12T01:29:12.247 回答