我有一张电子表格,我们的办公室用它来安排。每个标签代表一个月。我能够将每个选项卡导出到名为month1.csv,month2.csv ...等的文件中。我有以下代码来打开每个月文件并循环遍历它:
repeat with b from 1 to 12
tell application "Finder"
set curFileName to (sourceFolder & "month" & b & ".csv") as string
--display dialog curFileName
set workingFile to read file (curFileName)
--display dialog "File has been read"
set AppleScript's text item delimiters to {ASCII character 13}
set theContents to text items of workingFile as list
end tell
do stuff with the csv...
end repeat
脚本遍历第一个月,然后当b = 2时,我收到此错误:
Finder got an error: File file Macintosh HD:Users:lorenjz:Documents:Manpower:,month,2,.csv wasn’t found.
如何消除此错误需要做什么?
答案 0 :(得分:0)
使用以下行代替您的行。基本上,您尝试将文本一起添加以创建文件路径。但是,sourceFolder不是文本,因此您必须先将其转换为文本,然后才能向其中添加更多文本。 “b”也不是文字,因此也必须强制为文本。
一般来说,AppleScript很适合为你强制做事。它基本上试图将所有内容强制转换为行中第一个对象的类。因此,如果你将sourceFolder变成文本(它是一个文件说明符,除非你强制它),那么applescript将尝试强制之后的所有文本...意味着你真的不必强迫“b”文本,因为applescript会做对你来说......只要souceFolder当然是文本。我通常会强迫自己,但要确保它是正确的,所以我通常也会强迫自己“b”。
关键是当你一起添加文本时,一切都必须是文本。你不能只在行的末尾加上“as text”,你必须强制每个单独的项目都是文本。祝你好运。
set curFileName to (sourceFolder as text) & "month" & (b as text) & ".csv"