I'm trying to create a log file like so:
我试着创建一个日志文件:
set logFilePath to (path to desktop folder as string) & "waiting.scpt.log" -- "/tmp/waiting.scpt.log" as text
-- set logFilePath to (path to "temp" from user domain as text) & "waiting.scpt.log"
try
close access file logFilePath
on error err
display dialog err
end try
set logFile to open for access logFilePath with write permission
I get a dialog saying File file tlv-mp3u2:Users:idror:Desktop:waiting.scpt.log wasn’t open.
and then an error error "File file tlv-mp3u2:Users:idror:Desktop:waiting.scpt.log is already open." number -49 from file "tlv-mp3u2:Users:idror:Desktop:waiting.scpt.log" to «class fsrf»
我得到一个对话框,说文件文件tlv-mp3u2:用户:idror:Desktop:waiting.scpt。日志不开放。然后一个错误错误“文件文件tlv-mp3u2:用户:idror:Desktop:waiting.scpt。日志已经打开了。“从文件中输入-49号”tlv-mp3u2:用户:idror:Desktop:waiting.scpt。«类fsrf»日志”
I moved the file in question to the trash and ran the script again, same result
我将文件移到垃圾中,再次运行脚本,同样的结果。
Also, what I really want is to open the file under /tmp, but if I try that I get "file access error" (-54)
另外,我真正想要的是打开/tmp下的文件,但是如果我尝试了,就会得到“文件访问错误”(-54)
I've given up finding answers in Google... so please help
我已经放弃在谷歌找到答案……所以请帮助
2 个解决方案
#1
0
CTRL-Click
an AppleScript
and check out the menu Error Handlers=>Write Error to Log
- the code shows exactly how it's done.
单击AppleScript并检查菜单错误处理程序=>写入错误日志-该代码显示了它是如何完成的。
#2
0
I don't understand the sequence of your calls. The Applescript way to access files is always the same:
我不明白你电话的顺序。获取文件的Applescript方法始终是相同的:
- Open the file and store a file id pointing to the opened file
- 打开文件并将一个文件id存储到打开的文件中。
- If needed get the index of the end of the file (needed to append text at the end)
- 如果需要获取文件末尾的索引(需要在末尾追加文本)
- Read/Write the content you want
- 读/写你想要的内容。
- Close the access to the file id
- 关闭对文件id的访问。
Your snippet looks like you try to close before opening the file. And if the opening works (should have been only once) the file remains open, because you don't close it. Maybe a restart helps or securly emptying the trash.
您的代码片段看起来像是在打开文件之前尝试关闭。如果打开文件(应该只有一次),文件仍然打开,因为您不关闭它。也许重新启动可以帮助或将垃圾清空。
To do the log I recommend:
为了完成我推荐的日志:
set logFilePath to (path to desktop folder as string) & "waiting.scpt.log"
doLog(logFilePath, "A new line to log!")
on doLog(pathToLogFile, logText)
try
-- open the file at logFilePath and store the file id
set logFile to open for access pathToLogFile with write permission
-- write the given text to the file, starting at the end of file to append
write logText & return to logFile starting at (get eof logFile) + 1
-- close the file asap
close access logFile
on error
-- if something went wrong, finally try to close the file!
try
close access logFile
end try
end try
end doLog
Greetings, Michael / Hamburg
你好,迈克尔/汉堡
Update after comment:
更新后评论:
I added a third parameter to the handler. To delete the content of a file you have to set the eof to 0
as follows:
我向处理程序添加了第三个参数。要删除文件的内容,必须将eof设置为0,如下所示:
doLog(logFilePath, "A new line to log!", true)
on doLog(pathToLogFile, logText, appendLog)
try
-- open the file at logFilePath and store the file id
set logFile to open for access pathToLogFile with write permission
-- set the eof to 0, if appending is not wished
if not appendLog then
set eof logFile to 0
end if
-- write the given text to the file, starting at the end of file to append
write logText & return to logFile starting at (get eof logFile) + 1
-- close the file asap
close access logFile
on error
-- if something went wrong, finally try to close the file!
try
close access logFile
end try
end try
end doLog
#1
0
CTRL-Click
an AppleScript
and check out the menu Error Handlers=>Write Error to Log
- the code shows exactly how it's done.
单击AppleScript并检查菜单错误处理程序=>写入错误日志-该代码显示了它是如何完成的。
#2
0
I don't understand the sequence of your calls. The Applescript way to access files is always the same:
我不明白你电话的顺序。获取文件的Applescript方法始终是相同的:
- Open the file and store a file id pointing to the opened file
- 打开文件并将一个文件id存储到打开的文件中。
- If needed get the index of the end of the file (needed to append text at the end)
- 如果需要获取文件末尾的索引(需要在末尾追加文本)
- Read/Write the content you want
- 读/写你想要的内容。
- Close the access to the file id
- 关闭对文件id的访问。
Your snippet looks like you try to close before opening the file. And if the opening works (should have been only once) the file remains open, because you don't close it. Maybe a restart helps or securly emptying the trash.
您的代码片段看起来像是在打开文件之前尝试关闭。如果打开文件(应该只有一次),文件仍然打开,因为您不关闭它。也许重新启动可以帮助或将垃圾清空。
To do the log I recommend:
为了完成我推荐的日志:
set logFilePath to (path to desktop folder as string) & "waiting.scpt.log"
doLog(logFilePath, "A new line to log!")
on doLog(pathToLogFile, logText)
try
-- open the file at logFilePath and store the file id
set logFile to open for access pathToLogFile with write permission
-- write the given text to the file, starting at the end of file to append
write logText & return to logFile starting at (get eof logFile) + 1
-- close the file asap
close access logFile
on error
-- if something went wrong, finally try to close the file!
try
close access logFile
end try
end try
end doLog
Greetings, Michael / Hamburg
你好,迈克尔/汉堡
Update after comment:
更新后评论:
I added a third parameter to the handler. To delete the content of a file you have to set the eof to 0
as follows:
我向处理程序添加了第三个参数。要删除文件的内容,必须将eof设置为0,如下所示:
doLog(logFilePath, "A new line to log!", true)
on doLog(pathToLogFile, logText, appendLog)
try
-- open the file at logFilePath and store the file id
set logFile to open for access pathToLogFile with write permission
-- set the eof to 0, if appending is not wished
if not appendLog then
set eof logFile to 0
end if
-- write the given text to the file, starting at the end of file to append
write logText & return to logFile starting at (get eof logFile) + 1
-- close the file asap
close access logFile
on error
-- if something went wrong, finally try to close the file!
try
close access logFile
end try
end try
end doLog