无法打开要用 Apple 脚本写入的文本文件
Cannot open text file to write to with Apple script
我正在尝试创建一个记录我的计算机何时关闭和打开的日志。为此,我在启动时为 运行 编写了一个脚本,该脚本将写入一个文本文件,但它告诉我我没有写入该文件的权限。
使用 print 语句我确定 try 块在第一行之后终止。
writeTextToFile(getTimeInHoursAndMinutes(), "Users/labspecialist/Desktop/system_log")
on writeTextToFile(theText, theFile)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to (open for access file theFile with write permission)
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
on getTimeInHoursAndMinutes()
-- Get the "hour"
set timeStr to time string of (current date)
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "AM or PM"
set Pos to offset of " " in timeStr
set theSfx to characters (Pos + 1) through end of timeStr as string
return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes
我希望当我 运行 这得到 true 的输出并且我的文件包含当前时间的新行。然而,它目前 returns false 没有写入文件。
问题是您的脚本在 open for access
行中使用了 file
说明符,它应该使用 POSIX file
说明符(因为您正在为命令提供 POSIX路径)。它应该是这样的:
writeTextToFile(getTimeInHoursAndMinutes(), "/Users/labspecialist/Desktop/system_log")
on writeTextToFile(theText, theFile)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to (open for access POSIX file theFile with write permission)
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error errstr
display dialog errstr
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
P.s。是的,您确实应该 在文件路径中使用该开始斜杠,尽管它似乎无论如何都可以工作...
我正在尝试创建一个记录我的计算机何时关闭和打开的日志。为此,我在启动时为 运行 编写了一个脚本,该脚本将写入一个文本文件,但它告诉我我没有写入该文件的权限。
使用 print 语句我确定 try 块在第一行之后终止。
writeTextToFile(getTimeInHoursAndMinutes(), "Users/labspecialist/Desktop/system_log")
on writeTextToFile(theText, theFile)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to (open for access file theFile with write permission)
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
on getTimeInHoursAndMinutes()
-- Get the "hour"
set timeStr to time string of (current date)
set Pos to offset of ":" in timeStr
set theHour to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
-- Get the "minute"
set Pos to offset of ":" in timeStr
set theMin to characters 1 thru (Pos - 1) of timeStr as string
set timeStr to characters (Pos + 1) through end of timeStr as string
--Get "AM or PM"
set Pos to offset of " " in timeStr
set theSfx to characters (Pos + 1) through end of timeStr as string
return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes
我希望当我 运行 这得到 true 的输出并且我的文件包含当前时间的新行。然而,它目前 returns false 没有写入文件。
问题是您的脚本在 open for access
行中使用了 file
说明符,它应该使用 POSIX file
说明符(因为您正在为命令提供 POSIX路径)。它应该是这样的:
writeTextToFile(getTimeInHoursAndMinutes(), "/Users/labspecialist/Desktop/system_log")
on writeTextToFile(theText, theFile)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to (open for access POSIX file theFile with write permission)
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error errstr
display dialog errstr
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
P.s。是的,您确实应该 在文件路径中使用该开始斜杠,尽管它似乎无论如何都可以工作...