【发布时间】:2017-11-06 17:25:02
【问题描述】:
因为pbpaste不适用于图像数据,我使用以下命令将剪贴板的PNG内容从终端保存到文件中(实际上,来自另一种语言的system()命令,但是该问题也出现在终端):
osascript -e "set png_data to the clipboard as «class PNGf»" \
-e "set the_file to open for access POSIX path of \
(POSIX file \"image.png\") with write permission" \
-e "write png_data to the_file" \
-e "close access the_file"
如果文件已经存在,此命令将按需要工作,覆盖其内容,但如果文件不存在,我会收到错误:
execution error: Network file permission error. (-5000)
从 AppleScript 写入新文件的正确方法是什么?有没有比这更好的从终端/系统()命令将剪贴板数据保存到文件的方法?
【问题讨论】:
-
/image.png 的路径是什么?
-
@jweaks 该命令与问题中出现的相对路径为
image.png完全相同。我从我的主目录运行命令,~。 -
AppleScript 不支持相对路径。始终使用绝对路径。还要确保在覆盖之前
set eof the_file to 0,否则您可能会将旧数据留在新文件的末尾。 -
@has 一旦我得到它的工作,我实际上永远不会覆盖文件,但感谢您的提示!如果您能对my comment to jweaks's answer below 有所了解,我将不胜感激!
标签: macos shell applescript