Macro to Automatically Create Open File Macros

I am now often using the Drafts (Mac) app to write / store notes. Open file links are a bit cumbersome in Drafts. I discovered that it is much easier to create a KBM open file macro, and copy the KBM macro URL into drafts. It's rather amazing how the ability to trigger KBM macros turbocharges Drafts.
I would like to be able to highlight a file in Finder or Pathfinder and trigger a macro which would automatically generate a KBM macro which would
1- have as title the filename
2- copy the macro URL from or 'or by URL' trigger dropdown list, and copy it to the clipboard
3- contain only one action: open file with default app
Is this feasible ?
thank you for your time and help

Yes, this is feasible, though it requires scripting the KM editor to do so quickly and reliably. I'm far from the best AppleScripter on this forum, and ended up relying on a couple of KM actions to get the file path and name instead of doing the whole thing in pure AppleScript, but the result does work in my testing.

Create New "Open File" Macro and Copy Macro URL.kmmacros (3.1 KB)
image

I had the macro create the new Open File macros in Drafts by default, but you can change it to whatever group you like by editing the

tell macro group "Drafts"

line in the Execute an AppleScript action and replacing "Drafts" with the name of the desired macro group.

1 Like

your macro is superb. thank you so much !

It works perfectly.

In terms of workflow, I made 2 minor changes and have 2 minor problems:

1- I usually work in Pathfinder. Although I set pathfinder as the default file browser in its preferences, the macro only works in Finder, and I could not find any alternative to Finder in the macro drop down list. Would it be complicated to have the macro work both in Finder and Pathfinder because I work in both (some functions are only available in Finder) ?

2- I realized that I will probably regret only extracting the filename and not the folder, so I extracted the FolderName from the Split Path action, and I created the last action which copies 3 lines to the clipboard: filename, folder , URL of the macro.
The first 2 lines are fine, but the 3rd line (Macro URL) is broken.
My macro URL syntax is obviously wrong and would be grateful if you could help me.

thanks again very very much !

You're welcome. Glad to hear it's working well for you! To answer your questions:

  1. It does complicate the macro a little to accommodate Path Finder as well, but not too much. You mainly need to add another AppleScript to get the Path Finder selection, something the inimitable @ccstone (who no doubt will weigh in on at some point with a superior all-AppleScript solution, as is his wont :slightly_smiling_face:) provided in a prior topic.

  2. You were close: the macro syntax is just missing an = after macro. That said, how are you acquiring the UUID for the macroUUID variable? As far as I know KM only provides tokens for getting the UUID of currently executing macros, so getting the UUID of the newly created Open File macro requires doing so through AppleScript. In the meantime, since getting the newly created macro's UUID requires AppleScript anyway, I decided to modify the last part of the script in the macro I uploaded to set the clipboard to the information you wanted, i.e. the file's name, parent folder, and the newly created macro's trigger URL:

Create New โ€œOpen Fileโ€ Macro and Copy Macro URL 1.1.kmmacros (6.2 KB)
image

While I was at it, I also added a notification at the end to visibly indicate that the macro had completed, since as it's currently written there's no other indication besides checking the KM editor or the clipboard as to whether or not it succeeded. As always, feel free to modify as necessary to suit your needs (though if you have further questions, do please upload the full macro file as well, even if you choose to only post cropped images. As I said before, it makes it much easier to diagnose any issues if we have full access to the macro as you're currently using it).

1 Like

thank you very much. I will carefully read through the post and give you an update
I will heed your advice and upload the whole macro in the future.

1 Like

it works perfectly. Absolutely great !!

1 Like

link downloads a file named "Create New .dms" which isn't recognized by KM.

Suggestions?

Thanks!

1 Like

Huh, so it does on my system too. Very odd, especially since Ronald didn't seem to have any issues with it. At any rate, all the information in the macro is still there, so you can get around this quirk easily enough by renaming the downloaded file and changing its extension to kmmacros.

1 Like

Thank you!

@BernSh @gglick

Hello,

  • the macro works fine
  • I re-downloaded the link a minute ago, after reading Bernsh's post. On my computer the link downloads a kmmacros file. Strange !!
    The only possibility is that Hazel extracted the kmmacros file. I have a gazillion Hazel rules for the Downloads folder.

Thanks for the idea. I turned Hazel off and Safari still downloaded the file with a bad extension. I have Safari set to download to Desktop.

Btw: I simply have Hazel move every file that lands in the downloads folder to the Desktop and have my gazillion Hazel rules sort things out from there. What's left is what there is to work on. This way I have one less place to lose stuff :grin:.

I've previously successful downloaded many KM macros to the Desktop without an issue. Usually I suspect the issue is with something I'm doing but that @gglick is experiencing a similar issue suggest there's something afoot beyond my personal mischievous machinations :upside_down_face:.

1 Like

thank you for your note.
If the excellent @gglick has the same problem, it is bizarre.
Statistically, what are the chances in life that purely by accident, things go right, as in my case !
It's like slipping on a banana peel and avoiding a stray bullet.

@BernSh @gglick

It's me again. I could not sleep without solving this puzzle.

With Chrome which is the browser I use, the file in the download folder is ...kmmacros

I just tried the same link with Safari, and ended up with Create New.dms

@peternlewis would perhaps be interested in knowing about this.

image

1 Like

I got the same results with Chrome AND using Firefox the file downloaded as "Create New" with ANY extension. Why now and with this link and not others?

image

historically, there are precedents. Something to do with the file download name setting protocol which would explain the difference between browsers.

Two questions remain:

  • why only this link
  • why inter user discrepancies

You mean like this: https://www.youtube.com/watch?v=aK4-F_4EzwU :wink:

hilarious !! thank you very much !

It'd be interesting to see if changing the file name to something like Test.kmmacros in case something with this particular set of characters in this particular order is triggering something.

Maybe could also be something in how that particular file got uploaded. Maybe an initial test would be to just upload it again as is.

I'm assuming you've had previous uploads downloaded without issue.

Nice catch! I also use Safari as my main browser, and can confirm that the download works fine in Brave. Not sure why @BernSh is still having trouble with Chrome and Firefox though; I can only imagine the quotation marks in the name are throwing something off, as Brave does change those to underscores. In the interest of science, here's the same macro as above with the quotation marks removed. I have a feeling this one will work fine across all browsers:

Create New Open File Macro and Copy Macro URL 1.1.kmmacros (6.0 KB)

1 Like

Yes, it seems something somewhere does not like the quotes, and even with smart quotes, Safari mangles them. I will look and see if there is anything I can do on the Share to Forum end, or whether it is a Discourse or Safari bug.

2 Likes