Possible bug: no variable input for shell scripts


this is the behavior I’d expect:

Macro: Execute Shell Script
-> With Input from Variable: myvar1

Execute Text Script:
echo hi $@

Result: hi YOU

this doesnt work at all. I can acces the variable via $KMVAR_ but this is not the same.

It's stdin.

This works:

Or, with echo, for example:

[test] Shell with stdin.kmmacros (4.9 KB)


@Tom, I've tried following your examples, but I'm so ignorant about shell scripts I can't get this to work. What am I doing wrong?

I want to pass a text parameter to this script:

/usr/bin/perl -MMac::InternetConfig -le 'print (GetICHelper "$urlType")' \
| sed -E 's!^.{4}!!'

When I run this action, I get no results.
If I just replace $urlType with "applescript", it works fine.

TIA for your help.

Hey Jim,

You're trying to mix and match Shell and Perl variables, and it's not quite that easy.

Try this:

Execute a Shell Script.kmactions (846 B)

{ read urlType; }
eval "/usr/bin/perl -MMac::InternetConfig -le 'print (GetICHelper \"$urlType\")' | sed -E 's!^.{4}!!'"


1 Like

Thanks Chris. I figured I was doing something stupid like that. But maybe this will help others as well. I think I’ll add it to the KM Wiki.

Just to mention it:

If it’s about getting the default handler for an URL scheme you can also get it from Launch Services via Swift:

Get Default Handler for URL Scheme.kmmacros (2.4 KB)

1 Like

PS, @JMichaelTX :

I just found out there is a similar function LSCopyDefaultRoleHandlerForContentType that lets you get the default handler for a given Content Type (aka UTI).

It works very smililar to the above URL-handler function:

Get Default Handler for Content Type.kmmacros (2.5 KB)

Examples for Content Types:

com.apple.applescript.script → .scpt
com.apple.applescript.script-bundle → .scptd
com.apple.applescript.text → .applescript
1 Like

Another follow-up:

Here is a compiled version of the last Swift script (runs faster).

getDefaultEditorForContentType.zip (20.8 KB)

->->-> Put it somewhere in your PATH. <-<-<-

This version now also takes multiple arguments; example in the Terminal:

Or with KM:


1 Like

OK, I’m beginning to see some usefulness for all that :wink:

So, here another version.

It allows you to enter an Extension (txt, scpt, etc.) and the script will tell you the Content Type and the Default Editor for that.

As with the previous script you can enter multiple extensions.

For example in the Terminal:

Or with KM:


Here the compiled script:

getDefaultEditorForExtension.zip (21.3 KB)

--> --> Put the file somewhere in your PATH <-- <--

The source:

import Foundation
let arguments = Array(CommandLine.arguments.dropFirst())
for arg in arguments {
	let ext = arg as CFString
	let contentType = UTTypeCreatePreferredIdentifierForTag(
			kUTTagClassFilenameExtension, ext, nil)?.takeRetainedValue()
	if let myApp = LSCopyDefaultRoleHandlerForContentType(contentType!, LSRolesMask.editor) {
			let bundleID = Unmanaged.fromOpaque(myApp.toOpaque()).takeUnretainedValue() as CFString
			print ("\(bundleID) <-- \(contentType!) <-- \(ext)")
	} else {
			print ("[No default handler]  <-- \(contentType!) <-- \(ext)")

###Please note:

Since one Extension can have associations with more than one Content Type (there is something like a Content Type Tree), this script does not do exactly the same thing as the above script where you have to enter the Content Type.

###PS (2017-12-06):

Since I’ve seen that this post got some likes:

There is a more complete version of this macro in the Macro Library of the forum:

It combines the extension query with the UTI query and contains both scripts in one download.


Tom, many thanks for sharing all of your Swift solutions! :+1:

They all look very interesting and useful. I just need time to digest it all. :wink:

ASObjC Script to Get Default App

Just for completeness, and for those interested in ASObjC, here’s a script written by @ShaneStanley, and enhanced by @ionah, posted in the Script Debugger 6 Forum:

Not so difficult as it looks. The last two posts have download links for compiled Swift binaries. So just throw them into your path and find out if it serves for you. (In the Terminal or via KM.)

1 Like