我正在使用代码来格式化实验室数据的textedit文件。
我目前正在使用AppleScript在TextEdit中创建文档并向其添加文本,但当我试图保存它时,TextEdit给出了一个错误:“您没有权限将其保存为blahblahblah。”我已经尝试过更改保存它的文件夹的权限,但我认为这可能与AppleScript创建的文件有关。
准确的错误输出是来自TextEdit的对话框。
文件“1.txt”无法保存为“1”。你没有得到许可。 若要查看或更改权限,请选择“查找器”中的项并选择“文件”>“获取信息”。
我的代码中不起作用的部分是
tell application "TextEdit"
make new document with properties {name:("1.txt")}
end tell
--data formatting code here (n is set here)
tell application "TextEdit"
delay 1
close document 1 saving in ("/Users/bo/Desktop/Script Doc/" & (n as string))
set n to n + 1
make new document with properties {name:((n as string) & ".txt")}
delay 1
end tell我搜索了其他问题,并找到了代码段。
open for access document 1
close access document 1但是,我不知道如何实现这些/如果我甚至应该,如果不是,我不知道如何解决这个问题。
提前感谢
发布于 2019-02-12 18:13:11
使用最新版本的macOS Mojave直接保存到桌面工作
property outputPath : POSIX path of (((path to desktop as text) & "Script Doc") as alias)
property docNumber : 1
property docExtension : ".txt"
tell application "TextEdit"
set docName to (docNumber & docExtension) as text
set theText to "Your Desired Text Content"
set thisDoc to make new document with properties {name:docName, text:theText}
close thisDoc saving in POSIX file (outputPath & "/" & (name of thisDoc))
(* IF YOU WANT TO SAVE MULTIPLE FILES WITH CONSECUTIVE NAMES *)
set docNumber to docNumber + 1
set docName to (docNumber & docExtension) as text
set thisDoc2 to make new document with properties {name:docName} -- Removed Text Property This Time
close thisDoc2 saving in POSIX file (outputPath & "/" & (name of thisDoc2))
end tell发布于 2019-02-12 03:59:30
不幸的是,这条错误信息并没有多大帮助。实际上,您正在尝试保存到一个字符串中,该字符串将无法工作--例如,您需要使用文件说明符:
close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")注意,并不是所有的人都知道POSIX路径,在这种情况下,您需要像我的示例那样强制或指定它。
发布于 2019-02-12 08:26:35
TextEdit是沙箱。无法将文档保存在标准桌面文件夹中。
另一种方法是对TextEdit容器中的documents文件夹的路径进行硬编码。
tell application "TextEdit"
make new document with properties {name:"1.txt"}
end tell
set containerDocumentsFolder to (path to library folder from user domain as text) & "Containers:com.apple.TextEdit:Data:Documents:"
tell application "TextEdit"
close document 1 saving in containerDocumentsFolder & (n as string) & ".txt"
set n to n + 1
make new document with properties {name:(n as string) & ".txt"}
end tellhttps://stackoverflow.com/questions/54641142
复制相似问题