Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AHK V1 convert to V2 Error when code have variables with $ header. #153

Open
265785 opened this issue Mar 20, 2024 · 8 comments
Open

AHK V1 convert to V2 Error when code have variables with $ header. #153

265785 opened this issue Mar 20, 2024 · 8 comments

Comments

@265785
Copy link

265785 commented Mar 20, 2024

Error output example:

Error: This line does not contain a recognized action.

Text:	$WinID := WinExist("A")
Line:	29
File:	C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\ctrl+g-admin_newV2.ahk

The program will exit.

The function of the code is to use "Ctrl+G" key in the "Save as" or "Open" windows to jump to the using folder in File Explorer or Total Commander when using AHK v1:
Untitled

AHK v1 code:
(Code comes from https://meta.appinn.net/t/topic/3743/34 by xml123)

Loop, %0%  ; For each parameter:
  {
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    params .= A_Space . param
  }
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
 
if not A_IsAdmin
{
    If A_IsCompiled
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
    Else
       DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
    ExitApp
}

#SingleInstance Force
#NoEnv
;#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input
SetBatchLines -1
SetWorkingDir %A_ScriptDir%


#IfWinActive ahk_class #32770		; Open/Save dialog

^g::								; Control-g
{
	$WinID := WinExist("A")

;---------------[ File Explorer ]----------------------------------------

	For $Exp in ComObjCreate("Shell.Application").Windows	{
		$This := $Exp.Document.Folder.Self.Path
		Menu ContextMenu, Add,  %$This%, Choice
		Menu ContextMenu, Icon, %$This%, shell32.dll, 5
	}

;	Clean up
	$Exp := ""

;---------------[ Total Commander ]--------------------------------------

;	Total Commander internal codes
	cm_CopySrcPathToClip  := 2029
	cm_CopyTrgPathToClip  := 2030

	ClipSaved := ClipboardAll
	Clipboard := ""

	SendMessage 1075, %cm_CopySrcPathToClip%, 0, , ahk_class TTOTAL_CMD

	If (ErrorLevel = 0) {
			Menu ContextMenu, Add
			Menu ContextMenu, Add,  %clipboard%, Choice
			Menu ContextMenu, Icon, %clipboard%, shell32.dll, 5
	}

	SendMessage 1075, %cm_CopyTrgPathToClip%, 0, , ahk_class TTOTAL_CMD

	If (ErrorLevel = 0) {
			Menu ContextMenu, Add,  %clipboard%, Choice
			Menu ContextMenu, Icon, %clipboard%, shell32.dll, 5
	}


	Clipboard := ClipSaved
	ClipSaved := ""

;---------------

	Menu ContextMenu, Show
	Menu ContextMenu, Delete

}

#IfWinActive
Return



;_____________________________________________________________________________
;
					Choice:
;_____________________________________________________________________________
;

	$FolderPath := A_ThisMenuItem 
;	MsgBox Folder = %$FolderPath%


	Gosub FeedExplorerOpenSave
		
return


;_____________________________________________________________________________
;
					FeedExplorerOpenSave:
;_____________________________________________________________________________
;    

	WinActivate, ahk_id %$WinID%


;	Read the current text in the "File Name:" box (= $OldText)
	ControlGetText $OldText, Edit1, A
	ControlFocus Edit1, A


;	Go to Folder
	Loop, 5
	{
		ControlSetText, Edit1, %$FolderPath%, ahk_id %$WinID%		; set
		Sleep, 50
		ControlGetText, $CurControlText, Edit1, ahk_id %$WinID%		; check
		if ($CurControlText = $FolderPath)
			break
	}

	Sleep, 200
	ControlSend Edit1, {Enter}, A
	Sleep, 50


;	Insert original filename
	If !$OldText
		return

	Loop, 5
	{
		ControlSetText, Edit1, %$OldText%, A		; set
		Sleep, 50
		ControlGetText, $CurControlText, Edit1, A		; check
		if ($CurControlText = $OldText)
			break
	}

return

The Generated Code has no change to the line of the example error.
$WinID := WinExist("A")--->$WinID := WinExist("A")
image

I feel you can fix many bugs when correcting the generated code of this script. Thank you!

@265785 265785 changed the title AHK V1 convert to V2 Error when have variable with $ header. AHK V1 convert to V2 Error when have variables with $ header. Mar 20, 2024
@265785 265785 changed the title AHK V1 convert to V2 Error when have variables with $ header. AHK V1 convert to V2 Error when code have variables with $ header. Mar 20, 2024
@265785 265785 changed the title AHK V1 convert to V2 Error when code have variables with $ header. AHK V1 convert to V2 Error when code have variables with $ header. Mar 20, 2024
@mmikeww
Copy link
Owner

mmikeww commented Mar 20, 2024

In v2, variables names and function names can no longer start with the $ character

https://www.autohotkey.com/docs/v2/v2-changes.htm#names

Names cannot start with a digit and cannot contain the following characters which were previously allowed: @ # $. Only letters, numbers, underscore and non-ASCII characters are allowed.

What do you suggest it get converted to?

If we simply remove the $ or @ or # then the new variable name without that preceeding character might conflict with another variable name elsewhere in the script. Maybe its better to just output a comment into the converted v2 script instructing the user to do a mass find/replace to change the variable name. Maybe we already do a mass find/replace for certain variables like ClipboardAll but this converter doesn't detect scope so I dont know if thats the best thing for us to do

@eugenesvk
Copy link
Contributor

eugenesvk commented Mar 20, 2024

You can use the awesomeness of AHK unicode support and replace the ASCII values with fullwidth unicode ones

#
@
$

(or make it triple $$$ to reduce the chance of collision or use some UID )

@Banaanae
Copy link
Collaborator

Banaanae commented Mar 21, 2024

We could replace invalid characters with _ and to avoid conflict add AHKv1v2_ to the start?

Maybe its better to just output a comment into the converted v2 script instructing the user to do a mass find/replace to change the variable name.

Probably best solution, doing mass replace via the script can cause issues similar to #115

@Banaanae
Copy link
Collaborator

@eugenesvk I did some testing with your unicode characters and they don't seem to work? Am I using them wrong?
image

@eugenesvk
Copy link
Contributor

eugenesvk commented Jun 20, 2024

I don't see the char/version you use (is it $ or , looks like regular $?)

But it works just fine here (all dollars signs are unicode ones) on 2.0.10

$WinID := WinExist("A")
msgbox('$WinID=' $WinID)
ahk

@Banaanae
Copy link
Collaborator

I can run it is VSCode but not QuickConverter, strange
I wonder if the characters are converted from unicode to ascii?

@eugenesvk
Copy link
Contributor

is you default launcher AHK v2?

@Banaanae
Copy link
Collaborator

Yes, could it be because the quickconverter binary is 32-bit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants