AppleScript 的临时文件
Temporary file of AppleScript
我想使用 AppleScript 创建一个临时文件,然后从中提取文本。
这是别人写的代码,我完全看不懂。有没有更简单的?
set AppleScript's text item delimiters to ""
set tmpfile to ((path to temporary items folder as string) & "Pashua_" & (characters 3 thru end of ((random number) as string)) as string)
set fhandle to open for access tmpfile with write permission
write (config as string) to fhandle as «class utf8»
close access fhandle
set posixtmpfile to POSIX path of tmpfile
结果
如何使用Applescript
获取临时文件夹?
使用命令:(path to temporary items folder as string)
.
必须使用“open for access”命令(不是open),否则程序不会创建临时文件,会报错。这个动作就像vim:创建一个新文件,如果你不这样做
“open for access”需要添加“with write permission”以支持写操作
你执行了多少次“打开访问”操作,你必须执行多少次“关闭访问”。就像其他语言的“打开内存&清理内存”一样
临时文件还是要删除,因为即使我执行“关闭访问”,文件还是存在的。所以“临时文件”好像不存在
结束
是的,还有一个更简单的
set posixtmpfile to POSIX path of (path to temporary items folder) & "Pashua_" & text 3 thru -1 of ((random number) as string)
set fhandle to open for access posixtmpfile with write permission
write config to fhandle as «class utf8»
close access fhandle
但是强烈建议您在没有错误处理的情况下使用read/write术语。如果发生错误,fhandle
可能会保持打开状态并可能导致后续错误。
这是一个不太简单但更可靠的版本。它在任何情况下都会关闭文件(描述符)。
set posixtmpfile to POSIX path of (path to temporary items folder) & "Pashua_" & text 3 thru -1 of ((random number) as string)
try
set fhandle to open for access posixtmpfile with write permission
write config to fhandle as «class utf8»
close access fhandle
on error
try
close access posixtmpfile
end try
end try
要获得 真实 唯一标识符,请使用 AppleScriptObjC 和 Foundation 框架的 NSUUID
class。
它创建一个格式为 01234567-89AB-CDEF-0123-456789ABCDEF
的字符串
use framework "Foundation"
set uniqueIdentifier to current application's NSUUID's UUID()'s UUIDString as text
我想使用 AppleScript 创建一个临时文件,然后从中提取文本。 这是别人写的代码,我完全看不懂。有没有更简单的?
set AppleScript's text item delimiters to ""
set tmpfile to ((path to temporary items folder as string) & "Pashua_" & (characters 3 thru end of ((random number) as string)) as string)
set fhandle to open for access tmpfile with write permission
write (config as string) to fhandle as «class utf8»
close access fhandle
set posixtmpfile to POSIX path of tmpfile
结果
如何使用
Applescript
获取临时文件夹?使用命令:
(path to temporary items folder as string)
.必须使用“open for access”命令(不是open),否则程序不会创建临时文件,会报错。这个动作就像vim:创建一个新文件,如果你不这样做
“open for access”需要添加“with write permission”以支持写操作
你执行了多少次“打开访问”操作,你必须执行多少次“关闭访问”。就像其他语言的“打开内存&清理内存”一样
临时文件还是要删除,因为即使我执行“关闭访问”,文件还是存在的。所以“临时文件”好像不存在
结束
是的,还有一个更简单的
set posixtmpfile to POSIX path of (path to temporary items folder) & "Pashua_" & text 3 thru -1 of ((random number) as string)
set fhandle to open for access posixtmpfile with write permission
write config to fhandle as «class utf8»
close access fhandle
但是强烈建议您在没有错误处理的情况下使用read/write术语。如果发生错误,fhandle
可能会保持打开状态并可能导致后续错误。
这是一个不太简单但更可靠的版本。它在任何情况下都会关闭文件(描述符)。
set posixtmpfile to POSIX path of (path to temporary items folder) & "Pashua_" & text 3 thru -1 of ((random number) as string)
try
set fhandle to open for access posixtmpfile with write permission
write config to fhandle as «class utf8»
close access fhandle
on error
try
close access posixtmpfile
end try
end try
要获得 真实 唯一标识符,请使用 AppleScriptObjC 和 Foundation 框架的 NSUUID
class。
它创建一个格式为 01234567-89AB-CDEF-0123-456789ABCDEF
use framework "Foundation"
set uniqueIdentifier to current application's NSUUID's UUID()'s UUIDString as text