[英]Need a way to load image file into Mac clipboard using Python
I have a Python program that I'm porting to Mac.我有一个要移植到 Mac 的 Python 程序。 I need to load a saved image file into the clipboard so that it can be pasted into a document using cmd + v.
我需要将保存的图像文件加载到剪贴板中,以便可以使用 cmd + v 将其粘贴到文档中。
This was the closest thread to what I need but the solutions don't work because my osascript filepath is unknown. 这是最接近我需要的线程,但解决方案不起作用,因为我的 osascript 文件路径未知。 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.
它是用户在 Python 中定义的一个变量,我正在努力使用将变量从 Python 传递到 osascript 所需的语法。
This doesn't work:这不起作用:
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 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)" inpath 打印为: /Users/admin/Desktop/PROGRAMMING 这是正确的路径,但会导致“执行错误:无法将文件“:+ str(inpath)+:tc.jpg”转换为类型文件。(-1700 )"
Nor does this:这也不是:
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)'])
It results in: "syntax error: Expected “,” but found “"”.它导致:“语法错误:应为“,”但找到“”。 (-2741)"
(-2741)"
The following:以下:
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)'])
Results in: "SyntaxError: EOF while scanning triple-quoted string literal"结果:“语法错误:扫描三引号字符串时出现 EOF”
Any help would be greatly appreciated!任何帮助将不胜感激!
EDIT: 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)" ])
Now returns: "NameError: name 'inpath' is not defined"现在返回:“NameError: name 'inpath' is not defined”
EDIT 2: Completes without error but fails to load to clipboard.编辑 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()
This returns no errors and prints correct path but does not add the file to the clipboard.这不会返回错误并打印正确的路径,但不会将文件添加到剪贴板。
You have likely got a linefeed at the end of your string inpath
, so try:您可能在字符串
inpath
的末尾有换行符,因此请尝试:
inpath = line[2].strip('\n')
Then you want:然后你想要:
subprocess.run(["osascript", "-e", "set the clipboard to (read (POSIX file \"" + inpath + "/tc.jpg\") as JPEG picture)" ])
Mac has a command line utility for this: Mac为此提供了一个命令行实用程序:
pbcopy pbcopy
ONLY reads std in. Eg只读取标准输入。例如
cat image.jpg |猫图片.jpg | pbcopy
pbcopy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.