Select a file in Finder and email it automatically

Adam Moisa
5 min readMay 16, 2017

--

In this tutorial, we’ll write a service for selecting a File in finder and then sending it off in an email.

Getting set up

Open Automator and choose ‘Create new Service’. Make sure the top right panel says:

Service receives selected ‘files or folders’ in ‘any application’

Step 1

On the left, search for the command: “Ask for Text” and drag it to the right side. Set the question to “Which email?”. Add the default answer as your email in case something happens. Worst case it will email to you.

Step 2

Find the next command: “Set value of Variable” and drag it under the previous command on the right. Name it whatever you want — I name it ‘Email’. We wont be accessing it by name anyway so it doesn’t matter.

Step 3

Find the final command: “Run AppleScript” and drag it last on the right. Enter this script:

on run {input, parameters} 
set emailTo to (item 1 of input as text)
set myEmail to "myemail@example.com" -- Enter your email here
tell application "Finder"
set theFile to the selection -- Get selected file in finder
set fileName to name of (info for (selection) as alias) -- Get file name for use in subject line
end tell
set currentDate to date string of (current date) -- Get current date to use in subject line
set theSubject to "File: "& fileName & "| Current date: "& currentDate
set theBody to "Hi, here you go!"
set theAddress to emailTo
set theSender to "myEmail"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
tell theNewMessage
set visibile to false
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress} -- This adds the file
try
make
new attachment with properties {file name:(theFile as alias)} at after the last paragraph
set message_attachment to 0
log "message_attachment = "& message_attachment
on error
set
message_attachment to 1
end try

delay
1 -- Delay necessary for attachment to attach
send theNewMessage
end tell
end
tell
return input
end run

All this code is pretty self explanatory if you go through it line by line and I added comments to help understand the flow. The only unintuitive parts are:

set emailTo to (item 1 of input as text)

This is how to get the variable passed from the previous step — “Set Value of Variable”. The variable is returned as a list to input so you must ask for the first item of the input list.

Another odd thing is:

{file name:(theFile as alias)}

Two things here:
- The parameter name for the attachment is ‘file name’ instead of something more intuitive like ‘file path’ but don’t get caught up.
- AppleScript prefers the file to be passed as an alias so we have to make sure to do that.

Last odd thing:

delay 1

This is necessary for the file to actually attach itself. If you don’t have this line, the email will send without attaching a file!

Step 4

Save your service by hitting cmd + s or using the file menu up top.

Congrats! You’re done. You can now select a file, right click, go to services, and select your email service you just named. A popup will come up and you can type the email you want, and click OK!

___________________

Bonus Option 1 — Keyboard shortcut

If you want to make this a keyboard shortcut (i.e. select a file then hit a key combination on the keyboard):

  • Go to System Preferences
  • Go to Keyboard > Shortcuts
  • Select Services on the left and scroll to the Files and Folders dropdown
  • Find the name of your service and on the right, it will say ‘none’
  • Click on ‘none’ and hit the keys you’d like to use as a command. I use shift + cmd + e.

Bonus Option 2 — Email multiple selected files

This one is an easy customization - all we have to do is loop through the selected files and hit the ‘make new attachment’ command with each iteration.

First, we need to get the selection as a list. So on top, change the get select to:

set theFiles to selection as list
set fileName to name of first item of theFiles

Then in the tell part of “tell application Mail” in the tell part of “tell theNewMessage”:

tell theNewMessage
set visibile to false
set sender to theSender
make new to recipient at end of to recipients with properties {address:theAddress} -- This adds the file
repeat with aFile in theFiles
make
new attachment with properties {file name:(aFile as alias)} at after the last paragraph
end repeat

delay
1 -- Delay necessary for attachment to attach
send theNewMessage
end tell

Bonus Option 3— type an arbitrary string and let the program decide the recipient

If you can’t remember all the emails of the people you might send emails to, you can create a quick dictionary-like if else sequence so you can type in a name and associate it with an email. Now, instead of typing in an email in the prompt, you can type in a string that you know will match, maybe something like a name. Here’s how I would change the top of the code block:

set theName to (item 1 of input)
if (theName = "amy") then
set emailTo to "amy@gmail.com"
else if (theName = “john”) then
set emailTo to "john@gmail.com"
else
display dialog “Could not match an email”
return
end if

You can add as many as you’d like!

Bonus Option 4— Use a popup to select a recipient

If you want a more visual way to select a recipient, perhaps from address book, check out this link: https://macgrunt.com/2012/07/21/email-file-from-finder/

Note: Be sure to use my code for attaching the file as alias and adding `delay 1`!

Bonus Option 5— Email multiple recipients

If you want to be able to email multiple recipients selected from a popup list, check out this link: https://macgrunt.com/2012/07/21/email-file-from-finder/

Note: Be sure to use my code for attaching the file as alias and adding `delay 1`!

If this helped you out, please hit that little heart down below!

My name is Adam Moisa and I build software for a variety of enterprises, from cloud security monitors to food safety/compliance platforms for large chain restaurants and patient/order management systems for large hospitals.

Some of my work can be found at moisaconsulting.com

--

--

Adam Moisa

Owner at Moisaconsulting.com. I do everything I can, as fast as I can, with as little money as possible.