0

我有一个我们办公室用来安排日程的电子表格。每个选项卡代表一个月。我可以将每个选项卡导出到名为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.

我需要做什么来消除这个错误?

4

1 回答 1

0

Use the following line instead of yours. Basically you are trying to add text together to create a file path. However sourceFolder is not text so you must first make that into text before you can add more text to it. "b" is also not text so that has to be coerced to text too.

In general applescript is good about coercing things for you. It basically tries to coerce everything into the class of the first object in the line. So if you make sourceFolder into text (it's a file specifier unless you coerce it) then applescript will try to coerce everything after that to text... meaning that you really wouldn't have to coerce "b" to text because applescript will do that for you... as long as souceFolder is text of course. I generally do the coercing myself though to make sure it is correct so I would generally also coerce "b" myself.

The point is that when you add text together everything must be text. You can't just put "as text" at the end of the line, you must coerce every individual item to text. Good luck.

set curFileName to (sourceFolder as text) & "month" & (b as text) & ".csv"
于 2012-07-23T08:46:09.657 回答