Open File Within an Email or Application

Hello Keyboard Maestros,

I think I'm missing a setting that I'm hoping you can help me with. I'm trying to open an image within a new or reply email. When using a macro shown below, the Open File, Folder or Application spawns a brand new email and inserts the picture. I'd like to insert the picture in the email I am in. The same behavior happens in Messages as well. Can you let me know what I'm doing wrong or if there is a setting that I'm missing? I checked with my friend myafpelworld and his works correctly.

The action:

The result I'm getting:

I definitely appreciate the assistance.

Thanks much!
KC

I will paste the photo using these steps instead

30%20PM

1 Like

Thanks Alejandro. I definitely understand the many ways to accomplish this. However, I'm more concerned with the behavior of why it opens in a new message/email. When using the Open File action, does the same action paste into the existing/focus message or does it spawn a new message? Another KM user, on this forum, can paste directly into the mail message or Messages app without spawning a new message. They are using the same action. I'm just baffled as to why.

Thanks for your time.

KC

Hi Alejandro

That little Macro of yours is incredibly useful. I have been trying to work out a way to have a collection of images (maps showing directions to different places) in a folder that I can easily summon up on demand and paste into an email.

Somehow, I wasn't aware of the "Read File to System Clipboard" action.

But now I realise I can have a Group of Macros called for example, "Map". Then have a separate Macro, based on your example above, for each map.

I set a hotkey to "show/hide a palette" for this Group and can then just click on the palette for the map I want to paste (into an email).

All without having to set up named clipboards etc. It is a really neat solution as I can easily add more maps to this Finder folder and make a new Macro in the Group to call up and paste any new map I want.

My palette looks a bit like this:
image

2 Likes

...and each Macro, based on yours, looks like this:

Just wanted to mention this as it is something others might find useful if (like me) they hadn't figured out just how simple it was to do.

2 Likes

Great!, I purchased the Keyboard Maestro about 2 years ago. I have build so many macros in my computer that I barely do anything. Send me a message if you need help with anything. I am glad that I was able to help. :slight_smile:

Hey Mark,

It's a better practice to use the Paste action rather than the Type a Keystroke action, because Paste is designed to allow the previous actions to finish before doing its thing.

Under some circumstances simulating V can happen before the previous actions have finished and cause your macro to fail.

-Chris


Peter more clearly states what I mean in post #11 below.

Hey Folks,

Keyboard Maestro's Read a File action places the contents of the given file on the Clipboard.

In the case of images, it converts all image types (png, jpg, etc) to tiff, which is always much larger than the compressed types. For example, a jpg file of 46.3KB may produce a tiff file of 214KB (a factor of 4.6X).

The AppleScript below will place one or more file references on the Clipboard as pasteable objects that when pasted into Apple Mail will be the same file type as the source file. (Exactly as if you'd copied them in the Finder and then manually pasted into Mail.)

It will work with all file types – not just images.

------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2018/04/10 16:57
# dMod: 2018/04/10 16:57 
# Appl: AppleScriptObjC
# Task: Place a reference to one or more items on the clipboard for pasting into an app like Mail.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Place, @Reference, @Multiple, @Files, @Items, @Clipboard, @Paste
------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
------------------------------------------------------------

set filePath to POSIX path of "/Users/chris/Downloads/image01.jpg"
set filePath2 to POSIX path of "/Users/chris/Downloads/image02.jpg"

set theURL to current application's |NSURL|'s fileURLWithPath:filePath
set theURL2 to current application's |NSURL|'s fileURLWithPath:filePath2
set thePasteboard to current application's NSPasteboard's generalPasteboard()
thePasteboard's clearContents()
thePasteboard's writeObjects:{theURL, theURL2}

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

Use Keyboard Maestro's Execute an AppleScript action to run the AppleScript.


You may also want/need to force the cursor to be in the Mail message body before pasting, and this can be accomplished with AppleScript UI-Scripting:

How Do I Move Cursor to the Body of a Apple Mail Message?

-Chris

2 Likes

Ah. I hadn't realised that. So, what could be good is if Keyboard Maestro had an action that copied a specific file to the clipboard rather than reading it. I don't think it currently has that action.

And thank you for the tip about using the "Paste" Action rather than ⌘V.

If you use Mail's "Attach Files…" menu item you don't even need the clipboard:

Select Menu Item in Mail; File ⇢ Attach Files…
Pause Until All Conditions Met; Button name “Choose File” enabled
Type the ⇧⌘G Keystroke
Insert Text by Pasting; /Users/ss/Desktop/ImageToAttach.png
Type the Enter Keystroke
Pause Until All Conditions Met; Button name “Choose File” enabled
Type the Enter Keystroke

The ⌘V cannot happen before Keyboard Maestro has finished the previous action - Keyboard Maestro executes actions in sequence. It can happen before the results of the previous action are completed, but this is equally true of the Paste action. Paste is functionally implemented as:

  • Type a Keystroke: ⌘V
  • Pause Copy/Paste Delay Time (by default 0.2 seconds).

Since the “Typing” a character really means telling the system to add the character to the event queue, and the target application can read from that queue at some unknown time in the future, the Paste action has an inbuilt pause that allows time for the target application to read the ⌘V and read the clipboard and do its pasting.

A possible consequence then of using “Type a Keystroke: ⌘V” is not that the ⌘V can happen before the previous actions, but that the following actions can happen before the application reads the ⌘V from the event queue and then reads the clipboard. This is particularly important if you plan to change the clipboard immediately afterwards.

Since this macro finishes and does nothing further (unless it is ever called with an Execute a Macro action or otherwise as part of a sequence of things), and then “Type a Keystroke: ⌘V” is the last action, it makes no difference whether you use the Paste action. Though it is generally better simply for consistency and clarity, and in case Keyboard Maestro can ever do anything clever like pausing until the clipboard is actually read.

Hi Peter

As you have joined this thread can I ask you a quick question related to the above?

There is a "Copy File" Action which copies a specific file in the Finder to another location in the Finder.
There is a "Read File to Clipboard" Action which copies the contents of a file to the clipboard.
But there is not a "Copy File to Clipboard" Action.

I was wondering if there is a technical reason this does not exist?

What I liked about the really simple Macro that Alejandro suggested was that it worked in any app, not just Mail. The only problem being that as Chris pointed out, it pastes a TIFF rather than the original file format. I worked around that by keeping the file size small and modern email seems to be able to cope happily with larger file sizes anyway.

Thanks for making such a great app. I am sure I have told you before how essential it is in my workflow and how I use it everyday. But no harm telling you again.

1 Like

There is no particular reason Keyboard Maestro does not have an action to create a clipboard entry from a file URL. But there are lots of actions Keyboard Maestro does not have - it cannot create clipboards for many different flavors of data.

It is certainly possible I might add that as an action, although really it needs to have a reasonably high usage since it can already be done with a script fairly easily.

1 Like

The reason it's spawning a new message is all you are telling it is to do it in Mail. No specifics about in the window, or where the cursor is, that kind of thing. Computers are very literal and you have to be very specific with any automation command.

Blockquote

But it looks like KM9 does now have this ability.

There is the new action "Set System Clipboard to Image" which copies an image to the clipboard from a path location and keeps its original file format (for example, .jpg).

Thank you very much Peter for this and for all the other great new things to play with in KM9.

1 Like

There is also the explicit Set Clipboard to File Reference action.

3 Likes