How To Restore Deleted Macro

First of all, I am VERY disappointed in how KM handles the deletion of a macro:

  1. There is NO confirmation of the deletion
  2. The UI is too sloppy – it is easy to delete a Macro when all you wanted to do is delete an Action Step in a Macro
  3. The deletion is immediately permanent. There is no undo or Trash for deletions.

This just happened to me with a complicated Macro. After I recovered from my panic attack, I did a Google search, and a KM doc search, and did not find anything to help recover from macro deletion. Allthough there is a related article in these forums dealing with restore from TM, it did NOT show up in in a Google Search

Fortunately, I had setup KM to sync with DropBox, which, with Version Control, saved my a$$.

So, for anyone reading, I urge you to immediately setup your KM to sync with DropBox, even if you don’t plan on syncing it with another Mac. DropBox is your ONLY short-term recovery since KM itself offers NO recovery. TM can help, but most likely it will be at least a day old.

You can Undo a macro deletion, hence there is no need for confirmation.

You can also revert to a previous days macros (File ➤ Revert Macros), copy or export the macro, revert back and then paste or import the macro.

Also, of course, you should have daily automatic complete backups, and then you can restore your macros from there as well as anything else you might lose by any other means.

This is completely untrue, Keyboard Maestro has several layers of mechanisms to restore deleted macros, starting with Undo.

1 Like

Peter, thanks for the reply.

I have to disagree on the need for a confirmation, regardless of whether or not an undo is available. It is way too easy to hit the large DELETE key without knowing it, and it is hard to know in KM whether you are deleting an Action or Macro.

I did NOT know about an “Undo”, even though after discovering later that I had accidently deleted a complicated Macro that I had just created earlier in the day.

Perhaps one of the problems is lack of documentation.
As I stated in my OP, I did do my due diligence, searching in both Google and in KM documentation, and I found NOTHING about deletion or restoring deleted Macros.

IAC, none of the KM “layers of mechanisms to restore” are of much use for big changes made within the same day.

Please keep in mind the perspective of a very new KM user. “Undo” (CMD-Z) is most often used to undo a text change within a document. Most modern Mac apps that have objects like “Macros” have a Trash container to allow the user to easily find and restore deletions of these objects.

It seems to me that it would not be that hard to add a Confirmation, with a setting in Preferences of whether or not to use.

Thanks for a great product.

Hi, I have a simlar problem, though not exactly the same.
I have just started using Keyboard Maestro, and I really like it; I have two or three laptops that I bounce between; I’m developing a batch production style macro on one, and running it on the others. They are all syncing using the ^File^StartSyncingMacros feature where the file is located on directory that is associated with a cloud service (MEGASync). I thought this would be the best setup for me. I’ve been doing it for a two or three days and everything has been working fine. Today though I’m experiencing regression of the macro version. That is, I keep automagically loosing my edits. This has happened a few times now. I even just created an entirely new macro this morning, developed it for three hours, and then hay presto, it’s disappeared entirely. MEGASync does not keep a history like DropBox. My question is, “Is there any way I can get at the macro history to recover the macros I developed”?

Update: I’ve turned off Sync on all three laptops. I did a ^RevertMacros^BeforeLastSync, and my macro is back again at its last edit. Great. Lesson learnt, I’ll stay clear of syncing and do my own distribution to the other two laptops. It’s a bit more work, but that’s OK.

I’m glad you found the Revert to Before Last Sync and rescued the macro (it makes it very worthwhile that I spent the time on that feature!).

It sounds like MEGASync is not behaving very well. Syncing is hard, which is why Keyboard Maestro relies on a third party service to do it and why I did not try to implement it myself. In my experience, Dropbox is about the only syncing service that works reliably (which is a shame as I’d like to be able to recommend options).

Since I made that comment over 2 years ago, Keyboard Maestro has been changed so that it now requires a CMD-DELETE to delete a Macro. Normal DELETE still deletes an Action.

Thanks for that change, @peternlewis. I would still prefer a confirmation on Macro deletion, since it rarely happens, and has a much, much bigger impact. I know the macOS does not give us a confirmation on file/folder deletion, but actually it is just moving the item to the Trash.

Of course, both Macros and Actions can be restored by a CMD-Z.

Thanks. Appreciate your quick response. If the manual distribution becomes too much of a chore I’ll try DropBox.

I've been using DropBox for years now to sync a large variety of files and apps, including KM Macros. I've never had any issues and DB provides a restore file from prior version and deletion.

To delete a file in the Finder you have to select it and press ⌥⌘⌫ (note the ⌥ key). And it sure asks you for confirmation (fortunately!):

This has been introduced with El Capitan and is called “Delete Immediately” (File menu):

See also my nice blog post :wink:

1 Like

Hi. I just worked some hours on a new Macro an accidently klicked on delete.
Any change to get it back?
a tired and exhausted
Juergen

Try Z

2 Likes

Peter, I think most users, including me, would disagree with that.
Deleting a KM Macro is like deleting (NOT moving to trash) a macOS file.

When DELETING a file, it DOES provide a confirmation:

And, we had another user bit by accidential deletion:

