Copying and saving KM actions as images in one click

I've been creating a lot of KM tutorials and wanted to share a streamline I just came up with. It consists of two parts:

  • General purpose macro for saving images New from Clipboard in Preview.app
  • BetterTouchTool gesture

New From Clipboard

This macro automates the process of switching to Preview.app, creating a new image from your clipboard and calling down the save dialog. It incorporates a couple Pause Until actions to account for UI lag.
New Image From Clipboard.kmmacros (16.5 KB)

BTT Gesture

Part II is a KM-specific BTT gesture that will select the context menu action Copy as Image and then as an additional action, press the hotkey for the New from Clipboard macro.

Side Note: I hate polluting my hotkey space so I have a copy of the New from Clipboard macro in a KM only macro group. I wish I could just trigger KM actions by name in BTT, but we gotta work with what we got!

The end result is a single action to save KM actions as image files.

This is a good idea, but I think it can be improved. There's no need to go through Preview.app when KM can create image files from the clipboard on its own (note: requires Keyboard Maestro 8):

Copy as Image, Save to File.kmmacros (10.2 KB)

###AppleScript Used to Get Macro and Action Names:

tell application "Keyboard Maestro"
	
	#Check to see if selection is a macro or macro actions
	set kmSelection to selection
	set kmSelectionClass to class of item 1 of kmSelection
	
	#If selection is macro, get macro name, end script
	if kmSelectionClass is "macro" then
		return name of item 1 of kmSelection
		
		#Otherwise, get names of all selected actions
	else
		set ActionNames to ""
		repeat with i from 1 to (length of kmSelection)
			if ActionNames is "" then
				set ActionNames to name of item i of kmSelection
			else
				set ActionNames to ActionNames & ", " & name of item i of kmSelection
				
			end if
		end repeat
	end if
end tell

By default this macro will automatically name the new image file using the selected macro name or actions, but it also includes an optional prompt that can be enabled to enter a name for the image file manually (make sure to disable the AppleScript action if you go that route).

4 Likes

@gglick et al:

Here's a better way to determine what you have selected in the KM Editor:

tell application "Keyboard Maestro"
  
  --- First Select an Action, Macro, or Macro Group
  --    in the KM Editor, then run this script
  
  set kmList to selection
  set kmClass to class of item 1 of kmList
  
  -->Based on selection, will return one of these:
  -->action, macro, macro group
  
end tell

3 Likes

Thanks, @JMichaelTX! I thought there had to be a better way to determine what was selected in the KM editor, but I couldn’t figure it out on my own. Now that you’ve shown me the way, I revised the macro in my last post to include a version of the script that takes advantage of this much better method. Thanks again for the great tip!

2 Likes