Using AutoHotKey to provide option to reset workflow when Ctrl-S pressed

I have seen a few people mention the possibility of saving workflows without data, for example:

I ran into the problem myself a couple of times today where I had created workflows with a relatively large amount of data and then out of habit hit Ctrl-S. The first time it inconvenienced me for a few minutes, but the second time this happened, I found that 30 minutes later my KNIME was still out of action, and as I was only testing some ideas, I decided to go kill it from Task Manager [hands over ears “la la la”] because it seemed to be getting nowhere fast.

So it got me thinking, how could I prevent this situation from occurring a third time? I don’t want to get out of the habit of pressing Ctrl-S periodically, but likewise I don’t want to make my KNIME unusable for large amounts of time.

What I needed was a way to trap Ctrl-S, and have it give me the possibility of saving without data, or at least warn me, so I could decide what I wanted to do (i.e. reset my workflow and then save, or save it with data intact - and go read a book!). I decided that my solution also needed to deal with Ctrl-Shift-S (save all) in some way too.

I have used an application called AutoHotKey in the past, and I wondered if it could assist me here. (It is a free open source application that can trap key presses, and take action on them)

After some experimentation, I decided it could, and I put together the following script:


AutoHotKey script

/* Auto Hot Key script to trap Ctrl-S pressed in KNIME.exe
  Gives the user the option of resetting the workflow prior to saving
 This helps avoid large wait time for saving of large data sets with workflow

 Script written by @takbb Brian Bates - 18 May 2021
 This script is free to use for any use
 Use at your own risk - There is no cast-iron guarantee that it will always prevent unwanted saving of workflow data
 but it will hopefully help.

 To use this script, install AutoHotKey - available from autohotkey.com
 Place it in same folder as a copy of AutoHotKey.exe
 rename AutoHotKey.exe as KnimeSaveAfterReset.exe  (i.e. the same name, but with .exe extension as this script)
 and run at either at Windows Startup, or priot to running Knime.exe
*/

;TrayTip, AutoHotKey, Looking for KNIME Analytics Platform - Ctrl S press , 16
SetTitleMatchMode  3 ; window title equals
loop {
  #IfWinActive ahk_exe knime.exe 

	^+s::	
	MsgBox, 1,"KNIME Ctrl-Shift-S Pressed (detected by AHK script)", "This will save ALL unsaved workflows including data. Are you sure?" 
	ifMsgBox OK
	{
		Send ^+s
	}
	else 
	{
		MsgBox 0, "KNIME - AutoHotKey", "Workflow(s) not saved"
	}
	return
	  
	^s::	
	MsgBox, 3,"KNIME Ctrl-S Pressed (detected by AHK script)", "Do you want to reset the workflow before saving, to avoid saving data? 
	ifMsgBox Yes
	{
		Sleep, 30 
		Send ^{a}
		Sleep, 1000
		Send {F8}
		Sleep, 1000
		Send ^{s}
	}	
	else
	{
		IfMsgBox No
		{
			Send ^{s}
		}
		else
		{
			MsgBox 0, "KNIME - AutoHotKey", "Workflow not saved"
		}
	}
	return
}

I have uploaded it here as a text file too.

KnimeSaveAfterReset.ahk.txt (1.6 KB)

If you are interested and want to try it with AutoHotKey, save it somewhere and remove the .txt extension, so that it just becomes KnimeSaveAfterReset.ahk

Obviously you’ll need to download and install AutoHotKey too.

With AutoHotKey (AHK) installed on my pc, I can either run the KnimeSaveAfterReset.ahk file manually by double-clicking it, in which case AHK will run the script in my system tray, or alternatively I can place a copy of AutoHotKey.exe in the same folder as KnimeSaveAfterReset.ahk, and then rename the AutoHotKey.exe as KnimeSaveAfterReset.exe. In that case, executing the KnimeSaveAfterReset.exe will run AHK and automatically run the .ahk script, which is quite cool. In this way it can be set to run at Windows startup, and no messing with command line parameters!

Once running it sits there waiting for Ctrl-S or Ctrl-Shift-S to be pressed. If they are pressed outside of Knime, it ignores the presses. A line in the script tells it to only be interested if the active application is Knime.exe.

To provide some demo screenshots here, I have a demo Data Generator which is set to generate a 5000 rows and then does a cross join, thus creating 25 million rows. That’s enough to demo the effect.

image
Ctrl-S (Save)

Now if I press Ctrl-S, then instead of Knime saving straight away, the following dialog appears:

image

If I press Cancel, it blocks the save, but the workflow remains with its data intact:
image

If I choose Yes, it issues a Ctrl-A (select all), F8 (reset), and finally Ctrl-S to save, thus sparing me several minutes wait that I didn’t want. It does a short wait between button presses. That seems to work on my system. It would be good to get feedback about whether this works for other people, or if any tweaking is required.

image

If I choose No, it does the Ctrl-S, as it would have done, and saves the data while I go get a coffee (or write an article about AutoHotKey on the Knime forum). :wink:
image
(to be fair, 25 million rows didn’t even allow me to get downstairs and fill the kettle, but you get my point!)

Ctrl-Shift-S (Save All)

Trapping Save All was also something I decided it should do, although it isn’t so common for me to do that key press. In this case I cannot easily go through each workflow and reset, so I thought at least it should give a warning, and allowing the option of not doing the action.

