{"id":27950,"date":"2023-03-16T07:00:16","date_gmt":"2023-03-16T14:00:16","guid":{"rendered":"https:\/\/www.podfeet.com\/blog\/?p=27950"},"modified":"2023-03-19T17:45:56","modified_gmt":"2023-03-20T00:45:56","slug":"keyboard-maestro-quick-actions","status":"publish","type":"post","link":"https:\/\/www.podfeet.com\/blog\/2023\/03\/keyboard-maestro-quick-actions\/","title":{"rendered":"Adding Keyboard Maestro Macros to Quick Actions in Finder"},"content":{"rendered":"<p>A few weeks ago, <a href=\"https:\/\/www.podfeet.com\/blog\/2023\/02\/keyboard-maestro-click-checkbox\/\" target=\"_blank\" rel=\"noopener\">Bob Cassidy taught us some tips about how to get Keyboard Maestro<\/a> to do our bidding. He was helping me with a specific macro that needed to be triggered on a Finder item.  At the end of his explanation, he wrote:<\/p>\n<blockquote><p>\n  I think the ideal way to trigger this macro would be by right-clicking on the file and choosing from the contextual menu. In the back of my mind, I thought that Keyboard Maestro had that function, but after investigation, I don&#8217;t see a way to do it. If anyone knows how to do that, I would appreciate knowing how.\n<\/p><\/blockquote>\n<p>I went on my own quest to figure out how to do it.  I got really close to solving the problem, and then Bob came at it from a slightly different angle and we converged on the solution.<\/p>\n<p>I&#8217;ll start with the path I went down and then we&#8217;ll veer into Bob&#8217;s method to take us over the finish line. If you&#8217;d like to fast-forward a bit (or read ahead if you&#8217;re on the blog), I&#8217;ve written out the steps in an easy-to-read list at the end. But what&#8217;s the fun in that? Let&#8217;s go the long way around to get to the solution.<\/p>\n<p>So we&#8217;ve got a Keyboard Maestro macro that acts on a file or folder in the Finder. My particular macro acts on PNGs in the Finder. Instead of remembering a keystroke, or having to go up to the Keyboard Maestro Context Menu in the menu bar, we want to just right-click on the file, and choose the macro from the Quick Action menu.<\/p>\n<p>Before we get into the solution, I want to warn you that Apple uses a <em>lot<\/em> of different names for Quick Actions and it may get pretty confusing. I will try to add to the confusion by pointing out all of the names each time they change.<\/p>\n<h4>Export Macro as a Finder Quick Action<\/h4>\n<p>Inside Keyboard Maestro, if you select a Finder macro, you can select File \u2192 Export, and one of the options is Export as Finder Quick Action. That sounds like exactly what we want.  By the way, there&#8217;s an export menu available by right-clicking on a macro, but oddly that export menu does not include the Export as Finder Quick Action option; you have to do it from the File menu.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/AC37321B-08D6-4054-8A68-22CA115194D3.png\" alt=\"Keyboard Maestro Export macro as Finder Quick Action menu bar\"  title=\"AC37321B-08D6-4054-8A68-22CA115194D3.png\" width=\"460 \" height=\"429\"><figcaption style=\"text-align:center\">Export as Finder Quick Action<\/figcaption><\/figure>\n<p>The Keyboard Maestro export option calls it a Quick Action, but you are immediately asked what you want to name the saved <em>Service<\/em>.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/94992D3C-3725-4374-9C57-022623B8589D.png\" alt=\"Save Service a:s window\"  title=\"94992D3C-3725-4374-9C57-022623B8589D.png\" width=\"508 \" height=\"78\"><figcaption style=\"text-align:center\">Make Up Your Mind, Apple, Service or Quick Action?<\/figcaption><\/figure>\n<p>Once you have the Service saved, you still have to add it to the Quick Action menu. You&#8217;ll get a popup that explains that to enable this Finder Quick Action, you need to select customize from the Quick Action menu. To do this, we need to right-click on a file in the Finder, and in the menu that pops up, choose Quick Action. You&#8217;ll see a list of your existing Quick Actions, and at the bottom of the list it says &#8220;Customize&#8221;.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/70113A18-63A2-408E-90BE-0D9D56209CB9.png\" alt=\"Customize in Quick Action Menu\"  title=\"70113A18-63A2-408E-90BE-0D9D56209CB9.png\" width=\"531 \" height=\"600\"><figcaption style=\"text-align:center\">Customize in Quick Action Menu<\/figcaption><\/figure>\n<p>This customize request launches System Settings (or System Preferences if you&#8217;re pre-Ventura), to Privacy &amp; Security \u2192 Extensions. Wait, are they Services? Quick Actions? or are they Extensions??? Within the Extensions menu you&#8217;ll see Actions (but they&#8217;re not quick). Ignore those and go to the bottom of the list and choose Finder.  Within <em>Finder<\/em> we get a section that says &#8220;Select Quick Actions to show in Finder:&#8221; at the top. In this list you can finally see the Quick Actions you already have installed.  You&#8217;ll also see the one you exported from Keyboard Maestro. Check the box next to your Finder macro\/Quick Action\/Service\/Extension&#8217;s name.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/1DCD120D-70EE-46EE-8117-9D29F0862730.png\" alt=\"Keyboard Maestro macro waiting to be checked in Quick Action Finder Extensions menu\"  title=\"1DCD120D-70EE-46EE-8117-9D29F0862730.png\" width=\"454 \" height=\"600\"><figcaption style=\"text-align:center\">Finally We Find Our Macro<\/figcaption><\/figure>\n<p>Now from the Finder, if you right-click on a file and choose Quick Action from the menu, you can choose your Keyboard Maestro macro.  Whew!<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/6904C1F9-CEC2-43E8-9237-D09C0ACC6E1C.png\" alt=\"Our macro showing in the Quick Action menu\"  title=\"6904C1F9-CEC2-43E8-9237-D09C0ACC6E1C.png\" width=\"581 \" height=\"600\"><figcaption style=\"text-align:center\">Our Macro in the Quick Action Menu<\/figcaption><\/figure>\n<p>If this is the first time you&#8217;ve added a Keyboard Maestro macro to the Quick Action menu, you&#8217;ll be asked to grant the Finder permission to control Keyboard Maestro.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/F312F596-8473-464F-B733-997EB503C2E7.png\" alt=\"Finder wants access to control Keyboard Maestro app\"  title=\"F312F596-8473-464F-B733-997EB503C2E7.png\" width=\"260 \" height=\"276\"><figcaption style=\"text-align:center\">Finder Needs Permission to control Keyboard Maestro<\/figcaption><\/figure>\n<p>I&#8217;m really glad that apps can&#8217;t just suddenly start running Keyboard Maestro macros without me knowing about it!<\/p>\n<figure style=\"float: right; margin-left: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/783CF378-BA0E-4B30-AEAD-67835FBDE204.png\" alt=\"Run AppleScript error (text is in article)\"  title=\"783CF378-BA0E-4B30-AEAD-67835FBDE204.png\" width=\"260 \" height=\"292\"><figcaption style=\"text-align:center\">Danger!<\/figcaption><\/figure>\n<p>Once I had my macro in the Quick Action menu and I&#8217;d granted Finder permission to run it, I figured I was golden. I right-clicked on a PNG and chose my macro from the Services\/Quick Action menu, and I was greeted with an error:<\/p>\n<blockquote><p>\n  The action &#8220;Run AppleScript&#8221; encountered an error: &#8220;Keyboard Maestro Engine got an error: do script found no macros with a matching name (macros must be enabled, and in macro groups that are enabled and currently active).&#8221;\n<\/p><\/blockquote>\n<div style=\"background:red; color:white; overflow:hidden; padding:10px;\"> The following section was edited significantly after the developer of Keyboard Maestro (the lovely Peter Lewis) helped me better understand what &#8220;active&#8221; means in the tool. <\/div>\n<p>I knew that the macros were enabled and they were in groups that were enabled but I didn&#8217;t know what &#8220;active&#8221; meant. When I first wrote this up, the author of Keyboard Maestro, Peter Lewis, actually wrote a comment on the post and pointed me in the right direction to solve the error.<\/p>\n<p>Before we get into that part of &#8220;today I learned&#8221;, what&#8217;s this about AppleScript? I never wrote any AppleScript!  At the bottom of that error window, it had the option to Show Workflow, which opened Automator.  I never created an Automator workflow &#8230; or did I?<\/p>\n<p>Show Workflow opened the Services menu inside my user Library folder, with a workflow named after my Keyboard Maestro macro.  So my macro is a service is an extension is a Quick Action is a workflow \u2026 got it.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/891CA691-B442-45CE-9131-F95E93586CB1.png\" alt=\"Services in Finder showing workflow\"  title=\"891CA691-B442-45CE-9131-F95E93586CB1.png\" width=\"599 \" height=\"315\"><figcaption style=\"text-align:center\">There&#8217;s my Remove Alpha Workflow<\/figcaption><\/figure>\n<p>Opening this Automator Workflow revealed an AppleScript that was trying to run my Keyboard Maestro macro.  I found it ironic that Keyboard Maestro created an AppleScript. I&#8217;ve been trying to use Keyboard Maestro rather than learn AppleScript because I&#8217;ve been worried AppleScript might go away sometime soon as Apple seem to have lost interest. And yet here Keyboard Maestro goes creating an AppleScript for me!<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/F47CF030-B310-4375-B3FF-342897B050F0.png\" alt=\"AppleScript Embedded in Automatator Workflow\"  title=\"F47CF030-B310-4375-B3FF-342897B050F0.png\" width=\"600 \" height=\"576\"><figcaption style=\"text-align:center\">AppleScript Embedded in Automatator Workflow<\/figcaption><\/figure>\n<p>Since I haven&#8217;t learned AppleScript, I didn&#8217;t know why the Workflow\/AppleScript\/Service\/QuickAction was failing. I sent Bob everything I did to get to this point and asked him if he knew what was wrong.<\/p>\n<pre><code>on run {input, parameters} \n    set r to \"\"\n    set c to 0\n    repeat with theItem in input \n        set p to POSIX path of theItem \n        if c = 1 \n            then set r to r &amp; {ASCII character 10} \n        end if \n        set r to r &amp; p \n        if c &gt; 0 \n            then set r to r &amp; {ASCII character 10} \n        end if \n        set c to c + 1 \n    end repeat \n\n    tell application \"Keyboard Maestro Engine\" \n        do script \"13D4FF48-A5C9-4DA9-9AF5-7BD772124502\" with parameter r \n    end tell \n    return input \nend run\n<\/code><\/pre>\n<p>As it turns out, nothing was wrong with the AppleScript.  Keyboard Maestro&#8217;s developer Peter suggested I open the macro in question and run the Interactive Help option under the Help menu.  Learning about this the Interactive Help made me happy I had a problem with the macro because it&#8217;s crazy cool.<\/p>\n<p>With the macro that&#8217;s giving you trouble selected, Interactive Help asks you what&#8217;s wrong. The two most interesting choices are &#8220;Something unexpected is happening&#8221; and &#8220;Something expected is not happening.&#8221;  I chose the second option and it went through a series of tests on the macro. For the tests that succeed, you get a happy check, but for the ones that fail, you get a sad red x which then takes you to another step to diagnose the problem.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/Keyboard-Maestro-Interactive-Help.png\" alt=\"Keyboard Maestro Interactive Help walking through a bunch of tests\"  title=\"Keyboard Maestro Interactive Help.png\" width=\"600 \" height=\"384\"><figcaption style=\"text-align:center\">Interactive Help Stepping Through a Series of Tests<\/figcaption><\/figure>\n<p>When I ran it on my macro, it failed one of the tests and pointed me to this &#8220;active&#8221; thing. At the top of a macro, you define the triggers to make it go. What I didn&#8217;t realize is that these triggers aren&#8217;t options, they are required to make the macro active. When I originally created the macro (with Bob&#8217;s guidance), I had the trigger set to &#8220;The status menu is selected&#8221;. But if I&#8217;m calling the macro using a Quick Action, then the status menu is <em>not<\/em> selected so the Quick Action simply cannot run.<\/p>\n<p>As I explain it, this sure seems obvious, but I really hadn&#8217;t given that much thought to the importance of the triggers until the Interactive Help menu guided me to it!<\/p>\n<p>Once I fixed the trigger to make the action active for Quick Action to be allowed to run, it worked!<\/p>\n<h3>Bob&#8217;s Solution<\/h3>\n<p>Before I received help from Peter on this, I sent Bob everything I did up to and including the error about the macro being active. I&#8217;m not sure if he saw what I sent, but he came at this from a different angle and he arrived at essentially the same spot but with a solution that was a bit more simple.<\/p>\n<p>The one interesting bit in the auto-generated AppleScript code that I was using was towards the end. It says:<\/p>\n<pre><code>tell application \"Keyboard Maestro Engine\"\n    do script \"big long ASCII set of characters\" with parameter r\nend tell\n<\/code><\/pre>\n<p>This part of the script actually made sense. It&#8217;s telling AppleScript to tell Keyboard Maestro to run my script. I even knew what that big log ASCII set of characters was.  I don&#8217;t know if you remember, but last week I told you about <a href=\"https:\/\/www.podfeet.com\/blog\/2023\/03\/git-keyboard-maestro\/\" target=\"_blank\" rel=\"noopener\">Dan Thomas&#8217;s awesome Keyboard Maestro macro that lets you do version control on your macros<\/a>. I explained that the exported macros have your human-readable name, but then they&#8217;re followed by a big long ASCII set of characters that are the UUID for the macro from Keyboard Maestro.  I know that&#8217;s a lot of info I&#8217;m asking you to keep track of in your head, but I was pretty sure that UUID is the only way you can address the macro from within AppleScript.<\/p>\n<p>What I didn&#8217;t understand though was what &#8220;parameter r&#8221; was. Peter (in his comment on the blog post) explained:<\/p>\n<blockquote><p>\n  The \u201cwith parameter\u201d value is placed in the %TriggerValue% token when the macro is executed \u2013 that is how the file you have selected in the Finder is passed to the macro.\n<\/p><\/blockquote>\n<p>And one more thing from Peter. He explained that the UUID could be replaced by the macro&#8217;s name, so you could write it as <code>do script \"Macro Name\"<\/code>.  That&#8217;s pretty cool.<\/p>\n<h3>Bob&#8217;s Path to Success<\/h3>\n<p>Now let&#8217;s switch gears to Bob&#8217;s method.<\/p>\n<p>Bob&#8217;s solution was to fire up Automator, add a &#8220;Run AppleScript&#8221; action and type in the very simple lines that were in the auto-generated script, with the <code>tell Application Keyboard Maestro<\/code> and <code>do script blah blah blah<\/code>.  His script worked as well.<\/p>\n<p>But I had a question. The auto-generated AppleScript knew which UUID called the right script only because I had exported from the macro I wanted to run.  How did Bob know the UUID of the macro if he didn&#8217;t go through the export step? I knew it was a UUID because of the version control macro from Dan Thomas, but in Bob&#8217;s explanation he referred to it as a &#8220;macro script ID number designation thingy&#8221;.<\/p>\n<p>Bob&#8217;s explanation of how he knew the UUID taught me a new trick that makes this roundabout explanation worth all the time it&#8217;s taken to explain. Remember that I&#8217;ve not paid much attention to the trigger part of using Keyboard Maestro macros?<\/p>\n<p>One of the reasons I&#8217;ve not dug into triggers too far is because there are SO many options on how to trigger them. You can use a hotkey, you can have it run when a USB device is plugged in, you can have it trigger when your audio output changes or when a volume has mounted \u2013 the list goes on and on.  Bob explained that one of the many options is to trigger macros via a script. If you choose the script option, you can select AppleScript.<\/p>\n<figure style=\"float: center; margin: 10px\"><img decoding=\"async\" src=\"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/CleanShot-2023-03-15-at-18.05.11@2x.png\" alt=\"Keyboard Maestro trigger by AppleScript\"  title=\"CleanShot 2023-03-15 at 18.05.11@2x.png\" width=\"599 \" height=\"390\"><figcaption style=\"text-align:center\">Keyboard Maestro Trigger by AppleScript<\/figcaption><\/figure>\n<p>This <em>automatically<\/em> creates an AppleScript for you that says to <code>tell application Keyboard Maestro Engine<\/code> to <code>do script<\/code> and shows you the UUID (also known as the &#8220;macro script ID number designation thingy&#8221;.) This is exactly the AppleScript that you can export as a Quick Action as I described earlier.<\/p>\n<p>Bob doesn&#8217;t use the script itself, but he does use that script to find the UUID, and then put it in a very simple script:<\/p>\n<pre><code>tell application \"Keyboard Maestro Engine\"\n    do script \"Your UUID Goes Here\"\nend tell\n<\/code><\/pre>\n<h3>Concise Steps to Success<\/h3>\n<p>I walked you down these two different paths, again to demonstrate how much I&#8217;m learning by working on silly scripts like checking the Alpha checkbox in the Save As dialog box for Preview. I learned from Bob and now I learned from the developer himself, how cool is that?<\/p>\n<p>The only problem with my storytelling is that you don&#8217;t have a concise, step-by-step process to turn a Keyboard Maestro macro into a Quick Action, so let me boil it down for you here at the end.<\/p>\n<ol>\n<li>Write the Keyboard Maestro macro that works on a Finder item<\/li>\n<li>Set a trigger that you know will be active<\/li>\n<li>With the macro selected, select File \u2192 Export \u2192 Export as Finder Quick Action<\/li>\n<li>Open System Settings (or System Preferences) and navigate to Privacy &amp; Security \u2192 Extensions \u2192 Finder \u2192 and tick the checkbox next to your newly created Quick Action<\/li>\n<li>Save your Service in Automator<\/li>\n<li>To test, right-click on an appropriate file in the Finder and choose Quick Actions and select your macro\/Quick Action<\/li>\n<li>If Finder asks for permission to run Keyboard Maestro, say yes<\/li>\n<li>If anything goes wrong, check out the fabulous Interactive Help in Keyboard Maestro&#8217;s Help menu.<\/li>\n<\/ol>\n<p>I promise not to talk about Keyboard Maestro <em>every<\/em> week but I was so excited to learn even more about this amazing automation tool that I felt compelled to share. Also, if I write about it, I can always find the solution online when I forget how to do this!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago, Bob Cassidy taught us some tips about how to get Keyboard Maestro to do our bidding. He was helping me with a specific macro that needed to be triggered on a Finder item. At the end of his explanation, he wrote: I think the ideal way to trigger this macro would [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":27955,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[147],"tags":[101,1032,1830,247,4030,1034,1831],"class_list":["post-27950","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-posts","tag-applescript","tag-automator","tag-keyboard-maestro","tag-macro","tag-quick-actions","tag-services","tag-workflow"],"jetpack_featured_media_url":"https:\/\/www.podfeet.com\/blog\/wp-content\/uploads\/2023\/03\/Keyboard-Maestro-Macro-Export-no-alpha.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/27950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/comments?post=27950"}],"version-history":[{"count":8,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/27950\/revisions"}],"predecessor-version":[{"id":27990,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/posts\/27950\/revisions\/27990"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/media\/27955"}],"wp:attachment":[{"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/media?parent=27950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/categories?post=27950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.podfeet.com\/blog\/wp-json\/wp\/v2\/tags?post=27950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}