2014-10-19 105 views
1

我幾乎不知道蘋果腳本,並希望得到您的幫助!這裏是我想要做的事:重複在變量範圍內循環的腳本

編寫一個腳本,複製和粘貼Excel單元格字,它 然後自動將文件保存爲PDF。然後腳本 自動執行Apple郵件程序,根據Excel電子表格發送一堆電子郵件(附帶 附件)。

我至今寫了一個腳本,做這一切只是我遇到麻煩腳本重複此過程下一單元格,依此類推,直到所有的細胞和電子郵件完成。以下是我迄今爲止:

tell application "Microsoft Excel" 
activate 
set empName to string value of range "A4" of active sheet 
set myVal to string value of range "P4" of active sheet 
tell application "Finder" 
    set theFile to "Macintosh HD:users:deve:desktop:C.dotx" 
    tell application "Finder" 
     open file theFile 
     set the clipboard to myVal 
     tell application "Microsoft Word" 
      activate 
      tell application "System Events" 
       tell application process "Microsoft Word" 
        keystroke "v" using command down 
        keystroke "a" using command down 
        tell application "font" - this is just so I can fix a font issue (and I don't know how to do it using applescript so I made an automator program) 
         activate 
         delay 1 
        end tell 
        tell application "Microsoft Word" 
         save as active document file name "Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" file format format PDF 
        end tell 
        tell application "Mail" 
         delay 2 
         set theMessage to make new outgoing message with properties {visible:true, subject:"Message", content:myVal} 

         tell theMessage 
          make new to recipient at end of to recipients with properties {name:empName, address:"[email protected]"} 
         end tell 
         tell theMessage 
          make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" as alias} at after the last paragraph 
          make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Work Book.pdf" as alias} at after the last paragraph 
          make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Picture File.pdf" as alias} at after the last paragraph 
          make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Lyrics.pdf" as alias} at after the last paragraph 
          make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Comparison.pdf" as alias} at after the last paragraph 
          make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Earlier Version.pdf" as alias} at after the last paragraph 

         end tell 
        end tell 
       end tell 
       end tell 
      end tell 
     end tell 
    end tell 
end tell 

我將如何能夠循環整個過程使得細胞每次一下降,從而使empNamemyVal需要從A5P5它們的值...然後A6P6一直到42?

+2

祝賀你的第一篇文章。改變標題來表達你的具體問題 - 重複一個腳本循環的變量範圍。不要用大寫字母作爲重點,它讓很多人只是跳過你的問題,好的是你已經發布了一些工作代碼,你仍然可以重構一下,因爲水平滾動條不鼓勵深入分析。那麼什麼是錯的。它有助於獲得有用的答案。歡迎來到StackOverflow Deve。 – 2014-10-19 01:16:25

回答

0

您只需將行標識設置爲變量並將您的代碼放入重複循環中,然後在每次迭代後將其增加1。這裏是一個例子如何做到這一點。 (我也能抵禦消除你的一些錯誤地嵌套告訴塊(不窩告訴其他應用程序告訴塊,如果你沒有一個理由這樣做內部塊。

property firstRow : 5 
property lastRow : 42 

set r to firstRow 
repeat until r is (lastRow + 1) 

    tell application "Microsoft Excel" 
     activate 
     set empName to string value of range ("A" & r) of active sheet 
     set myVal to string value of range ("P" & r) of active sheet 
    end tell 

    tell application "Finder" 
     set theFile to "Macintosh HD:users:deve:desktop:C.dotx" 
     open file theFile 
     set the clipboard to myVal 
    end tell 

    tell application "Microsoft Word" 
     activate 
     tell application "System Events" 
      tell application process "Microsoft Word" 
       keystroke "v" using command down 
       keystroke "a" using command down 
      end tell 
     end tell 
    end tell 

    tell application "font" -- this is just so I can fix a font issue (and I don't know how to do it using applescript so I made an automator program) 
     activate 
     delay 1 
    end tell 
    tell application "Microsoft Word" 
     save as active document file name "Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" file format format PDF 
    end tell 

    tell application "Mail" 
     delay 2 
     set theMessage to make new outgoing message with properties {visible:true, subject:"Message", content:myVal} 

     tell theMessage 
      make new to recipient at end of to recipients with properties {name:empName, address:"[email protected]"} 
     end tell 
     tell theMessage 
      make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:CL:" & empName & " Letter.pdf" as alias} at after the last paragraph 
      make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Work Book.pdf" as alias} at after the last paragraph 
      make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Picture File.pdf" as alias} at after the last paragraph 
      make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Lyrics.pdf" as alias} at after the last paragraph 
      make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Comparison.pdf" as alias} at after the last paragraph 
      make new attachment with properties {file name:"Macintosh HD:Users:Deve:Desktop:Materials for Applescript:Earlier Version.pdf" as alias} at after the last paragraph 
     end tell 
    end tell 

    set r to r + 1 
end repeat 

我沒有測試這在我的機器上,但這回答你的基本問題。發佈任何後續幫助你需要。

+0

謝謝。它現在完美。有沒有辦法得到它,所以它會自動根據已在Excel工作表中填寫的內容來自動計算出lastRow數字是什麼?謝謝你的幫助。 – Dan 2014-10-20 16:50:38

+0

當然。例如,如果該行中的單元格爲空白,則在獲取單元格值後,可以添加:如果myVal爲「」,則退出重複 – jweaks 2014-10-20 17:54:33