[Small Tutorial]: Manage file opening behavior in KM

This will create a single application "KMOpen" that can be used as the default app for any file type to customize launching behavior.

1: Create an Applescript such as this:

on open (theFile)
	tell application "Finder"
		set theExtension to name extension of (theFile as item)
	end tell
	tell application "Keyboard Maestro Engine"
		do script ("." & theExtension) with parameter theFile
	end tell
end open

2: Save as application like so:

3: Click "get info" in the context menu for a .txt file (for example) and change "Open with" like so:

4: Click "Change all" to set KMOpen as the default app for .txt files.

5: Import this example macro. Note it starts with a "." so the file will be hidden. Google "show hidden files mac terminal" for quick steps on showing hidden files.

Now txt files should open in terminal. You can easily switch back to your previous text editor by reversing steps 3 and 4. My use case was to open .log with an lnav command. If anyone creates any useful macros inspired by or related to this tutorial, please share.

A final optional step can allow you to set custom icons for different file types using KMOpen. Follow this tutorial to find out how.

1 Like

Thanks for sharing, Matthew.

That’s a very interesting approach, and I’m sure many will find it useful.

I have been using a macro for some time with a similar purpose: Open a file with an alternate app. But my macro takes a very different approach.

It does NOT change the macOS default association for a file, so I can always open any file using its standard default app by double-clicking or pressing RETURN.

But if I want to use an alternate app, I just trigger my macro, where I have setup all of the alternate apps by file ext/type.

If you are interested, you can view/download my macro here:
Open Finder File with Alternate App

Thanks JMichaelTX,

It looks like your macro is excellent for when you have a file type that you want to launch in multiple different ways.

One unique aspect of my macro is that it will also change the way the app opens from contexts other than finder, such as Alfred or Launchbar. Though I’m sure if anyone wanted they could make scripts linking those apps to your macro.

With LaunchBar, you can “send” a file to any app you want. So no macro is needed. In fact, you can even use the LB send command from the Finder.

Technically a lot of macros are not “needed”. Sometimes its just about reducing the number of required steps.

If you want to quickly change the default opening behavior of a filetype across your system or have a central place to manage all your opening behaviors, this macro is good for that.

It also complements your macro nicely. In theory we could even merge them so that default opening behavior and alternate opening behavior can be easily switched.

If you are running Sierra, this has become easier now:

1 Like