Automatically email new files in attachment fails

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi

I'm new to Hazel and currently in a trial period on the verge of buying this product. I want to use it for automatically sending invoices on iCloud to my accounting software by sending new files on a folder to an email recipient so that it uploads to the accounting package.

I created this action but it ends up in sending the email correctly with the sender/recipient/subject and content but the file itself that is the trigger of the action is not uploaded/attached.

Can someone help me out on this?

Kind regards

Wout

Script:

-- Convert Hazel's provided file path to a file reference for Mail
set theAttachment1 to (POSIX file theFile)

-- Define the email subject and body
set subject_ to "Nieuw Aankoopfactuur"
set the_content to "Er is een nieuw aankoopfactuur geüpload. Zie bijlage."

-- Create and send the email
tell application "Mail"
-- Create a new outgoing message
set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return, sender:"test@account.com"}

tell newMessage
-- Add recipient
make new to recipient at end of to recipients with properties {address:"recipient@mail.be"}

-- Attach the file
try
make new attachment with properties {file name:theAttachment1} at after the last paragraph
on error errMsg
display dialog "Error attaching file: " & errMsg
end try

-- Set visibility to false for silent sending
set visible to false

-- Send the email
send
end tell
end tell
Wout
 
Posts: 3
Joined: Wed Nov 20, 2024 6:41 am

First off, please do not post questions to the Tips forum. The title of the forum specifically states this. I've moved this topic to a more appropriate section.

Have you tried the script outside of Hazel?
Mr_Noodle
Site Admin
 
Posts: 11598
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thanks, I indeed was inside the tips section, apologies.

Yes, I just edited the script to retreive a specific file on my Downloads folder and ran in in Scripteditor manually and it does include the attachment.

Code: Select all
set theFile to "/Users/woutwerk/Downloads/creditnota_gusting_20241114_2024-0023.pdf"
set theAttachment1 to (POSIX file theFile)

set subject_ to "Nieuw Aankoopfactuur"
set the_content to "Er is een nieuw aankoopfactuur geüpload. Zie bijlage."

tell application "Mail"
   set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return, sender:"wout@xxx.be"}
   tell newMessage
      make new to recipient at end of to recipients with properties {address:"aankoop-077198edezzef8158@ccc.be"}
      make new attachment with properties {file name:theAttachment1} at after the last paragraph
      set visible to true -- Set to true if you want to preview the email
      send
   end tell
end tell
Wout
 
Posts: 3
Joined: Wed Nov 20, 2024 6:41 am

Hi. Here's a script I created to email an attachment which works. The script uses some inputs from Hazel including the email address. Importantly,

Code: Select all
on hazelProcessFile(theFile, inputAttributes)
   
   --Set variables based in input attributes from Hazel
   set recipientEmail to item 1 of inputAttributes as string --Property Manager email address
   set recipientName to item 2 of inputAttributes as string --Property Manager first name
   set theProperty to item 3 of inputAttributes as string --Property short name (eg: '21 Jump')
   set theAddress to item 4 of inputAttributes as string --Property full address (eg: '21 Jump Street, Beverley Hills')
   
   -- Strip any enclosing double quotes from theAddress
   set theAddress to ReplaceChars(theAddress, "\"", "")
   
   -- Get the file name from the file path
   set fileInfo to info for theFile
   set fileName to name of fileInfo
   
   -- Compose email body
   set emailBody to "Hi " & recipientName & "," & return & return & ¬
      "Please find attached the latest Water Rates notice for " & theAddress & "." & return & return & ¬
      "Could you please invoice the tenants for water consumption ASAP". " & ¬
      "Thanks!" & return & return & ¬
      "Lachlan"
   
   -- Compose the email in Apple Mail
   tell application "Mail"
      set newMessage to make new outgoing message with properties {subject:"Water rates for " & theProperty, content:emailBody, visible:true}
      tell newMessage
         -- Add recipient
         make new to recipient at end of to recipients with properties {address:recipientEmail}
         
         -- Attach the file
         try
            make new attachment with properties {file name:theFile} at after the last paragraph
         on error errMsg
            display dialog "There was an error attaching the file '" & fileName & "'" & return & errMsg
         end try
      end tell
      
      -- Display the new email (not sending automatically, user can review)
      activate
   end tell
   
end hazelProcessFile

-- Helper function to replace characters in a string
on ReplaceChars(theText, searchString, replaceString)
   set AppleScript's text item delimiters to searchString
   set textItems to every text item of theText
   set AppleScript's text item delimiters to replaceString
   set theText to textItems as text
   set AppleScript's text item delimiters to ""
   return theText
end ReplaceChars
Lachlan Williams
 
Posts: 18
Joined: Fri Jun 11, 2021 4:06 am

Lachlan Williams wrote:Hi. Here's a script I created to email an attachment which works. The script uses some inputs from Hazel including the email address. Importantly,

Code: Select all
on hazelProcessFile(theFile, inputAttributes)
   
   --Set variables based in input attributes from Hazel
   set recipientEmail to item 1 of inputAttributes as string --Property Manager email address
   set recipientName to item 2 of inputAttributes as string --Property Manager first name
   set theProperty to item 3 of inputAttributes as string --Property short name (eg: '21 Jump')
   set theAddress to item 4 of inputAttributes as string --Property full address (eg: '21 Jump Street, Beverley Hills')
   
   -- Strip any enclosing double quotes from theAddress
   set theAddress to ReplaceChars(theAddress, "\"", "")
   
   -- Get the file name from the file path
   set fileInfo to info for theFile
   set fileName to name of fileInfo
   
   -- Compose email body
   set emailBody to "Hi " & recipientName & "," & return & return & ¬
      "Please find attached the latest Water Rates notice for " & theAddress & "." & return & return & ¬
      "Could you please invoice the tenants for water consumption ASAP". " & ¬
      "Thanks!" & return & return & ¬
      "Lachlan"
   
   -- Compose the email in Apple Mail
   tell application "Mail"
      set newMessage to make new outgoing message with properties {subject:"Water rates for " & theProperty, content:emailBody, visible:true}
      tell newMessage
         -- Add recipient
         make new to recipient at end of to recipients with properties {address:recipientEmail}
         
         -- Attach the file
         try
            make new attachment with properties {file name:theFile} at after the last paragraph
         on error errMsg
            display dialog "There was an error attaching the file '" & fileName & "'" & return & errMsg
         end try
      end tell
      
      -- Display the new email (not sending automatically, user can review)
      activate
   end tell
   
end hazelProcessFile

-- Helper function to replace characters in a string
on ReplaceChars(theText, searchString, replaceString)
   set AppleScript's text item delimiters to searchString
   set textItems to every text item of theText
   set AppleScript's text item delimiters to replaceString
   set theText to textItems as text
   set AppleScript's text item delimiters to ""
   return theText
end ReplaceChars



Hi Lachlan

Thanks a lot for trying to help out. Whenever I do this and eg add the start 'on hazelProcesFile' it mentions it expects end for some reason. I can't figure out why he can't compile the syntax...

See: https://photos.app.goo.gl/NnamF7CsSkxL58BG7

Any ideas?
Wout
 
Posts: 3
Joined: Wed Nov 20, 2024 6:41 am


Return to Support