【问题标题】:AppleScript cannot save a textedit fileAppleScript 无法保存文本编辑文件
【发布时间】:2019-02-13 02:20:27
【问题描述】:

我正在使用代码为实验室数据格式化文本编辑文件。

我目前正在使用 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

但我不确定如何实施这些/如果我应该实施,如果没有,我不确定如何解决这个问题。

提前致谢

【问题讨论】:

    标签: applescript


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      不幸的是,该错误消息没有多大帮助。您实际上是在尝试保存到一个字符串中,但这是行不通的 - 您需要使用 文件说明符,例如:

      close document 1 saving in POSIX file ("/Users/bo/Desktop/Script Doc/" & n & ".txt")
      

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

      【讨论】:

        【解决方案3】:

        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
        

        【讨论】:

          猜你喜欢
          • 2014-11-07
          • 2017-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-24
          • 1970-01-01
          相关资源
          最近更新 更多