我正在使用代码格式化textedit文件以获取实验室数据。
我目前正在使用AppleScript在TextEdit中创建文档并向其添加文本,但是当我尝试保存它时,TextEdit给出了错误“您没有权限保存为blahblahblah”。我已经尝试更改我保存它的文件夹的权限,但我认为它可能与它是AppleScript创建的文件有关。
确切的错误输出是TextEdit的对话框
文档“1.txt”无法保存为“1”。你没有权限。
要查看或更改权限,请在Finder中选择项目,然后选择“文件”>“获取信息”。
我的代码段不起作用
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
但我不知道如何实现这些/如果我应该,如果没有,我不知道如何解决这个问题。
提前致谢
直接保存到桌面适用于我使用最新版本的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
不幸的是,该错误消息没有多大帮助。你本质上是试图保存到一个不起作用的字符串 - 你需要使用文件说明符,例如:
close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")
请注意,并非一切都知道POSIX路径,在这种情况下,您需要强制或指定它,如我的示例中所示。
TextEdit是沙盒。您无法将文档保存在标准桌面文件夹中。
另一种方法是对TextEdit容器中的文档文件夹的路径进行硬编码。
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 tell