AppleScript的临时文件

时间:2018-08-09 07:15:10

标签: applescript

我想使用AppleScript创建一个临时文件,然后从中提取文本。这是其他人编写的代码,但我一点也不明白。有更简单的吗?

function doAddGoogleGroupMember(){
    var user = "i.s.e@example.com";
    var groupEmail = "g-s@example.com";
    var member = {
      email: user,
      role: "MEMBER"
      };
    try {
    member = AdminDirectory.Members.insert(member, groupEmail);
    Logger.log('User %s added as a %s of group %s.', user, role, groupEmail);
    }
    catch(e) {
    if (e.message == "Member already exists.") {
      Logger.log('User %s is already in the group %s.', user, groupEmail);
    }
    }
}

1 个答案:

答案 0 :(得分:1)

是的,有一个更简单的

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

强烈劝阻,不要使用没有错误处理的读/写术语。如果发生错误,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

要获取 real 唯一标识符,请使用AppleScriptObjC和Foundation框架的NSUUID类。
它创建格式为01234567-89AB-CDEF-0123-456789ABCDEF

的字符串
use framework "Foundation"
set uniqueIdentifier to current application's NSUUID's UUID()'s UUIDString as text