重复在variables范围内循环的脚本

我几乎不知道苹果脚本,并会感谢您的帮助! 这是我正在做的事情:

编写一个脚本,将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:"XXXX@gmail.com"} 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 

我将如何能够循环整个过程,使得单元格每次向下移动一次,以便empNamemyValA5P5取值…然后A6P6一直到42?

您只需要将行标识设置为一个variables,并将您的代码放在重复循环中,然后在每次迭代后将其增加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:"XXXX@gmail.com"} 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 

我没有在我的机器上testing这个,但是这回答了你的基本问题。 发布任何后续帮助您的需要。