Attaching file to mail in KM

Using KM I open a new email and leave it open, now I would like to attach a file to it. I have the location of the file stored as a KM variable if possible I would like to just send a command so that the file is attached to the email.

Assuming you are using the Send Mail Message action for the new mail, there is a field to attach a file. Just put your variable into that field.


2 Likes

Hi, thank you for your response, could you show a working example. I am a little confused.

Sorry for confusing :anguished:

This is the macro from the screenshot above:

_[test] New Mail with Attachment.kmmacros (1.7 KB)

Of course, you have to change the path in the variable definition to something that exists on your computer.

1 Like

Sorry, I checked your response on my mobile and did not see the image that you posted. Many thanks.

Could i attach 2 files? If so how?
Many thanks

According to the Wiki, no. But with AppleScript it’s certainly possible.

1 Like

Hey Ali,

Look here:

Scripting Apple Mail

-Chris

1 Like

Hey Ali,

Also see this thread:

Putting File Aliases on the Clipboard

-Chris

1 Like

Hi,
When my variable looks like something like this how do I format it properly so that KM can attach the file to Mail?

Thank you.

Hey Ali,

You can't.

The content of your variable is a useless text-representation of an alias.

If you want to get from a path in a Keyboard Maestro to an AppleScript variable you'll have to do something like this:

See that I'm using a $HOME-based path (otherwise known as a tilde-based path).

** Never use absolute paths when you don't have to, because they are not portable and are easily broken.

Then I've filtered the variable to expand the tilde-path to a full path.

Now to get that path from the Keyboard Maestro variable into your Mail.app AppleScript you'll have to do something like this:

-------------------------------------------------

# Get the file path from Keyboard Maestro:
tell application "Keyboard Maestro Engine"
   set myFilePath to getvariable "filePath"
end tell

# Convert the file path to an AppleScript alias:
set fileAlias to convertPosixPathToAlias(myFilePath)

-------------------------------------------------
--» HANDLERS
-------------------------------------------------
on convertPosixPathToAlias(posixPath)
   return alias POSIX file posixPath
end convertPosixPathToAlias
-------------------------------------------------

-Chris

1 Like