Applescript为文件夹中的文件重复完整的脚本

Applescript为文件夹中的文件重复完整的脚本,applescript,repeat,Applescript,Repeat,我现在已经设法将所有这些代码整合在一起,只需要最后一步就可以了。任何帮助都将不胜感激 我已经设置了这个脚本,在一个文件夹中打开一个.xlsx文件,更改日期,然后将其保存到另一个文件夹中。然后,它通过查找客户机代码(在excel文件中找到)来创建一封邮件,随后在Database.xlsx文件中查找此代码,并返回客户机的电子邮件地址,并将其添加到邮件中的“收件人”字段。然后,它将新创建的PDF附加到此邮件,我只需单击并发送即可 脚本在第一个.xlsx文件打开后停止,这样我就可以在创建PDF和邮件之前

我现在已经设法将所有这些代码整合在一起,只需要最后一步就可以了。任何帮助都将不胜感激

我已经设置了这个脚本,在一个文件夹中打开一个.xlsx文件,更改日期,然后将其保存到另一个文件夹中。然后,它通过查找客户机代码(在excel文件中找到)来创建一封邮件,随后在Database.xlsx文件中查找此代码,并返回客户机的电子邮件地址,并将其添加到邮件中的“收件人”字段。然后,它将新创建的PDF附加到此邮件,我只需单击并发送即可

脚本在第一个.xlsx文件打开后停止,这样我就可以在创建PDF和邮件之前检查详细信息是否正确

我的问题是:如何让这个过程对初始文件夹中的每个文件重复?我尝试过重复功能,但没有效果

任何帮助都将不胜感激

多谢各位

--Complete script for updating invoice, saving (as PDF too in seperate folder) and e-mailing invoices

--Select the first file in a folder and then repeat for the next

set theFolder to POSIX path of (choose folder with prompt "Choose Folder containing .xlsx invoices")
set theFolderList to list folder theFolder without invisibles

repeat with x from 1 to count of theFolderList
    set theFile to theFolder & item x of theFolderList
    set theNewFile to theFolder & theFile
    tell application "Microsoft Excel"
        activate
        open theFile
        set ActiveClientCode to value of range ("B1")
    end tell


--Change date of one cell to date of next month

tell application "Microsoft Excel"
    activate
    open "/Users/pienaar0/Documents/AdminAssist/" & ActiveClientCode & ".xlsx"
    set d to value of cell ("A1")
    set d to my MonthAdd(d)
    set value of cell ("A1") to d
end tell

on MonthAdd(d)
    set m to ((month of d as integer) + 1)
    set ddd to day of d
    if m > 12 then
        set m to m - 12
        set year of d to (year of d) + 1
    end if
    if {m} is in {4, 6, 9, 11} and ddd = 31 then --AppleScript treats "Apr 31" as May 1,
        set day of d to 30
    end if
    set month of d to m
    if m = 2 and month of d as integer = 3 then --AppleScript treats "Feb 31" as Mar 3,
        set day of d to 1 -- Mar 1
        set d to d - (1 * days) -- last day of Feb
    end if
    return d
end MonthAdd

property dialog_timeout : 36000
display dialog "Make sure the invoice is correct before clicking OK" buttons {"OK"} giving up after dialog_timeout
set the user_choice to the button returned of the result


--Save document and PDF
tell application "Microsoft Excel"
    save active workbook
    save active workbook in "Macintosh HD:Users:pienaar0:Documents:AdminAssistPDF:" & ActiveClientCode & ".pdf" as PDF file format


end tell

--Find e-mail address, and Name in Database (Check filepath and ranges)

tell application "Microsoft Excel"
    open "Users/pienaar0/Documents/Database.xlsx"
    set searchRange to range ("D2:D5")
    set foundRange to find searchRange what ActiveClientCode with match case
    set fRow to first row index of foundRange
    set ClientEmail to value of range ("C" & fRow as text)
    set ClientFirstname to value of range ("A" & fRow as text)
    (* do something with the foundRange *)