So pressing Ctrl-Shift-S results in the following dialog:

image

Pressing OK will continue with the action (AHK will send a repeat Ctrl-Shift-S to KNIME).

Pressing Cancel will result in the workflows not being saved, and the following warning dialog being displayed. I can then go through and reset individually any workflows that have large amounts of data:

image


Obviously this is only trapping Ctrl-S and Ctrl-Shift-S. It cannot trap the use of the File-Save and File-Save All, and nor can it trap the pressing of the Save icons on the toolbar. If you use those, then this isn’t really going to help. Sorry!

Likewise, if you don’t want to be resetting your data when you save then this isn’t for you either (although it doesn’t prevent you doing so, so perhaps having the option presented each time might still benefit you).

I strongly advise though using the Auto Save feature built into Knime (and having Save data switched off on that option). Perhaps with that plus this, you have the best of both worlds. You can rely on Auto Save to protect you from disaster, and this key-press trap stops you tying up your pc when you don’t want to!

If you are like me, and just naturally press Ctrl-S periodically without giving it a second-thought, then maybe you too will find this useful if you are often working with large data sets. :slight_smile: But it does mean I might cut down on coffee… :crazy_face:

Enjoy (or ignore :wink: ) as you see fit!

8 Likes

Hi @takbb
Once time more, I find one of your threads crystal clear and extremely helpful. I’m adding this comment not just to thank you for sharing this great solution but also to encourage you to publish it as a KNIME blog. In my opinion, it is ready to go to press :clap: :smiley: !

Best,
Ael

2 Likes

Thank you @aworker, your comments are generous as always!

I’m glad you enjoyed that post from the past. I guess it’s true to say that some of my posts would actually be better suited to a blog format, and I’ve sometimes thought about writing the odd blog on my own website (but I’ve also doubted that anybody would read it, lol). I’ve not really looked into how to go about it, and if I did start writing such things you’d probably see a barrage of several posts in a few days and then nothing for months…

Sometimes I come across things that I find useful and I think others might too. So I end up writing about it. In many ways it’s my way of documenting something useful in case “future-me” needs it. It’s a gift from “past-me”, to maybe make up for all the problems I’m otherwise storing up for “future-me”! I’m never quite sure though if anybody other than me will find these things useful, and it’s difficult to gauge.

In some ways, it’s the same with components. If I ever see the same questions being asked again and again, or if I find myself repeating parts of workflows I end up making a more generic component but again I never know if anybody else agrees that these things have a use. I write them anyway because I know I’ll use them, but I like to think they’ll help others too. My big regret with components in KNIME is that they feel like “second-class citizens” to the standard Nodes, which is a shame because the beauty of components is that they are relatively easy to build, document and share, and they re-use existing tried-and-tested nodes. But whilst the ease with which they can shared on the hub, and dropped into a workflow is great, they are hindered in my view in that there is no way to add them to a local component-palette for easy recall of “favourites”. This, I think, stifles their take-up across the community. And there I go… writing another blog … :rofl: Yeah, maybe you are right. I need to look into where and how to start writing them. You’ll regret mentioning it :wink:

best wishes!


(While I’m here, having re-read the above post, which focussed on trapping control key presses with AutoHotKey, I realised that one thing was missing. I never mentioned the website. A web search would quickly find it, but this is the link for more information https://www.autohotkey.com/. AHK is very powerful and can act both to capture keypresses and also detect the presence of specific application windows and be scripted to take actions such as press buttons on dialogs, thereby enabling the automation of certain tasks.)

2 Likes

Great points as always @takbb!

I agree that components are neglected by this platform from a UI standpoint. They should have the same level of respect, UI capabilities and easy access as base nodes. If they were accessible in the node library (or similar) and could be accessed by keyboard / drag drop then it would allow us to create and easily drop in “small task” components.

I also use programs like auto hot key, Macro Recorder, x-mouse, etc. They really open up a lot more customization possibilities for users to improve program UI themselves instead of just waiting around for programmers to address them for you.

3 Likes

It would be great to get some user feedback support for better component organization, UI tools and quicker deployment. I tried a feedback post, but it didn’t go anywhere…

2 Likes

@iCFO I agree with you.

There has been quite a lot of publicity given to components and I have been creating a few myself for functionality that I think might be useful. To me they are like macros and the beauty of them is that they can make complex portions of workflow much more accessible to others. Not everybody wants to have to understand exactly how to transform data from A to B any more than they want to know the inner workings of a Sort Routine. For many people, there is a job to be done and they understandably want the simplest path to achieving their goal.

I can understand that user-written components should be kept separate from fully fledged nodes but my take on it is that should still be accessible. I would like a panel in which to drop the ones that I find useful, for ease of access. Currently I store my own in my public or private area on the hub, but they aren’t as easy to access. And say I wish to use a component that you’ve made… I have to go find it each time in your public hub space, or I have to store a copy of it in a local “useful components workflow” I have for the purpose. It works but it isnt’t as friendly as it could be.

The component aspect though is still something I think is great about KNIME. I just want to see it made even better!

3 Likes

How did I miss this thread???

Thank you first of all at @aworker for commenting about it so that it “revived”, and of course, thank you @takbb for sharing!!

1 Like