Rename Process Name of uncompiled scripts

Post your working scripts, libraries and tools for AHK v1.1 and older
punchin
Posts: 439
Joined: 17 Jan 2014, 17:54

Rename Process Name of uncompiled scripts

22 Aug 2016, 21:49

Just throw this into your My Documents\AutoHotkey\Lib folder and add Rename() to the top of your script. It will rename the Process name to your script name ".exe".

Code: Select all

Rename()
{
	NewName := A_ScriptName ;-- Grabs current script name (script.ahk)
	StringTrimRight, NewName, NewName, 3 ;-- Removes the AHK extension
	NewName .= "exe" ;-- Appends EXE extension
	
	 StringSplit, AHKName, A_AHKPath, \ ;-- Takes current AHK executable path and splits it by \

	Loopy := AHKName0 - 1 ;-- Sets the loop iteration count to one less than the parts of the file path
	Loop, %Loopy% ;-- Loops through file path parts
		AHKDir .= AHKName%A_Index% . "\" ;-- Rebuilds AHK path. Leaves off executable name
	
	AHK_CB_Dir := AHKDir . NewName ;--Appends the current script's name with EXE extension to the AHK path
	AHK_AH_Dir := AHKDir . "AutoHotkey.exe" ;-- Build default AHK path
  	ScriptDir := A_ScriptFullPath ;-- Saves current script path including file name
	
 	Name := AHKName%AHKName0% ;-- Grabs the last portion of the split AHK path
 	
 	If (Name != NewName) ;-- Tests to see if script name matches AHK name
 	{
 		FileMove, %AHK_AH_Dir%, %AHK_CB_Dir% ;-- Renames AutoHotkey.exe to current script.exe
 		If ErrorLevel ;-- Unable to rename?
  		{
 			MsgBox, Unable to rename AutoHotkey.exe
 			Run, %AHKDir%
 			ExitApp
 		}
 		
		IfExist, %AHK_CB_Dir% ;-- Verifies new AHK executable is in place
 			Run, %AHK_CB_Dir% %A_ScriptFullPath%, UseErrorLevel ;-- Reloads script via new AHK name
  		
  		If ErrorLevel ;-- Unable to run new ahk name?
  		{
  			MsgBox, Error running %A_ScriptFullPath%
  			FileMove, %AHK_CB_Dir%, %AHK_AH_Dir%
  			ExitApp
		}
		ExitApp ;-- Ensures rest of code does not run after 
	}
 	else ;-- AHK path matches script name
	{
		FileMove, %A_AHKPath%, %AHKDir%AutoHotkey.exe ;-- Renames AHK back to AutoHotkey.exe
		If ErrorLevel ;-- Unable to rename?
		{
			MsgBox, Unable to rename %NewName%
			Run, %AHKDir%
			ExitApp
		}
  	}
}
Last edited by punchin on 23 Aug 2016, 18:53, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rename Process Name of uncompiled scripts

20 Sep 2016, 11:15

Edit 2018-06-11: An AHK v2 version is available here: rename(), again, thanks for this function.
punchin wrote:Just throw this into your My Documents\AutoHotkey\Lib folder and add Rename() to the top of your script. It will rename the Process name to your script name ".exe".
First, thank you very much for this script, I might have good use of this in some situations.
I get problems when there are white spaces in the script path. It renames the autohotkey.exe to the script name, but doesn't rename it back, and the script doesn't run.
docs wrote: To pass parameters, add them immediately after the program or document name. If a parameter contains spaces, it is safest to enclose it in double quotes (even though it may work without them in some cases).
Source: Run
Changing this line,

Code: Select all

Run, %AHK_CB_Dir% %A_ScriptFullPath%, UseErrorLevel ;-- Reloads script via new AHK name
to

Code: Select all

Run, % AHK_CB_Dir " " RegExReplace(A_ScriptFullPath,"([ ]+)","""$1"""), UseErrorLevel ;-- Reloads script via new AHK name
should be sufficient to fix it. However, I didn't try it in your original script, I have made some minor modifications:

