Вложение не работает при создании сообщения из AppleScript - PullRequest
0 голосов
/ 14 января 2020

этот код почти работает. :) В основном я пытаюсь заархивировать файл, а затем прикрепить это письмо к новому почтовому сообщению.

Процесс запускается в автоматоре, а затем запускается скрипт. Все работает, за исключением фактического присоединения файла.

Мои знания об 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

Ответы [ 2 ]

0 голосов
/ 15 января 2020

Вот то, что я в конечном итоге придумал, и это сработало, но обработка файла для вложения все еще требовала работы.

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
0 голосов
/ 15 января 2020

Я исправил большинство вещей, связанных с вашим кодом. Следующее работает в действии automator 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 вложения для псевдонима сжатого файла, создаваемого сценарием
  • Я очистил до того, чтобы прояснить ситуацию.
...