附件在从 AppleScript 创建消息时不起作用

Attachment not working in creating message from AppleScript

此代码几乎可以正常工作。 :) 基本上,我试图压缩一个文件,然后将该电子邮件附加到新的邮件消息中。

进程在自动程序中启动,然后脚本运行。除了实际附加文件外,一切正常。

我对 AppleScript 的了解非常有限,但我正在努力解决这个问题。我结合了不同的代码片段来实现这一目标。

on run {input, parameters}

    set display_text to "Please enter your password:"
    repeat
        considering case
            set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
            set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
            if (final_pass = init_pass) then
                exit repeat
            else
                set display_text to "Mismatching passwords, please try again"
            end if
        end considering
    end repeat

    tell application "Finder"
        set theItems to selection
        set theItem to (item 1 of input) as alias
        set itemPath to quoted form of POSIX path of theItem
        set fileName to name of theItem
        set theFolder to POSIX path of (container of theItem as alias)
        set zipFile to quoted form of (fileName & ".zip")
        do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & final_pass & "' " & zipFile & " ./'" & fileName & "'"
    end tell
    tell application "Finder"
        #I believe this is where the problem is, I am trying to use the variables from above in order to attach the file in mail.
        set folderPath to theFolder
        set theFile to zipFile
        set fileName to zipFile
    end tell

    set theSubject to fileName
    set theBody to "Hello sir. Here is my " & fileName
    set theAddress to "Some Email"
    set theAttachment to theFile
    set theSender to "Some Sender"

    tell application "Mail"
        set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
        tell theNewMessage
            set visibile to true
            set sender to theSender
            make new to recipient at end of to recipients with properties {address:theAddress}
            try
                make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph
                set message_attachment to 0
            on error errmess -- oops
                log errmess -- log the error
                set message_attachment to 1
            end try
            log "message_attachment = " & message_attachment
            #send
        end tell
    end tell


    return input
end run

我已经修复了您的代码中的大部分问题。如果您将包含别名的列表传递给文件,以下内容将在自动程序 Run AppleScript 操作中起作用:

on run {input, parameters}

    set display_text to "Please enter your password:"
    repeat
        considering case
            set init_pass to text returned of (display dialog display_text default answer "" with hidden answer)
            set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer)
            if (final_pass = init_pass) then
                exit repeat
            else
                set display_text to "Mismatching passwords, please try again"
            end if
        end considering
    end repeat

    tell application "System Events"
        set selected_file to item 1 of input
        set selected_file_name to name of selected_file
        set containing_folder_path to POSIX path of (container of selected_file) & "/"
        set zip_file_path to containing_folder_path & selected_file_name & ".zip"
        do shell script "cd " & quoted form of containing_folder_path & "; zip -x .DS_Store -r0 -P " & quoted form of final_pass & " " & quoted form of zip_file_path & " " & quoted form of selected_file_name
        set zip_file_alias to file zip_file_path as alias
    end tell

    set theSubject to selected_file_name
    set theBody to "Hello sir. Here is my " & selected_file_name
    set theAddress to "Some Email"
    set theSender to "Some Sender"

    tell application "Mail"
        set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true}
        tell theNewMessage
            set visibile to true
            set sender to theSender
            make new to recipient at end of to recipients with properties {address:theAddress}
            try
                make new attachment with properties {file name:zip_file_alias} at end of attachments
            on error errmess -- oops
                log errmess -- log the error
            end try
            log "message attachment count = " & (count of attachments)
            #send
        end tell
    end tell

    return input
end run

亮点:

  • 我从使用 Finder 切换到使用系统事件。除非绝对需要,否则请避免使用 Finder(这是一个非常繁忙的应用程序)
  • 我将附件的 name 属性 设置为脚本创建的压缩文件的别名
  • 为了让事情更清楚,我清理了这个那个。

这是我最终想出的办法,它起作用了,但附件的文件处理仍然需要改进。

on run {input, parameters}


    tell application "Finder"
        set theItem to (item 1 of input) as alias
        set theItemCopy to theItem
        set itemPath to quoted form of POSIX path of theItem
        set fileName to name of theItem
        set theFolder to POSIX path of (container of theItem as alias)
        set zipFile to quoted form of (fileName & ".zip")
        set setPass to "Password" #test password        
        do shell script "cd '" & theFolder & "'; zip -x .DS_Store -r0 -P '" & setPass & "' " & zipFile & " ./'" & fileName & "'"
    end tell


    tell application "Mail"
        set fileLocation to (fileName & ".zip")
        set theSubject to "Subject" -- the subject
        set theContent to "Attached are the documents from the last session." -- the content
        set theAddress to "email@email.com" -- the receiver 

        set theAttachmentFile to "Macintosh HD:Users:dave:desktop:" & fileLocation -- the attachment path

        set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
        delay 1
        tell msg to make new to recipient at end of every to recipient with properties {address:theAddress}
        tell msg to make new attachment with properties {file name:theAttachmentFile as alias}


    end tell

    return input
end run