A utility to bring Crimson Editor to the foreground

Posted by Nick Gammon on Tue 23 May 2006 04:58 AM — 8 posts, 49,547 views.

Australia Forum Administrator #0
I have been playing with Crimson Editor, making it the default program for editing MUSHclient scripts. However I found to my annoyance that when I pressed Ctrl+Shift+H although Crimson Editor opened the script file, the program was not brought to the foreground, so that it was an extra step to click on it, to edit the file.

I couldn't find an easy way around this - it seems that the code used inside Crimson Editor to prevent multiple copies being active at once does not correctly bring the program to the front if it was already running.

Thus, I have written a small DLL that you can download and use with MUSHclient. It exposes a couple of handy utilities:

  • windows_utils.send_to_front - bring a program to the front
  • windows_utils.shell_execute - uses the Windows shell to run a program, open a file, print it, etc.


The DLL, and its source code, can be downloaded from:

http://www.gammon.com.au/files/mushclient/lua5.1_extras/windows_utils.zip

File size: 27 Kb.

Inside are windows_utils.dll and windows_utils.c.

Put windows_utils.dll into the same directory as MUSHclient.exe.

To make it work for me I edited the Lua "sandbox" in the Global Preferences, and added these lines *before* the call to MakeSandbox (near the top in other words):


assert (package.loadlib ("windows_utils.dll", "luaopen_windows_utils")) ()

Accelerator ("Shift+Ctrl+H", 
  [[/DoCommand "EditScriptFile"
  windows_utils.send_to_front "Crimson Editor"]])


What this does is load the DLL, and then use the Accelerator script command to change the accelerator for editing the script file (Shift+Ctrl+H) to:

  • DoCommand "EditScriptFile" -- this still edits the script file
  • windows_utils.send_to_front "Crimson Editor" -- this brings Crimson Editor to the front


If you want to use this with a different editor, just change the words "Crimson Editor" to something else (eg. UltraEdit). The match is case-sensitive on the start of the title of the window.

The other utility in the DLL is an interface to the ShellExecute operating system function. This lets you open, explore or print documents or programs. For example:


assert (windows_utils.shell_execute ("c:\\mushclient\\worlds\\SMAUG.MCL"))


Warning - this could be misused if you ran untrusted plugins or scripts. If this is likely to happen I suggest you put this line into your Lua sandbox to disable it:


windows_utils.shell_execute = nil


You would do this after the line:


assert (loadlib ("windows_utils.dll", "luaopen_windows_utils")) ()
Amended on Wed 04 Jun 2008 09:31 PM by Nick Gammon
USA #1
Wow, thats nifty Nick. Thanks!
USA #2
Is it possible to access the ftp features of Crimson Editor with aliases and a little scripting in mushclient? What I want to do is open a file from the mud I play one for editing.
USA #3
One note with this.. The "FindWindow" API, which seems to be what he is using (just a guess, since I haven't looked at the code, but used the same for one of my tests), has a problem. It cannot tell the difference between different program windows with the same name and bigger issues arise when trying to deal with a window that changes its name, such as Mushclient does, when you open a new world (its looking at the Title bar for the name, since MS wasn't bright enough to make a "name" field or require that such a filed have a unique name for every program/process.

The safest solution *if* you could manage it, is to do something more complicated, you need to either find the window after creation (poor, but possibly the only solution), then remember that windows ID from then on (or) you would need to run the application with an API call that returns the unique process ID that Windows gives it when it starts, then remember that, so you can find the window when you need it again. Otherwise, the only "safe" solution is to only use it with programs that will not allow you to run more than one copy of themselves at one time, so you can't have a name conflict.

Anyway, nice utility. Its useful, but people should be aware that there are programs that it is "not" reliable to use it with, such as, for example, if you tried to use it with Internet Explorer and had 'several' such windows open. Unless you can identify what window you opened when you ran the program and remember it, you are relying on an API call that has no real idea what you are looking for, so might "find" and "bring to front" the wrong window.
Australia Forum Administrator #4
Quote:

Is it possible to access the ftp features of Crimson Editor with aliases and a little scripting in mushclient?


Not sure, depends a bit on what scripting is available in Crimson Editor, but I have my doubts. Generally programs are protected from influencing each other, for security reasons.
Australia Forum Administrator #5
These two utilities are now built into MUSHclient version 4.60 onwards as utils.sendtofront and utils.shellexecute (note the different function names, for consistency with the rest of the Lua utilities).
USA #6
Nice update, but is this a new all-time record in thread necromancy? :)
Australia Forum Administrator #7
Not really, sometimes threads just pop up again after years.

Plus, if someone found this thread and were planning to download the utility, they don't need to bother now.