end tell

--Create e-mail

tell application "Mail"
    set theMessage to make new outgoing message with properties {visible:true, subject:"Your monthly invoice", content:"Dear " & ClientFirstname & ",

I trust this mail finds you well?

Please find attached your monthly invoice for your immediate consideration.

Regards,

AdminAssist

"}
    set message signature of theMessage to signature "Replies & Forwards"
    delay 1
    tell content of theMessage
        make new attachment with properties {file name:"/Users/pienaar0/Documents/AdminAssist/PDF/" & ActiveClientCode & " Sheet1.pdf"}
        tell theMessage
            make new to recipient at end of to recipients with properties {address:ClientEmail}
        end tell
    end tell
end tell
end repeat

您需要将处理程序移到重复块之外:

property dialog_timeout : 36000
--Complete script for updating invoice, saving (as PDF too in seperate folder) and e-mailing invoices
--Select the first file in a folder and then repeat for the next

set theFolder to POSIX path of (choose folder with prompt "Choose Folder containing .xlsx invoices")
tell application "System Events" to set theFolderList to name of every file of folder theFolder whose visible is true

repeat with x from 1 to count of theFolderList
    set theFile to theFolder & item x of theFolderList
    set theNewFile to theFolder & theFile
    tell application "Microsoft Excel"
        activate
        open theFile
        set ActiveClientCode to value of range ("B1")
    end tell

    --Change date of one cell to date of next month
    tell application "Microsoft Excel"
        activate
        open "/Users/pienaar0/Documents/AdminAssist/" & ActiveClientCode & ".xlsx"
        set d to value of cell ("A1")
        set d to my MonthAdd(d)
        set value of cell ("A1") to d
    end tell

    display dialog "Make sure the invoice is correct before clicking OK" buttons {"OK"} giving up after dialog_timeout
    set the user_choice to the button returned of the result

    --Save document and PDF
    tell application "Microsoft Excel"
        save active workbook
        save active workbook in "Macintosh HD:Users:pienaar0:Documents:AdminAssistPDF:" & ActiveClientCode & ".pdf" as PDF file format
    end tell

    --Find e-mail address, and Name in Database (Check filepath and ranges)

    tell application "Microsoft Excel"
        open "Users/pienaar0/Documents/Database.xlsx"
        set searchRange to range ("D2:D5")
        set foundRange to find searchRange what ActiveClientCode with match case
        set fRow to first row index of foundRange
        set ClientEmail to value of range ("C" & fRow as text)
        set ClientFirstname to value of range ("A" & fRow as text)
        (* do something with the foundRange *)
    end tell

    --Create e-mail

    tell application "Mail"
        set theMessage to make new outgoing message with properties {visible:true, subject:"Your monthly invoice", content:"Dear " & ClientFirstname & ",

I trust this mail finds you well?

Please find attached your monthly invoice for your immediate consideration.

Regards,

AdminAssist

"}
        set message signature of theMessage to signature "Replies & Forwards"
        delay 1
        tell content of theMessage
            make new attachment with properties {file name:"/Users/pienaar0/Documents/AdminAssist/PDF/" & ActiveClientCode & " Sheet1.pdf"}
            tell theMessage
                make new to recipient at end of to recipients with properties {address:ClientEmail}
            end tell
        end tell
    end tell
end repeat


on MonthAdd(d)
    set m to ((month of d as integer) + 1)
    set ddd to day of d
    if m > 12 then
        set m to m - 12
        set year of d to (year of d) + 1
    end if
    if {m} is in {4, 6, 9, 11} and ddd = 31 then --AppleScript treats "Apr 31" as May 1,
        set day of d to 30
    end if
    set month of d to m
    if m = 2 and month of d as integer = 3 then --AppleScript treats "Feb 31" as Mar 3,
        set day of d to 1 -- Mar 1
        set d to d - (1 * days) -- last day of Feb
    end if
    return d
end MonthAdd

哦,是的,谢谢你!我会试试这个,但那肯定是答案。非常感谢你!