在Applescript

时间:2015-09-02 22:56:44

标签: error-handling applescript

这是一个脚本,它将一组文件重命名为在excel中创建的新文件名。它的工作原理很棒!但问题是如果脚本结束的文件组中不存在文件并产生错误..

property sourceFolder : "Macintosh HD:Users:COMP1:Desktop:folder1" -- adjust as appropriate 

set filenameList to read file "Macintosh HD:Users:COMP1:Desktop:renamethis.txt" using delimiter {return}

set {oldDelims, text item delimiters} to {text item delimiters, tab}


tell application "Finder"
    repeat with aFile in filenameList
        set codified_name to (sourceFolder & ":" & (text item 1 of aFile)) as string
        set real_name to text item 2 of aFile
        set name of file codified_name to real_name
    end repeat
end tell

我四处寻求帮助,他们给了我这个代码补充。问题是我不知道如何以及在何处添加此代码以及如何定义它。我希望能够创建一个包含未重命名的文件的.txt文件,而不是停止整个脚本。

try
   set name of file codified_name to real_name
on error error_string number error_number
   write error_string to log_file_handle starting at eof
end

我真的不知道如何编码,只是依赖互联网上的帖子并尝试调整它们。所以,如果有人能帮助我......那真是太棒了!

1 个答案:

答案 0 :(得分:0)

这里显示了如何在上面的代码中添加try块。您还需要更多代码来写入错误日志文件。应该足够了:

property sourceFolder : "Macintosh HD:Users:COMP1:Desktop:folder1" -- adjust as appropriate 
set log_file_path to "Macintosh HD:Users:COMP1:Desktop:myErrorLogFile.txt"
set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}
try
    set filenameList to read file "Macintosh HD:Users:COMP1:Desktop:renamethis.txt" using delimiter {return}
    tell application "Finder"
        repeat with aFile in filenameList
            set codified_name to (sourceFolder & ":" & (text item 1 of aFile)) as string
            set real_name to text item 2 of aFile
            set name of file codified_name to real_name
        end repeat
    end tell
    set AppleScript's text item delimiters to oldDelims
on error error_string number error_number
    set AppleScript's text item delimiters to oldDelims
    set log_file_handle to (open for access file log_file_path with write permission)
    write ((current date) as string) & tab & error_string to log_file_handle starting at eof
    close access log_file_handle
end try