Getting the clipboard into variable then Javascript, then back to clipboard


I'd like to make a macro that takes a file path in the form of file:///Volumes/... with web friendly URL schemes and "translate" it in a way that the finder (or Path Finder in my case) can access it with the reveal in action.

So I found that I could convert the web friendly file path into a Reveal in friendly path with Javascript decodeURIComponent code, but I have no idea how to proceed. Here's what I got so far...

I can't find a way to make the myURL visible to the Javascript and all documentation on the subject is so cryptique, I am at a loss.

After going through the Javascript, I need to put it back into the clipboard so I can use the reveal in action or just use the clipboard to copy on a remote mac and use the Go To folder command.

Right now, I have a pseudo solution using a bunch of search clipboard, replacing with action that clean up the URL, but I'd rather have something that will work with all characters and not just the one I've searched for. I'm bound to have forgotten some for sure. I'm French speaking so most of the characters are accentuated letters like é, à, ç, etc,...

Thanks to Peter for providing my with the Shell script part via Twitter, but in 140 characters, you can't do much and that's why I'm here for your help.


PS, as a suggestion, since you do provide a way to encode URL in KM, it would be nice to have the reverse available as well. Maybe in an update?

You can access the variable from within the ShellScript (ie, from within the JavaScript within the Shell Script) like this:

osascript -l JavaScript <<END_TEXT
console.log( decodeURIComponent( "$KMVAR_myURL" ) );

But I don’t believe decodeURIComponent will do anything to help you with this.

decodeURIComponent is meant to decode an individual component of a URI, not break out the parts. What you want is the path of the URL.

What I would generally do for this class of problems (which is quite a large class of “do something via a script that isn’t directly supported by Keyboard Maestro”) is to search for what I want with the word AppleScript added in (AppleScript can easily execute shell scripts, so it functions as a search key to say “I want a scripted solution that works on a Mac”).

So search for: applescript get the path of a file url

Which leads to the pythod urllib and urlparse.urlparse.

Which leads to something like this shell script:


import urllib, urlparse, sys, os;
r = urlparse.urlparse( os.environ['KMVAR_myURL'] );
print urllib.url2pathname( r.path )

That wont keep the volume name though, its stored as r.netloc, and what you how you handle that I’m not sure. But that should get you somewhere.

Thanks Peter,

I did try to find some info on google about converting a URL or URI to POSIX path, UNIX path, and all permutation of that, with automator, applescript, javascript, OS X, etc,...

I didn't found what I was looking for exactly, it was either some online tool, or some python or other language script which left me with the how to get one variable from one language to another. I finally decided to use the new JXA in Yosemite. This means my scrip only works in Yosemite, but the bug I'm trying to workaround is a Yosemite bug which make it impossible to Path Finder to open File:// URL as it could do in Mavericks and before.

So here is my solution in KM, for the few people who would need something like this.

I had to put a copy clipboard after the script because it didn't work if I didn't. I don't understand why but hey, at least this works.

Also, I get an error message if I activate the log in the script, but the script still runs.

It would be nice if KM would support this new JXA natively also though, like the Run Apple script and Run Shell script action. It would be Run JavaScrip or JXA.

I also did a service with the same code that don't require to have KM.

Thanks for the heads up on the decoceURIComponent, without that bit of info, I wouldn't have found it.

Native JXA support is on the todo list for 7.0. It might work fine on my Mac, but it’s always hard to be certain of these things.

Hi, just noticed this while skimming through posts on using JXA Javascript with KM.

If you go back to this I think you may find that KM was actually able to write to the system clipboard from the JXA osacript, but you were (quite naturally) expecting .setTheClipboard to be a function which returns a value. If you check it in the Standard Additions Library, you'll see that its a method, which returns no value.

When you wrote:

var myClip = currentApp.setTheClipboardTo(res);

the problem will have been that as .setTheClipboard is a method rather than a function, it has the side-effect you want (writes to clipboard), but as it returns no value, myClip will have been left undefined rather than, as perhaps you expected, containing a value matching that of the clipboard.

On my system at least, the following does actually work:

osascript -l JavaScript<<JXA_END 2>/dev/null
var app = Application.currentApplication();

app.includeStandardAdditions = true;

	).replace('file://', '')
1 Like

Thanks Peter,

Your script works on my machine also. I’m new to Javascript in general so still not too clear on what the difference is between function and method. I’ll look that up to be better familiarized with those concept.