Code: Select all

Rename(NewName:="")
{
	if NewName
		NewName.= RegExMatch(NewName,"\.exe$") ? "":".exe"
	else
		NewName := RegExReplace(A_ScriptName,"\.ahk$",".exe") ;-- Grabs current script name (script.ahk)
		
	SplitPath, A_AHKPath, Name, AHKDir
	AHKDir.="\"
	AHK_CB_Dir := AHKDir . NewName ;--Appends the current script's name with EXE extension to the AHK path
	AHK_AH_Dir := AHKDir . "AutoHotkey.exe" ;-- Build default AHK path

 	If (Name != NewName) ;-- Tests to see if script name matches AHK name
 	{
 		FileMove, %AHK_AH_Dir%, %AHK_CB_Dir% ;-- Renames AutoHotkey.exe to current script.exe
 		If ErrorLevel ;-- Unable to rename?
  		{
 			MsgBox, Unable to rename AutoHotkey.exe
 			Run, %AHKDir%
 			ExitApp
 		}
 		
		IfExist, %AHK_CB_Dir% ;-- Verifies new AHK executable is in place
 			Run, % AHK_CB_Dir " " RegExReplace(A_ScriptFullPath,"([ ]+)","""$1"""), UseErrorLevel ;-- Reloads script via new AHK name
  		
  		If ErrorLevel ;-- Unable to run new ahk name?
  		{
  			MsgBox, Error running %A_ScriptFullPath%
  			FileMove, %AHK_CB_Dir%, %AHK_AH_Dir%
  			ExitApp
		}
		ExitApp ;-- Ensures rest of code does not run after 
	}
 	else ;-- AHK path matches script name
	{
		FileMove, %A_AHKPath%, %AHKDir%AutoHotkey.exe ;-- Renames AHK back to AutoHotkey.exe
		If ErrorLevel ;-- Unable to rename?
		{
			MsgBox, Unable to rename %NewName%
			Run, %AHKDir%
			ExitApp
		}
  	}
}
This have not been tested thoroughly.
Last edited by Helgef on 11 Jun 2018, 10:48, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rename Process Name of uncompiled scripts

21 Sep 2016, 05:46

There was also some problems with reloading renamed scripts, I've added an option to reload from the tray menu. Tray->Reload renamed script

Code: Select all

Rename(NewName:="", reloading:=0)
{
	if NewName
		NewName.= RegExMatch(NewName,"\.exe$") ? "":".exe"
	else
		NewName := RegExReplace(A_ScriptName,"\.ahk$",".exe") ;-- Grabs current script name (script.ahk)
		
	SplitPath, A_AHKPath, Name, AHKDir
	AHKDir.="\"
	AHK_CB_Dir := AHKDir . NewName ;--Appends the current script's name with EXE extension to the AHK path
	AHK_AH_Dir := AHKDir . "AutoHotkey.exe" ;-- Build default AHK path
	;MsgBox, % Name "`n" NewName
 	If (Name != NewName || reloading) ;-- Tests to see if script name matches AHK name
 	{
 		FileMove, %AHK_AH_Dir%, %AHK_CB_Dir% ;-- Renames AutoHotkey.exe to current script.exe
 		If ErrorLevel ;-- Unable to rename?
  		{
 			MsgBox, Unable to rename AutoHotkey.exe
 			Run, %AHKDir%
 			ExitApp
 		}
 		
		IfExist, %AHK_CB_Dir% ;-- Verifies new AHK executable is in place
 			Run, % AHK_CB_Dir " " RegExReplace(A_ScriptFullPath,"([ ]+)","""$1"""), UseErrorLevel ;-- Reloads script via new AHK name
  		
  		If ErrorLevel ;-- Unable to run new ahk name?
  		{
  			MsgBox, Error running %A_ScriptFullPath%
  			FileMove, %AHK_CB_Dir%, %AHK_AH_Dir%
  			ExitApp
		}
		ExitApp ;-- Ensures rest of code does not run after 
	}
 	else ;-- AHK path matches script name
	{
		FileMove, %A_AHKPath%, %AHKDir%AutoHotkey.exe ;-- Renames AHK back to AutoHotkey.exe
		If ErrorLevel ;-- Unable to rename?
		{
			MsgBox, Unable to rename %NewName%
			Run, %AHKDir%
			ExitApp
		}
		Menu,Tray,Add,Reload renamed script,Rename_Reload
  	}
	return
	Rename_Reload:
		SplitPath, A_AHKPath, exeName
		Rename(exeName,1)
	return
}
Again, this have not been tested thoroughly.
Guest

