1 votes

AppleScript ajoute un nouveau nom de fichier et un nouveau chemin dans le courrier électronique uniquement lorsque l'extension des fichiers est .pdf et .doc.

J'ai un Mac qui fonctionne sous MacOS Mojave.

ce script envoie un mail à plusieurs utilisateurs lorsque quelqu'un ajoute un nouveau fichier dans mon dossier.

Comment puis-je ajouter le nom du fichier dans le courrier ? Sujet et le fichier du chemin dans le corps du mail ? Comment puis-je appliquer cette action uniquement lorsque l'extension du fichier est .pdf ou .doc ?

voici mon script :

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.

on adding folder items to this_folder after receiving added_items
try
    tell application "Finder"
        --get the name of the folder
        set the folder_name to the name of this_folder
    end tell

    -- find out how many new items have been placed in the folder
    set the item_count to the number of items in the added_items
    --create the alert string
    set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text
    if the item_count is greater than 1 then
        set alert_message to alert_message & (the item_count as text) & " new items have "

    else
        set alert_message to alert_message & "One new item has "
    end if

    set recipientName to "New file added"
    set recipientAddress to "user1@gmail.com, user2@gmail.com"
    set theSubject to "new file" & fileName
    set theContent to "¡Hello! new file added: pathName fileName"

    tell application "Mail"

        ##Create the message
        set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

        ##Set a recipient
        tell theMessage
            make new to recipient with properties {name:recipientName, address:recipientAddress}

            ##Send the Message
            send

        end tell
    end tell

end try
end adding folder items to

J'ai essayé plusieurs fois d'utiliser cet exemple, mais c'était impossible : Applescript pour retourner le nom du nouveau fichier ajouté au dossier

Merci beaucoup d'avance !

0voto

wch1zpink Points 6067

Cela fonctionne pour moi avec la dernière version de MacOS Mojave.

J'ai fait quelques ajustements pour vous et ajouté quelques éléments à votre code. Vous devrez peut-être ajuster quelques éléments pour mieux répondre à vos besoins, mais pour l'instant, je pense que le code suivant devrait vous remettre sur la bonne voie :

property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.
property name_extensions : {"pdf", "doc"}

on adding folder items to this_folder after receiving added_items
    set files_added to {}
    set file_names to {}
    tell application "Finder" to set folder_name to the name of this_folder
    repeat with i from 1 to count of added_items
        set this_Item to item i of added_items
        tell application "Finder"
            if name extension of this_Item is in name_extensions then
                set end of files_added to (this_Item & linefeed)
                set end of file_names to " " & name of this_Item & " "
            end if
        end tell
    end repeat

    -- find out how many new items have been placed in the folder
    set the item_count to count of added_items
    --create the alert string
    set alert_message to ("Folder Actions Alert:" & return & return) as Unicode text

    if the item_count is greater than 1 then
        set alert_message to alert_message & (the item_count as text) & " New items have been added"
    else
        set alert_message to alert_message & "One new item has been added"
    end if

    if files_added is {} then
        activate
        display alert alert_message giving up after dialog_timeout
        return
    else
        try
            set recipientName to ""
            set recipientAddress to "user1@gmail.com, user2@gmail.com"
            set theSubject to "new file: " & file_names
            set theContent to "¡Hello! new file added: " & linefeed & linefeed & files_added

            tell application "Mail"
                ##Create the message
                set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
                ##Set a recipient
                tell theMessage
                    make new to recipient with properties {name:recipientName, address:recipientAddress}
                    ##Send the Message
                    send
                end tell
            end tell
        end try
        activate
        display alert alert_message giving up after dialog_timeout
    end if
end adding folder items to

UPDATE :

Dans la version suivante du code mis à jour, j'ai supprimé toutes les commandes et variables "Afficher l'alerte". Cette version vous enverra un email avec les noms de tous les fichiers et leur chemin d'accès. Elle enverra également un e-mail aux adresses de votre liste de destinataires, avec le nom des fichiers et le chemin d'accès aux fichiers (uniquement si les extensions de fichiers sont .pdf ou .doc).


property myEmail : "fireDevelop@gmail.com" -- Replace With Your Email Address
property sendToEmailAddress : "user1@gmail.com , user2@gmail.com"
property name_extensions : {"pdf", "doc"}

on adding folder items to this_folder after receiving added_items
    set files_added_filtered to {}
    set file_names_filtered to {}
    set files_added to {}
    set file_names to {}

    repeat with i from 1 to count of added_items
        set this_Item to item i of added_items
        tell application "Finder"
            set end of files_added to (this_Item & linefeed)
            set end of file_names to " " & name of this_Item & " "
            if name extension of this_Item is in name_extensions then
                set end of files_added_filtered to (this_Item & linefeed)
                set end of file_names_filtered to " " & name of this_Item & " "
            end if
        end tell
    end repeat

    if files_added_filtered is {} then
        try
            set recipientName to ""
            set recipientAddress to myEmail
            set theSubject to "new file: " & file_names
            set theContent to "¡Hello! new file added: " & linefeed & linefeed & files_added

            tell application "Mail"
                set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
                tell theMessage
                    make new to recipient with properties {name:recipientName, address:recipientAddress}
                    send
                end tell
            end tell
        end try
        return
    else
        try
            set recipientName to ""
            set recipientAddress to sendToEmailAddress
            set theSubject to "new file: " & file_names_filtered
            set theContent to "¡Hello! new file added: " & linefeed & linefeed & files_added_filtered

            tell application "Mail"
                set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
                tell theMessage
                    make new to recipient with properties {name:recipientName, address:recipientAddress}
                    send
                end tell
            end tell
        end try
        try
            set recipientName to ""
            set recipientAddress to myEmail
            set theSubject to "new file: " & file_names
            set theContent to "¡Hello! new file added: " & linefeed & linefeed & files_added

            tell application "Mail"
                set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
                tell theMessage
                    make new to recipient with properties {name:recipientName, address:recipientAddress}
                    send
                end tell
            end tell
        end try
    end if
end adding folder items to

LesApples.com

LesApples est une communauté de Apple où vous pouvez résoudre vos problèmes et vos doutes. Vous pouvez consulter les questions des autres utilisateurs d'appareils Apple, poser vos propres questions ou résoudre celles des autres.

Powered by:

X