Applescript不';我没有文件的写入权限

Applescript不';我没有文件的写入权限,applescript,Applescript,因此,我正在尝试编写一个脚本,将我希望的任何文本写入桌面上的新文件 set the theText to "hello world" set fileName to "file.txt" set thePath to "~/Desktop/" try tell application "Finder" to make file at thePath with properties {name:fileName

因此,我正在尝试编写一个脚本,将我希望的任何文本写入桌面上的新文件

set the theText to "hello world"
set fileName to "file.txt"
set thePath to "~/Desktop/"
try
    tell application "Finder" to make file at thePath with properties {name:fileName}
end try
set myFile to open for access (thePath & fileName) with write permission
write theText to myFile
close access myFile

此代码基于上第二个最受欢迎的答案。但是,当我尝试执行上面的版本时,它会给我一个错误:
“文件未以写入权限打开。”编号-61从“~/Desktop/File.txt”到«class fsrf»
。我试着运行我的代码所基于的代码,它工作得完美无缺。我还试着用
choosefile
运行它,这也很有效。为什么Applescript在使用预定义路径时会出现问题?

正如注释中所述,查找程序不知道POSIX路径

但是,即使它确实
openforaccess
不知道如何展开波浪线,您也必须指定完整路径

创建用于将文本写入的文件时,根本不需要Finder。基本上这已经足够了

set the theText to "hello world"
set fileName to "file.txt"
set thePath to "/Users/myself/Desktop/"
set myFile to open for access (thePath & fileName) with write permission
write theText to myFile
close access myFile
但是,这是一种不好的做法,因为write命令可能会失败,然后文件仍处于未定义的打开状态

最可靠和用户独立的语法是

set the theText to "hello world"
set fileName to "file.txt"
set thePath to POSIX path of (path to desktop)
set myFile to thePath & fileName
try
    set fileDescriptor to open for access myFile with write permission
    set eof of fileDescriptor to 0
    write theText to fileDescriptor starting at eof
    close access fileDescriptor
on error
    try
        close access myFile
    end try
end try

正如注释中已经提到的,查找器不知道POSIX路径

但是,即使它确实
openforaccess
不知道如何展开波浪线,您也必须指定完整路径

创建用于将文本写入的文件时,根本不需要Finder。基本上这已经足够了

set the theText to "hello world"
set fileName to "file.txt"
set thePath to "/Users/myself/Desktop/"
set myFile to open for access (thePath & fileName) with write permission
write theText to myFile
close access myFile
但是,这是一种不好的做法,因为write命令可能会失败,然后文件仍处于未定义的打开状态

最可靠和用户独立的语法是

set the theText to "hello world"
set fileName to "file.txt"
set thePath to POSIX path of (path to desktop)
set myFile to thePath & fileName
try
    set fileDescriptor to open for access myFile with write permission
    set eof of fileDescriptor to 0
    write theText to fileDescriptor starting at eof
    close access fileDescriptor
on error
    try
        close access myFile
    end try
end try

查找程序不知道POSIX路径。@red_nerge我以前见过类似的关于查找程序和POSIX路径的评论,但我不理解。我的理解是posix路径只是文件说明符,并且内置在applescript中——与别名和文件相同。我不确定发现者和它有什么关系?您当然可以告诉查找器使用posix样式的引用。请注意,我不是在质疑答案,因为我同意不需要查找程序来创建文件。只是想知道您是否可以详细说明上面的评论。@Mockman:OP试图使用Finder创建一个使用POSIX路径的文件-一些术语可以使用任何一种格式,但Finder使用冒号分隔的HFS路径。将查找器与POSIX路径一起使用时,需要使用
POSIX文件
说明符来指定使用POSIX路径的文件(
System Events
)。有关更多信息,请参阅AppleScript语言指南中的一节。但这不只是语法问题吗?OP的代码可以通过简单地包括“posix文件”来创建该文件(将tilde问题放在一边,不管怎样都需要修复)。同样,我同意这个答案,但如果目的是使用shell脚本来编写文本和做其他事情,那么posix文件方法就可以了。用这种方式来描述它听起来有点不祥,好像这种方法充满了危险,注定要失败。即使有更好的方法,它也能工作。@MockmanAppleScript是OSX之前时代的遗物。在System 7–9中,没有UNIX子基,因此没有POSIX路径。在OS X中,苹果逐渐在许多API(如系统事件和脚本添加)中启用了对POSIX路径的支持,但Finder始终停留在HFS气泡中。Finder不知道POSIX路径。@red_威胁我以前见过关于Finder和POSIX路径的类似评论,但我不理解。我的理解是posix路径只是文件说明符,并且内置在applescript中——与别名和文件相同。我不确定发现者和它有什么关系?您当然可以告诉查找器使用posix样式的引用。请注意,我不是在质疑答案,因为我同意不需要查找程序来创建文件。只是想知道您是否可以详细说明上面的评论。@Mockman:OP试图使用Finder创建一个使用POSIX路径的文件-一些术语可以使用任何一种格式,但Finder使用冒号分隔的HFS路径。将查找器与POSIX路径一起使用时,需要使用
POSIX文件
说明符来指定使用POSIX路径的文件(
System Events
)。有关更多信息,请参阅AppleScript语言指南中的一节。但这不只是语法问题吗?OP的代码可以通过简单地包括“posix文件”来创建该文件(将tilde问题放在一边,不管怎样都需要修复)。同样,我同意这个答案,但如果目的是使用shell脚本来编写文本和做其他事情,那么posix文件方法就可以了。用这种方式来描述它听起来有点不祥,好像这种方法充满了危险,注定要失败。即使有更好的方法,它也能工作。@MockmanAppleScript是OSX之前时代的遗物。在System 7–9中,没有UNIX子基,因此没有POSIX路径。在OS X中,Apple逐渐在许多API(如系统事件和脚本添加)中启用了对POSIX路径的支持,但Finder始终停留在HFS气泡中。它工作得很好,只是它没有删除以前文件中的旧文本,只是覆盖了它。因此,如果我有
“hello world”
,然后只是
“Dog”
,它将不是
Dog
,而是
Doglo world
,但我只希望它是
Dog
。请查看编辑。它工作得很好,只是它没有删除以前文件中的旧文本,只是覆盖了它。因此,如果我有
“hello world”
,然后只是
“Dog”
,它将不是
Dog
,而是
Doglo world
,但我只希望它是
Dog
。请查看编辑。