2012-07-23 80 views
0

我有我們的辦公室用於計劃的電子表格。每個標籤代表一個月。我可以每個選項卡導出到文件名爲month1.csv,month2.csv ......等等,我有以下的代碼,通過它打開每個文件月份環:applescript文件名錯誤

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

使用下面一行代替你的。基本上你正在嘗試添加文本來創建一個文件路徑。但是,sourceFolder不是文本,因此您必須先將其寫入文本,然後才能向其中添加更多文本。 「b」也不是文字,所以也必須強制文字。

一般來說,applescript對於強迫你而言是好事。它基本上試圖強制一切進入行中第一個對象的類。所以如果你把sourceFolder變成文本(除非你強制它是一個文件說明符),那麼applescript會試圖在文本之後強制一切......這意味着你真的不必強制「b」文本,因爲applescript會做對你來說......只要souceFolder是課文。我通常會自我強迫,以確保它是正確的,所以我通常也會強迫自己「b」。

問題是,當你在一起添加文本時,一切都必須是文本。您不能僅僅將「作爲文本」放在行尾,您必須將每個單獨的項目強制轉換爲文本。祝你好運。

set curFileName to (sourceFolder as text) & "month" & (b as text) & ".csv"