使用Python将图像文件加载到Mac剪贴板的方法。

3
我有一个Python程序,正在将其移植到Mac。我需要将一个保存的图像文件加载到剪贴板中,以便可以使用cmd + v将其粘贴到文档中。
这个帖子是我所需内容最接近的,但解决方案不起作用,因为我的osascript文件路径未知。 它是由用户在Python中定义的变量,并且我正在努力理解必须从Python传递变量到osascript所需的语法。
以下代码不起作用:
import subprocess


def imagepath():                               
    f=open('config.txt')
    line=f.readlines()
    inpath = (line[2])    
    print(inpath)
    return(inpath)

imagepath()

subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file  "+ str(inpath) + /tc.jpg") as JPEG picture)'])

inpath 打印的是:/Users/admin/Desktop/PROGRAMMING,这是正确的路径,但它导致了“执行错误:无法将文件":+ str(inpath) + :tc.jpg”转换为文件类型。 (-1700) "。

也不会出现这种情况:

import subprocess


def imagepath():                               
    f=open('config.txt')
    line=f.readlines()
    inpath = (line[2])    
    print(inpath)
    return(inpath)

imagepath()

subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file  """+ str(inpath) + /tc.jpg""") as JPEG picture)'])

结果为:"语法错误:期待“,”但找到“"”。 (-2741)"。
以下内容:
import subprocess


def imagepath():                                 # check line 1 of config file (screencap name)
    f=open('config.txt')
    line=f.readlines()
    inpath = (line[2])    # note: confusing.  0=line 1, 1=line2 etc.
    print(inpath)
    return(inpath)

imagepath()

subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file  ''' + str(inpath) + '''/tc.jpg") as JPEG picture)'])

结果为:"SyntaxError: 扫描三引号字符串文字时遇到 EOF"

非常感谢任何帮助!

编辑:下面是更新的代码:



def imagepath():                                 # check line 1 of config file (screencap name)
    f=open('config.txt')
    line=f.readlines()
    inpath = line[2].strip('\n')
    print(inpath)
    return(inpath)

imagepath()

subprocess.run(["osascript", "-e", "set the clipboard to (read (POSIX file \"" + inpath  + "/tc.jpg\") as JPEG picture)" ])

现在返回:"NameError:名称 'inpath'未定义"。
编辑2:完成时没有错误,但无法加载到剪贴板。
import subprocess


def imagepath():                                 # check line 1 of config file (screencap name)
    f=open('config.txt')
    line=f.readlines()
    inpath = (line[2]).strip('\n')
    print(inpath)
    return(inpath)
    subprocess.run(
        ["osascript", "-e", "set the clipboard to (read (POSIX file \"" + inpath + "/tc.jpg\") as JPEG picture)"])
imagepath()

这段代码没有出现错误,打印了正确的路径,但是没有将文件添加到剪贴板中。

1
你需要获取返回值,inpath = imagepath() - Mark Setchell
1
你已经在函数返回之后添加了子进程调用!它不会在那里运行。删除 return(inpath) 部分。 - Mark Setchell
正在工作。新手错误,非常感谢你! - Matt W
2个回答

1
您可能在字符串inpath的末尾有一个换行符,因此请尝试以下操作:
inpath = line[2].strip('\n')

然后您想要:
subprocess.run(["osascript", "-e", "set the clipboard to (read (POSIX file \"" + inpath  + "/tc.jpg\") as JPEG picture)" ])

谢谢您的回复。我尝试了这个方法,但没有结果。以下是正确的语法吗?subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file " + str(inpath) + /tc.jpg") as JPEG picture)']) - Matt W
1
我已经更新了我的答案,包括subprocess部分,请再看一下。 - Mark Setchell
非常感谢您的帮助。我已经完全复制/粘贴了您的代码,但现在出现了“NameError: name 'inpath' is not defined”的错误。如果我还有其他问题,请参考原始问题中附加的完整代码。 - Matt W
我尝试在函数imagepath()内运行子进程,然后执行imagepath()。它打印出正确的路径,并且没有错误,但是它没有将图像复制到剪贴板。 - Matt W
1
@MattW 请在您的代码下点击 编辑 并展示您运行的实际代码和错误。谢谢。 - Mark Setchell

0

Mac系统有一个命令行实用程序:

pbcopy

它只能读取标准输入。例如:

cat image.jpg | pbcopy


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接