首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AppleScript无法保存textedit文件

AppleScript无法保存textedit文件
EN

Stack Overflow用户
提问于 2019-02-12 00:31:44
回答 3查看 1.2K关注 0票数 1

我正在使用代码来格式化实验室数据的textedit文件。

我目前正在使用AppleScript在TextEdit中创建文档并向其添加文本,但当我试图保存它时,TextEdit给出了一个错误:“您没有权限将其保存为blahblahblah。”我已经尝试过更改保存它的文件夹的权限,但我认为这可能与AppleScript创建的文件有关。

准确的错误输出是来自TextEdit的对话框。

文件“1.txt”无法保存为“1”。你没有得到许可。 若要查看或更改权限,请选择“查找器”中的项并选择“文件”>“获取信息”。

我的代码中不起作用的部分是

代码语言:javascript
复制
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

我搜索了其他问题,并找到了代码段。

代码语言:javascript
复制
open for access document 1
close access document 1

但是,我不知道如何实现这些/如果我甚至应该,如果不是,我不知道如何解决这个问题。

提前感谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-12 18:13:11

使用最新版本的macOS Mojave直接保存到桌面工作

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2019-02-12 03:59:30

不幸的是,这条错误信息并没有多大帮助。实际上,您正在尝试保存到一个字符串中,该字符串将无法工作--例如,您需要使用文件说明符

代码语言:javascript
复制
close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")

注意,并不是所有的人都知道POSIX路径,在这种情况下,您需要像我的示例那样强制或指定它。

票数 0
EN

Stack Overflow用户

发布于 2019-02-12 08:26:35

TextEdit是沙箱。无法将文档保存在标准桌面文件夹中。

另一种方法是对TextEdit容器中的documents文件夹的路径进行硬编码。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54641142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档