本文介绍了需要一种使用Python将图像文件加载到Mac剪贴板的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要移植到Mac的Python程序。我需要将保存的图像文件加载到剪贴板中,以便可以使用cmd + v将其粘贴到文档中。

I have a Python program that I'm porting to Mac. I need to load a saved image file into the clipboard so that it can be pasted into a document using cmd + v.

,但该解决方案无法正常工作,因为我的osascript文件路径未知。这是用户在Python中定义的变量,我正在努力将变量从Python传递到osascript所需的语法。

This was the closest thread to what I need but the solutions don't work because my osascript filepath is unknown. It is a variable defined in Python by the user and I'm struggling with syntax necessary to pass the variable from Python to 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)

inpath prints as: /Users/admin/Desktop/PROGRAMMING which is the correct path but it results in "execution error: Can’t make file ":+ str(inpath) + :tc.jpg" into type file. (-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)

It results in: "syntax error: Expected "," but found """. (-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用三引号括起来的字符串文字

Results in: "SyntaxError: EOF while scanning triple-quoted string literal"

任何帮助将不胜感激!

Any help would be greatly appreciated!

编辑:下面的更新代码:

Updated code below:



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'

Now returns: "NameError: name 'inpath' is not defined"

EDIT 2:完成但没有错误,但无法加载到剪贴板。

EDIT 2: Completes without error but fails to load to clipboard.

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

这不会返回错误,并打印正确的路径,但不会将文件添加到剪贴板。

This returns no errors and prints correct path but does not add the file to the clipboard.

推荐答案

您可能在字符串 inpath 的末尾有换行符,因此请尝试:

You have likely got a linefeed at the end of your string inpath, so try:

inpath = line[2].strip('\n')

然后您想要:

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

这篇关于需要一种使用Python将图像文件加载到Mac剪贴板的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:08