附件无法从AppleScript创建消息

问题描述 投票:0回答:2

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

该过程在automator中启动,然后脚本运行。除实际附加文件外,其他所有操作均有效。

我对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
applescript automator
2个回答
0
投票

我已经修正了您的代码中大部分与之无关的东西。如果您将包含别名的列表传递给文件,则以下内容将在自动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属性设置为脚本创建的压缩文件的别名
  • 我整理了这个,使情况更清楚。

0
投票

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

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 protected]" -- 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
© www.soinside.com 2019 - 2024. All rights reserved.