gpt4 book ai didi

error-handling - 在Applescript中创建错误文本文件

转载 作者:行者123 更新时间:2023-12-03 08:53:58 25 4
gpt4 key购买 nike

这是一个将一组文件重命名为在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

我真的不知道如何编码,只是依靠互联网上的帖子来尝试调整它们。因此,如果有人可以帮助我..那将真的很棒!

最佳答案

在这里,这显示了如何在上面的代码中添加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

关于error-handling - 在Applescript中创建错误文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32364487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com