So please, @peternlewis, add a confirmation to the Macro and Macro Group Deletions.

There is pretty much never a confirmation on a Mac for an Undoable action.

Of course there is a confirmation on “Delete Immediately”, because its right there in the dialog:

image

If you delete something in Keyboard Maestro accidentally, that is what Undo is for - specifically to avoid confirmation dialogs.

@peternlewis: I've had great success with Resilio Sync

Peter, forget about the technical details. Just consider the point of view of many of your users who accidently delete a Macro, and do not realize it for a while -- long after an undo will work.

What's the harm in presenting a confirmation of deleting such an important entity as a KM Macro? You can set the default key to "Delete", so it is a very minor inconvenience to those users who know what they are doing.

Please reconsider this request.

Thanks.

It's true: the couple of times this has happened to me, I didn't realize until days later. Thanks.

Peter, thank you for your implementation of Undo in KM. It's saved my bacon many times.

I agree that it's easy to accidentally delete a Macro, but I'm not sure if I agree about the need for a delete confirmation, unless it's accompanied by a "don't show in future" check box.

I do agree that some additional documentation would be helpful (copy what you wrote above and paste into the doco).

For me, when a Macro goes missing, I rarely notice immediately. When I realize it, I have to search for a solution - which led me to this forum post - which is a time drain. At that point, I've likely made other changes... undo repeatedly until I find the deleted macro, copy it, then redo all, paste macro. Of course, as devoy noted, if I realize days later, KM has been quit. In that situation, it's off to the backups.

I just decided to do is create a disabled Group called "zzzzzzz TRASH zzzzzzz" (name pushes it to bottom of list). This will help for any macros I intend to delete, but not macros I accidentally delete.

