【问题标题】:Attachment not working in creating message from AppleScript附件无法从 AppleScript 创建消息
【发布时间】:2020-01-14 13:47:42
【问题描述】:

这段代码几乎可以工作。 :) 基本上我正在尝试压缩文件,然后将该电子邮件附加到新邮件中。

该过程在 automator 中启动,然后脚本运行。除了文件的实际附加之外,一切正常。

我对 AppleScript 的了解非常有限,但我试图解决这个问题。我结合了不同的代码 sn-ps 来达到这个目标。

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

【问题讨论】:

  • 在第一个tell application "Finder" block中你有set theItems to selection,然后是set theItem to (item 1 of input) as alias为什么!?您再次混合了通常不一起使用的 代码Finder 上下文中的selection 是在Finder 窗口桌面 中选择的内容。也就是说,处理调试帮助的问题应该符合How to create a Minimal, Reproducible Example。请参阅What topics can I ask about here? 下的#1。
  • BTW 使用 Script Editor 来帮助 debug 您的 code 而不是尝试 debug它在 Automator 中。
  • 您只是将文件名传递给make new attachment ...,并且您需要将完全限定的文件路径名传递给它。 (也可能作为别名。)此外,您可以使用 at after last paragraph 而不是 at after the last word of the last paragraph
  • 您的代码有很多问题。没有验证密码不是空的字符串。绝对不需要两个 tell application "Finder" blocks 并且第一个 tell application "Finder" block 应该是 code 来获取容器。您在do shell script 命令 中使用双单引号。 ./ 不是必需的,因为您已经更改了目录并且将在那里创建 .zip 文件。这只是列举一些我认为不正确的事情。
  • 感谢您的意见,就像我说 AppleScript 对我来说是新的一样,我通过构建 Automator 工作流程开始了这项工作。我希望将其保留在其中,但需要做更多的事情。

标签: applescript automator


【解决方案1】:

我已经修复了您的代码中的大部分问题。如果您将包含别名的列表传递给文件,则以下内容适用于自动机 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 属性设置为脚本创建的压缩文件的别名
  • 我清理了这个和那个以使事情更清晰。

【讨论】:

  • 哇哦!!!我昨天继续根据您的建议编写代码,并在大约一个小时前最终运行。但是,它比这更笨重!!!非常感谢!
【解决方案2】:

这是我最终想出的,虽然可行,但附件的文件处理仍然需要工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    相关资源
    最近更新 更多