[Feature Request] Go to Macro in Execute a Macro

Hey Folks,

Courtesy of Shane Stanley.

I'll get back again as soon as I find out how to put the KM-Action and KM-Macro data-types back on the Clipboard.

-Chris

---------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/06/03 22:05
# dMod: 2016/06/03 22:33
# Appl: Keyboard Maestro
# Task: Extract Keyboard Maestro action or macro plist from the clipboard.
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Extract, @Keyboard_Maestro, @Plist
---------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
---------------------------------------------------------------------------------

set kmPlistStr to missing value
set systemPasteBoard to current application's NSPasteboard's generalPasteboard()

if (systemPasteBoard's canReadItemWithDataConformingToTypes:{"com.stairways.keyboardmaestro.actionarray"}) as boolean then
  # Copied Action or Actions
  set kmPlistStr to systemPasteBoard's stringForType:"com.stairways.keyboardmaestro.actionarray"
else if (systemPasteBoard's canReadItemWithDataConformingToTypes:{"com.stairways.keyboardmaestro.macrosarray"}) as boolean then
  # Copied Macro or Macros
  set kmPlistStr to systemPasteBoard's stringForType:"com.stairways.keyboardmaestro.macrosarray"
end if

if kmPlistStr ≠ missing value then
  return kmPlistStr as text
else
  beep
end if

---------------------------------------------------------------------------------

Hey Folks,

Again courtesy of Shane Stanley.

This script will put a Keyboard Maestro Action on the Clipboard ready for pasting into a macro.

The same script with the alternate data-type can be used to put a Keyboard Maestro Macro on the Clipboard ready for pasting.

-Chris

---------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/06/03 22:45
# dMod: 2016/06/03 22:56
# Appl: Keyboard Maestro
# Task: Put Action or Macro Plist data on the clipboard with appropriate data-type.
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Plist, @ASObjC
---------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
---------------------------------------------------------------------------------

# Action data-type 
set kmPasteboardDataType to "com.stairways.keyboardmaestro.actionarray"

# Macro data-type 
# set kmPasteboardDataType to "com.stairways.keyboardmaestro.macrosarray"


set kmPlistData to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<array>
  <dict>
    <key>MacroActionType</key>
    <string>Notification</string>
    <key>SoundName</key>
    <string>Tink</string>
    <key>Subtitle</key>
    <string>EUREKA!  IT WORKS!</string>
    <key>Text</key>
    <string></string>
    <key>Title</key>
    <string>%ExecutingMacro%</string>
  </dict>
</array>
</plist>
"

set systemPasteBoard to current application's NSPasteboard's generalPasteboard()
systemPasteBoard's clearContents()
systemPasteBoard's setString:kmPlistData forType:kmPasteboardDataType

---------------------------------------------------------------------------------

Hey Chris, maybe I missed something, but I thought the objective was to read the plist/XML to get the sub-macro name, and then open that macro for editing.

Hey Folks,

Okay, let’s build upon the “Extract Keyboard Maestro action or macro plist from the clipboard” script.

Open a macro that has an Execute a Macro action in it.

Select that action.

Copy it to the Clipboard.

Run the script.

Find yourself editing the macro called by the selected Execute a Macro action.

-Chris

---------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/06/03 22:05
# dMod: 2016/06/04 01:16
# Appl: Keyboard Maestro
# Task: GO-TO macro of selected “Execute a Macro” action in the Keyboard Maestro Editor.
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Extract, @Keyboard_Maestro, @Plist
---------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
---------------------------------------------------------------------------------

set kmPlistStr to missing value
set actionUUID to missing value
set systemPasteBoard to current application's NSPasteboard's generalPasteboard()

if (systemPasteBoard's canReadItemWithDataConformingToTypes:{"com.stairways.keyboardmaestro.actionarray"}) as boolean then
  # Copied Action or Actions
  set kmPlistStr to systemPasteBoard's stringForType:"com.stairways.keyboardmaestro.actionarray"
end if

if kmPlistStr ≠ missing value then
  set kmPlistStr to kmPlistStr as text
  
  tell application "System Events"
    set actionPlistRecord to value of (make property list item with properties {text:kmPlistStr})
    set actionUUID to MacroUID of item 1 of actionPlistRecord
  end tell
  
  if actionUUID ≠ missing value then
    tell application "Keyboard Maestro"
      editMacro actionUUID
    end tell
  end if
  
else
  beep
end if

---------------------------------------------------------------------------------
1 Like

@peternlewis - If we start deciphering KM’s Clipboard format like this, what kind of headache will this cause either you or us in the future? Do you see yourself needing to change the clipboard format itself? I realize the values in the plists will change, but what about the format itself?

I'm staring at my keyboard, trying to figure out how to describe the feelings I'm having right now, watching this work on my machine.

If I said I wanted to kiss you, would you take it the wrong way? How about a high-five then. Simply. Awesome.

Also, the timeliness of the code for deciphering the clipboard is downright spooky, considering what I'm working on right now. Again, awesome.

Did I say "Thanks"? Thanks.

2 Likes

Alright guys, here’s what I want next:

I want a palette, or something palette-like, where I can put the names of the macros I’m currently working on, so I click or double-click a macro and jump right to it in the editor.

Pie-in-the-sky, I’d be able to drag-re-order the list, drag macros to it, remove macros from it, etc.

I have to admit that although just about anything is possible, I don’t expect to get this. But “you have not because you ask not”, so, I’m putting this out there.

I make no promises on the internal format of the macro files, not on the format or arrangement of the Keyboard Maestro preferences folder.

Enjoy whatever you like for as long as it lasts, but if it makes sense to change the format at some point in the future, it'll change and any macros or workflows that depend on it will have to be reworked - I can't afford to have progress of Keyboard Maestro limited by this sort of thing. I could just say "its all private, don't touch", but you may as well get as much value out of it as you can in the mean time, and that time might be decades for all I know.

Good response.

I was also concerned that we might be distressing you with the thought of us tinkering like this. Glad to know you’re cool with it.

Looks like I didn't post this here or if I did I missed it.

@JMichaelTX - Can you mark this topic as "Solved"?

Since @MitchellModel posted the original topic/question, protocol suggests that he should select the post which is "best answer" or "Solved".

1 Like

Protocol? We have Protocol? … just kidding

We ever actually solve anything definitively? :slight_smile:

I haven’t paid any attention to the “solved” checkbox — what is the effect of checking it? I will check it for this one forthwith. I use Dan’s macro every day and am delighted to have it. i am willing for us to take the hit if the underlying data formats change — they won’t change often enough to matter.

I think you see why, now. I just wanted anyone who might search for this to realize a solution exists, and if they see a checkmark, then they’d know.

I was searching for something else and this topic came up in the search, and I thought “Hey!”. :slight_smile:

See:

A year later while searching for this feature: Thanks a lot to @ccstone & @DanThomas !

It seems like I was able to right click once and go to referenced macro kind of like command+r does in the Finder for aliases. I came here searching for that but I guess I was dreaming.

By the way I love in Keyboard Maestro 8 that you can Comtion (Command+Option (⌥)) + drag a macro to another macro to reference it, very intuitive and just the same as you would in the Finder way to go Peter!

1 Like

You mean something like this?

5

1 Like

Oh snazzy, I wasn’t dreaming, thanks!

1 Like

Hey Folks,

I believe it was Keyboard Maestro 8 that significantly improved its AppleScript support.

The following macro is greatly streamlined thanks to @peternlewis' efforts.

-Chris


Go To ⇢ the Selected Execute Macro Action's ⇢ Target Macro v2.00.kmmacros (6.3 KB)

2 Likes