Re: Rename Process Name of uncompiled scripts

24 Sep 2016, 11:42

That Rename Reload looks interesting. I tried to make it into a function, for convenience. But every attempt failed. (sometimes losing ahk association with autohotkey.exe. Can anyone out there make that into a function.?
Guest

Re: Rename Process Name of uncompiled scripts

24 Sep 2016, 13:57

Plse disregard my Guest posting at 11:42.
"Include" was the command was what I was looking for and have resolved my dilemma to my satisfaction.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rename Process Name of uncompiled scripts

24 Sep 2016, 14:52

Guest wrote:That Rename Reload looks interesting. I tried to make it into a function, for convenience. But every attempt failed. (sometimes losing ahk association with autohotkey.exe. Can anyone out there make that into a function.?
If Tray->Reload renamed script works, then I see no reason this wouldn't work:

Code: Select all

Rename_reload_func()
{
	SplitPath, A_AHKPath, exeName
	Rename(exeName,1)
}
Eg,

Code: Select all

Rename("Hello")
Random,rnd
MsgBox, % rnd

F1::
	Rename_reload_func()
return

Rename_reload_func()
{
	SplitPath, A_AHKPath, exeName
	Rename(exeName,1)
}
DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 06:59

so as you said i saved this script in .ahk name "Rename()" and put it in My Documents\AutoHotkey\Lib but when i added "Rename()" to top of one of my script it errors "Call to nonexistent function", what am i missing? :?:
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 07:05

The script in My Documents\AutoHotkey\Lib must be named Rename.ahk, not Rename().ahk
DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 07:32

Helgef wrote:The script in My Documents\AutoHotkey\Lib must be named Rename.ahk, not Rename().ahk
tnx :lol: :D
DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 08:14

Helgef wrote:The script in My Documents\AutoHotkey\Lib must be named Rename.ahk, not Rename().ahk
so i don't know why but after addiding rename() to top of a couple of my scripts, and reloading them i got this error:
>"C:\Program Files\AutoHotkey\AutoHotkey.exe" /ErrorStdOut "D:\AHK\!Mine\!!Remap.ahk"
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
so i restarted my computer but now, non of my scripts are readable for window:
Image

any suggestion? :idea:

Edit: installed the new version and it fixed but didn't understand what was cause this problem :shock: :?
Last edited by DeepMind on 27 Sep 2016, 08:28, edited 1 time in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 08:27

If this function fails it can rename your autohotkey.exe file, check your AHK folder and see if there is an .exe file with one of your scripts name, then rename it to AutoHotkey.exe
Edit:
It is unfortunate.

I have not worked on this script any further to solve this, I have used it quite a bit without any problems as it is now though. But do not use the regular reload function. Use the Reload renamed script from the tray menu, or call a function like this:

Code: Select all

Rename_reload_func()
{
	SplitPath, A_AHKPath, exeName
	Rename(exeName,1)
}
Last edited by Helgef on 27 Sep 2016, 08:31, edited 1 time in total.
DeepMind
Posts: 271
Joined: 19 Jul 2016, 14:47

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 08:30

Helgef wrote:If this function fails it can rename your autohotkey.exe file, check your AHK folder and see if there is an .exe file with one of your scripts name, then rename it to AutoHotkey.exe
ohhhhkaaay, tnx :)
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Rename Process Name of uncompiled scripts

27 Sep 2016, 22:03

Tip: whenever you see "c:\Program" you can be pretty sure your error is related to quotes, because "c:\Program" rarely exists but "c:\Program Files" often does

Suggestion: instead of FileMove'ing (renaming) autohotkey.exe i'd recommend filecopy'ing it, running it, then deleting it (or keeping it)

Or personally i compile almost every script with a simple function that effectively turns any .ahk file into a shortcut to a newly compiled .exe (which gives me the same benefit as well as makes distribution seemless)
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: Rename Process Name of uncompiled scripts

28 Sep 2016, 01:12

I am not answering a particular post in this thread, but I wound up modifying exe() previously to copy AutoHotkey.exe to the name of my script and launching that copy with the script as the argument. That winds up with multiple .exe files that are really just copies of AutoHotkey but it was an easy solution to get the process name right without doing a compile.

The AutoHotkey.exe is also not copied again if the .exe file exists, it just launches what's there, so an upgrade to a newer version of AutoHotkey requires a wipe of the .exe files that match existing script names.

This approach had some great benefits for me:
1) The process names look normal now
2) It handles reloads just fine (avoids one of the problems exe() had)
3) It lets each of the script tray icons be handled differently by Windows, so if you don't want all of them in either the tray or the inactive icon group, they can now be split (vs. all of them falling under AutoHotkey.exe and getting grouped)
punchin
Posts: 439
Joined: 17 Jan 2014, 17:54

