Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AppleScript无法保存文本编辑文件_Applescript - Fatal编程技术网

AppleScript无法保存文本编辑文件

AppleScript无法保存文本编辑文件,applescript,Applescript,我正在使用一个代码来格式化实验室数据的文本编辑文件 我目前正在使用AppleScript在TextEdit中创建文档并向其中添加文本,但当我尝试保存文档时,TextEdit会显示错误“您没有保存为blahblahblah的权限”。我已尝试更改保存该文档的文件夹的权限,但我认为这可能与AppleScript创建的文件有关 确切的错误输出是TextEdit的一个对话框 无法将文档“1.txt”另存为“1”。你没有权限 要查看或更改权限,请在Finder中选择项目,然后选择文件>获取信息 我的代码中不

我正在使用一个代码来格式化实验室数据的文本编辑文件

我目前正在使用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
但是我不确定如何实现这些/如果我应该,如果不,我也不确定如何解决这个问题


提前感谢

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

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

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

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

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

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

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

文本编辑是沙盒。无法将文档保存在标准桌面文件夹中

另一种方法是硬编码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

使用最新版本的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

使用最新版本的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