Additionally, noting in the doco the levels (#) of undos could be useful.
In a perfect world, implementing a History feature as seen in photoshop.

  1. Confirmation dialogs are necessary, regardless of how other apps behave.

  2. Having an UNDO option does not fix the issue of accidentally deleting something 3 hours ago (or even 10 minutes ago).

  3. Yes, I believe that having daily backups is a must, and I do have those, but that's not an excuse to make an app less secure. My daily or even hourly backup is not 100% secure. If my hourly backup runs at 10am, I create an important macro at 10:05, accidentally delete it at 10:30, and work for the next minutes, having a backup has zero impact on recovering the macro. Advocating for backups as a solution, but then not implementing an equal secure system inside KM, is just weird to me.
    My workflow outside KM should not dictate how KM works. It's like a dentist saying: I'm not gonna treat your cavities, because you should have taken care of your teeth in the first place.

  4. Having a Trash macro group would be, in my opinion, a better approach. Better than having the confirmation. Finder is a great example of this. No confirmation, sends to Trash, confirmation when trying to empty trash or delete item from the trash. Simple.

But, since I'm sure none of this will be implemented, here's an AppleScript I'm using now:

-- Check if items are disabled (showing "Enable...") or enabled (showing "Disable...")
tell application "Keyboard Maestro" to activate
tell application "System Events"
	repeat until frontmost of process "Keyboard Maestro" is true
		delay 0.05
	end repeat
	tell process "Keyboard Maestro"
		set menuItem to "none"
		
		-- Check for Enable items (single or multiple)
		repeat with itemName in menu items of menu 1 of menu bar item "View" of menu bar 1
			try
				set itemTitle to name of itemName
				if itemTitle starts with "Enable" and enabled of itemName is true then
					set menuItem to "enable"
					exit repeat
				end if
			end try
		end repeat
		
		-- Check for Disable items if none found
		if menuItem is "none" then
			repeat with itemName in menu items of menu 1 of menu bar item "View" of menu bar 1
				try
					set itemTitle to name of itemName
					if itemTitle starts with "Disable" and enabled of itemName is true then
						set menuItem to "disable"
						exit repeat
					end if
				end try
			end repeat
		end if
	end tell
end tell

-- Exit if no valid selection
if menuItem is "none" then return

-- Show appropriate dialog based on state
try
	if menuItem is "enable" then
		set userChoice to display dialog "⚠️ Do you want to delete the current selection?" buttons {"Cancel", "Delete"} default button "Delete"
	else
		set userChoice to display dialog "⚠️ Do you want to delete the current selection?" buttons {"Cancel", "Delete", "Disable"} default button "Disable"
	end if
on error
	return
end try

tell application "Keyboard Maestro" to activate
tell application "System Events"
	repeat until frontmost of process "Keyboard Maestro" is true
		delay 0.05
	end repeat
	delay 0.1
	tell process "Keyboard Maestro"
		if button returned of userChoice is "Delete" then
			-- Edit > Delete
			set deleteItem to menu item "Delete" of menu 1 of menu bar item "Edit" of menu bar 1
			if enabled of deleteItem then click deleteItem
		else if button returned of userChoice is "Disable" then
			-- View > Disable Action / Disable Macro / Disable Macro Group
			repeat with itemName in menu items of menu 1 of menu bar item "View" of menu bar 1
				try
					set itemTitle to name of itemName
					if itemTitle starts with "Disable" and enabled of itemName is true then
						click itemName
						exit repeat
					end if
				end try
			end repeat
		else
			return
		end if
	end tell
end tell
  1. My trigger is CMD+Delete, but I have the script being triggered by Better Touch Tool, because it completely takes over any keystroke in KM. If it's set as a KM macro, there's the risk of me running it, but if I try to run it right away, the macro is not triggered, so it just fires the default CMD+Delete, which will delete the action/macro/macro group right away. It always needs a few seconds to work properly (I've learned yesterday about this, because of a plist file that needs to be loaded every time some changes are made). So if you have BTT or any other similar app, I would recommend it over creating a KM macro.

  2. To avoid accidents, I've decided to add a Disable button and set it to default. Just in case. You can still delete it clicking the Delete button. This minimizes the risk of deleting the item by default when opening the dialog and hitting Enter right away. Also, disabling an action/macro/macro group, allows you to maybe recover it in the future or take time to really see if you wanted to delete it. If you are sure you don't need the Disable button, feel free to adjust the script, of course. I'm just overly obsessed with avoiding accidents and all that.

  3. It works for Macro Groups, Macros, Actions, because it checks the View menu. And if no valid option is available (for example if you are editing a macro's name), it just doesn't show any dialog.

Since I'm not an AppleScript expert, I asked ChatGPT and Claude to help me with this and after an hour or so going back and forth, it's now working. If anyone wants to optimize it or sees some other case that I'm not aware, feel free to share it here.


EDIT: I just decided to add an extra "nugget". When macros are disabled instead of deleted, it renames the macro by appending (deleted ). That gives a visual cue of why that macro is disabled. It works almost like a "Trash Bin". So an original macro Trigger XYZ will be disabled and renamed (deleted) Trigger XYZ

I don't think this can be applied to Macro Groups or Actions (but actions is not a big deal, because we usually just use the Delete key anyway). If any of you know how to rename a macro group as well, please share.

If you want this behavior, use this script instead:

-- Check if items are disabled (showing "Enable...") or enabled (showing "Disable...")
tell application "Keyboard Maestro" to activate
tell application "System Events"
	repeat until frontmost of process "Keyboard Maestro" is true
		delay 0.05
	end repeat
	tell process "Keyboard Maestro"
		set menuItem to "none"
		
		-- Check for Enable items (single or multiple)
		repeat with itemName in menu items of menu 1 of menu bar item "View" of menu bar 1
			try
				set itemTitle to name of itemName
				if itemTitle starts with "Enable" and enabled of itemName is true then
					set menuItem to "enable"
					exit repeat
				end if
			end try
		end repeat
		
		-- Check for Disable items if none found
		if menuItem is "none" then
			repeat with itemName in menu items of menu 1 of menu bar item "View" of menu bar 1
				try
					set itemTitle to name of itemName
					if itemTitle starts with "Disable" and enabled of itemName is true then
						set menuItem to "disable"
						exit repeat
					end if
				end try
			end repeat
		end if
	end tell
end tell

-- Exit if no valid selection
if menuItem is "none" then return

-- Show appropriate dialog based on state
try
	if menuItem is "enable" then
		set userChoice to display dialog "⚠️ Do you want to delete the current selection?" buttons {"Cancel", "Delete"} default button "Delete"
	else
		set userChoice to display dialog "⚠️ Do you want to delete the current selection?" buttons {"Cancel", "Delete", "Disable"} default button "Disable"
	end if
on error
	return
end try

tell application "Keyboard Maestro" to activate
tell application "System Events"
	repeat until frontmost of process "Keyboard Maestro" is true
		delay 0.05
	end repeat
	delay 0.1
	tell process "Keyboard Maestro"
		if button returned of userChoice is "Delete" then
			-- Edit > Delete
			set deleteItem to menu item "Delete" of menu 1 of menu bar item "Edit" of menu bar 1
			if enabled of deleteItem then click deleteItem
		else if button returned of userChoice is "Disable" then
			-- View > Disable Action / Disable Macro / Disable Macro Group
			repeat with itemName in menu items of menu 1 of menu bar item "View" of menu bar 1
				try
					set itemTitle to name of itemName
					if itemTitle starts with "Disable" and enabled of itemName is true then
						click itemName
						exit repeat
					end if
				end try
			end repeat
			
			-- Rename selected macros by prepending "(deleted) "
			tell application "Keyboard Maestro"
				set selMacroIDs to get selectedMacros
				repeat with mID in selMacroIDs
					set theMacro to macro id mID
					set oldName to name of theMacro
					if oldName does not start with "(deleted) " then
						set name of theMacro to "(deleted) " & oldName
					end if
				end repeat
			end tell
			
		else
			return
		end if
	end tell
end tell

Those AppleScripts look like quite the cool solution to the problem; I don't have time to test them this weekend, unfortunately.

My much-less-advanced quick hack is the following: Whenever I'm changing anything important, or writing a big new fancy macro, Command-D becomes one of my most-used hot keys. This is the shortcut for Duplicate, and I use it on Groups and Macros all the time.

I have a macro I use that enhances this, but I need to clean it up a bit for posting. I'll try to do that if I get some free time today or tomorrow, otherwise it'll be Monday.

-rob.

1 Like