Re: Rename Process Name of uncompiled scripts

28 Sep 2016, 01:50

I agree that creating a copy and running it instead os the optimal path, but the computer I do nearly all of my coding on is my work computer. The security on it makes launching non-approved exes impossible. Thats why I made my script rename it instead of copying it. Although now I'm wondering how plausible it would be to launch scripts with a copy of the ahk version of the exe in the same directory...
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Rename Process Name of uncompiled scripts

28 Sep 2016, 03:01

gwarble wrote:Tip: whenever you see "c:\Program" you can be pretty sure your error is related to quotes, because "c:\Program" rarely exists but "c:\Program Files" often does
Suggestion: instead of FileMove'ing (renaming) autohotkey.exe i'd recommend filecopy'ing it, running it, then deleting it (or keeping it)
Or personally i compile almost every script with a simple function that effectively turns any .ahk file into a shortcut to a newly compiled .exe (which gives me the same benefit as well as makes distribution seemless)
There is no hardcoded paths in the script, and the error is not related to non existing paths, it is an error due to having spaces in paramaters passed in the Run command. As described in post #3 above.
As for compiling instead, that is exactly what I wanted to avoid, you might get the same benefits if you compile, but you get new disadvantages. :)

Regarding FileCopy, changing that was the first thing I tried, but I still had problems, so I changed it back, and I have had no problems with this script since my small modification from post #3 and #4.
It's a great script by Punchin, I had tried Exe() earlier but it didn't fit my needs, and I didn't want to put in any effort to modify it.
User avatar
gwarble
Posts: 524
Joined: 30 Sep 2013, 15:01

Re: Rename Process Name of uncompiled scripts

28 Sep 2016, 21:06

Helgef wrote:There is no hardcoded paths in the script, and the error is not related to non existing paths, it is an error due to having spaces in paramaters passed in the Run command. As described in post #3 above.
It doesn't really have anything to do with hard-coded paths, its the difference between Run, %AHK_File_Path% and Run, "%AHK_File_Path%" in many cases.

Edit: I guess if it wasn't clear my original post was responding to DeepMind's post with that error, in which case he must have been using the OP's posted coded without your quotation fix
EitherMouse - Multiple mice, individual settings . . . . www.EitherMouse.com . . . . forum . . . .

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 115 guests