我正在使用代码来格式化实验室数据的textedit文件。
我目前正在使用AppleScript在TextEdit中创建一个文档并向其中添加文本,但是当我尝试保存它时,TextEdit出现了错误“您无权另存为”。我已经尝试过更改保存它的文件夹的权限,但是我认为它可能与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
但是我不确定如何实现这些功能,如果不能,甚至不确定,我也不知道如何解决此问题。
预先感谢
答案 0 :(得分:0)
不幸的是,该错误消息并没有太大帮助。本质上,您试图将其保存为字符串,但这将不起作用-您需要使用文件说明符,例如:
close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")
请注意,并非所有人都知道POSIX路径,在这种情况下,您将需要像我的示例中那样强制或指定它。
答案 1 :(得分:0)
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 tell
答案 2 :(得分:0)
使用最新版本的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