mac怎么用python读取desktop上图片_需要一种使用Python将图像文件加载到Mac剪贴板的方法...

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.

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.

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

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

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"

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"

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.

解决方案

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

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)" ])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值