Can't figure out why my script chooses wrong item

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Kellyzkorner_NJ
Posts: 84
Joined: 20 Oct 2017, 18:33

Can't figure out why my script chooses wrong item

02 Jul 2023, 14:55

This is a script that MikeyWW helped me with (thanks so much!!). It works fine, other than if I highlight more than one item. The files, let's say 4, are named as:

Movie Name (2000).mp4
Movie Name (2000)-backdrop.jpg
Movie Name (2000)-logo.png
Movie Name (2000)-poster.jpg

I leave my cursor over the first item in the list. There are sometimes periods between the file name(s). My code already strips that. What is happening that I'm trying to figure out is that my folder will get named Movie Name (2000)-logo or Movie Name (2000)-poster, etc. I don't understand why it chooses other items and it doesn't always seem to be consistent as to which it chooses. I would like it to use the first item in the list to create the name, to circumvent having to go back and fix them. This could also be tv shows, but the same principle applies, I'd like it to use the first in the list for the name, and not lose track of the initially highlighted quantity. Does that make sense and it is doable? Thanks in advance and sorry about my coding.

PS: my gosub hotkey is attached below also

Code: Select all

#If WinActive("ahk_class CabinetWClass")  

					#^]:: ;  when in windows explorer window, and wanting to save a video to the computer, choose the main folder, it makes the folder and subfolder if needed and then makes the save button active to hit enter 

					
					CoordMode, Mouse, Screen
					x := (A_ScreenWidth / 2)
					y := (A_ScreenHeight / 2)
					CoordMode, Caret, Screen ; new
					yx := A_CaretX
					yy := A_CaretY
					MouseGetPos, px, py
					
					;msgbox, 4100, , Explorer Exists, 2
					
					;clipboard =
					WinGetTitle, title
								
					clipboard = 
					
					MouseMove, px, py
					Send {F2}{END}{ENTER}
					Send ^c
					ClipWait, 2			
					
					;msgbox, 4100, , %aax%
						  aax:= % Explorer_GetSelection() ;  worked but commented to test gst
					;MsgBox, 4100, , AAX which is or are the file or files %aax% is Selection `n`nand `n`nclipboard is %clipboard%, 1
					;Msgbox, 4100, , %clipboard% is what is in clipboard - is this the whole file name? ;  , 1
					
					checkifshowormovieexplorer:
					
;  strip text of periods and add back the period before extension

					Sleep 150					
					
					If InStr( title, "TV Shows E" )
					{
	;msgbox, 4100, Currently Opened Folder, TV Shows E, 0.5
						diry = e:\~TV Shows E
						;  msgbox, 4100, , Directory is %diry%, 0.4
					     Sleep 250
					}
					Else
						If InStr( title, "TV F" )
						{
	;msgbox, 4100, Currently Opened Folder, TV F, 1
							diry = F:\~All Video F\~TV F
							;msgbox, 4100, , Directory is %diry%  , 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "TV M" )
						{
		;msgbox, 4100, Currently Opened Folder, TV M, 1
							
							diry = M:\~All Videos M\~TV M
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "TV Shows J" )
						{
		;msgbox, 4100, Currently Opened Folder, TV Shows J, 1
							diry = J:\~All Videos J\~TV Shows J
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "TV Shows L" )
						{
		;msgbox, 4100, Currently Opened Folder, TV Shows L, 1
							diry = L:\~All Video L\~TV Shows L
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "TV Shows N" )
						{
		;msgbox, 4100, Currently Opened Folder, TV Shows L, 1
							diry = N:\~All Video N\~TV Shows N
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "Movies M" )
						{
		;msgbox, 4100, Currently Opened Folder, Movies M, 1
							diry = M:\~All Videos M\~Movies M
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "Movies E" )
						{
		;msgbox, 4100, Currently Opened Folder, Movies M, 1
							diry = E:\~Movies E
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					Else
						If InStr( title, "Movies F" )
						{
		;msgbox, 4100, Currently Opened Folder, Movies F, 1
							diry = F:\~All Video F\~Movies F
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, mx, my
							Sleep 250
						}
					Else
						If InStr( title, "Movies J" )
						{
		;msgbox, 4100, Currently Opened Folder, Movies J, 1
							diry = J:\~All Videos J\~Movies J
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					
					Else
						If InStr( title, "Movies L" )
						{
		;msgbox, 4100, Currently Opened Folder, Movies L, 1
							diry = L:\~All Video L\~Movies L
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					
					Else
						If InStr( title, "Movies N" )
						{
		;msgbox, 4100, Currently Opened Folder, Movies L, 1
							diry = N:\~All Video N\~Movies N
							msgbox, 4100, , Directory is %diry%, 0.4
							Mousemove, px, py
							Sleep 250
						}
					
					
					If InStr( Clipboard, "S0" )
						
					Msgbox, 4100, , This is A TV Show of 9 Seasons or less., 0.3 
					Else
						If InStr( Clipboard, "S1" )
		;Msgbox, 4100, , This is Season 1 of this Show., 1
						{
							Msgbox, 4100, , This is a Season 10 or over of this Show., 0.3
							Mousemove, px, py
						}
				;season = S1
					
					Else
					{
						Msgbox, 4100, , There is no season - or may be more than season 10 - or is a movie., 1

							CoordMode, Mouse, Screen
						x := (A_ScreenWidth / 2)
						y := (A_ScreenHeight / 2)
						CoordMode, Caret, Screen ; new
						x := A_CaretX
						y := A_CaretY
						
						Sleep 60
						;Clipboard = ;  Empty clipboard.
						Mousemove, px, py
						Sleep 700

						SplitPath, clipboard, Oname, Odir, Oext, Oname_no_ext, Odrive 	; get the split name of the original selected file (without extension) to be moved to new or preexisting folder and saved to variables.
						Sleep 1000
						clipboard = %Oname_no_ext%
	;Msgbox, 4100, , clipboard contains %clipboard%, 1

						gosub :*:ttesty
	;msgbox, 4100, , Go to rest of F4 copy file and make folder bit.
						clipboard := haystack
						videoname := clipboard
;;;;;msgbox, 4100, , clipboard contains %clipboard% and videoname contains %videoname%, 1
						
						If RegExMatch(haystack, "i)(.+)\h+S(\d\d)", m)
							name := m1      , season := m2 + 0 ;was just m2
						Else name := clipboard, season := ""
							MsgBox, 64, Show, Name: %name%, 0.8
						
						WholeDir := diry . "\"name
;msgbox, 4100, , I hope this combined them whole directory is %wholedir%
						
						FileCreateDir, %wholedir%
						Sleep 1000

					}
					
;  https://www.autohotkey.com/board/topic/129203-multiline-string-parsing-and-string-manipulation/
					Haystack = 
(
join`r`n %clipboard%
)
					arra := StrSplit(text, "`n", "`r")  
					count := arra.MaxIndex()
					if count = 14 
						arrb := StrSplit(arra.3, "|", a_space), res := arra.1 " " arrb.1 " " arrb.3 " " arra.4 " " arra.5 " " arra.6 " " arra.7 " " arra.4 " " arra.8 " " arra.9 " " arra.10 " " arra.11 " " arra.12 " " arra.13 " " arra.14 ;  was just up to arrb.3
					else
						arrb := StrSplit(arra.2, "|", a_space), res := arra.1 " " arrb.1 " " 
;  arrb.13 " " arra.13 ;  was just 3
		;;;  msgbox % res
;  above worked to eliminate all but the file name and the folder I'm trying to make new folder(s) in, need to remove superfluous stuff now and extract to variable other parts to make folder with
;msgbox, 4100, , the above result is %res% which I hope to save to a variable for next section to work on.


					CoordMode, Mouse, Screen
					x := (A_ScreenWidth / 2)
					y := (A_ScreenHeight / 2)
					CoordMode, Caret, Screen ; new
					x := A_CaretX
					y := A_CaretY
					

					Sleep 60
					

					SplitPath, clipboard, Oname, Odir, Oext, Oname_no_ext, Odrive 	; get the split name of the original selected file (without extension) to be moved to new or preexisting folder and saved to variables.
	;;;;;msgbox, 4100, , Drive %Odrive% `n`nDir %Odir% or %diry%`n`nName without extension %Oname_no_ext%`n`n`n`nDirectory and File Name - %diry% or %Odir%\%Oname_no_ext%, 1
					Sleep 1000
					clipboard = %Oname_no_ext%
	Msgbox, 4100, , clipboard contains %clipboard%, .5
;Return ;  Temporary stop point for testing
					
;  test to see if I run ttesty now, will it mess up below, may have to do it after
					gosub :*:ttesty
	;msgbox, 4100, , Go to rest of F4 copy file and make folder bit.
					clipboard := haystack
					videoname := clipboard
;;;;;msgbox, 4100, , clipboard contains %clipboard% and videoname contains %videoname%, 1
					
; haystack := "TV Show Name" Nightwatch S02E01
;If RegExMatch(haystack, "(.+)\h+S(\d\d)", m)
					If RegExMatch(haystack, "i)(.+)\h+S(\d\d)", m)
;If RegExMatch(videoname, "(.+)\h+(?i:S(\d\d)", m)
;If RegExMatch(clipboard, "(.+)\h+(?i:S(\d\d)", m)
     ;  name := m1      , season := m2 + 0
						name := m1      , season := m2 + 0 ;was jsut m2
;Else name := videoname, season := ""
					Else name := clipboard, season := ""
						MsgBox, 64, Show, Name: %name%`n`nSeason: %season%, 0.8
					if !season
						Msgbox, 4100, , There is no Season so this must not be a TV Show., 0.3
					Else
;  good up to here divert for the non-tv show part.  Need to, though, make the multiple file part be in this too 
						
					WholeDir := diry . "\"name . "\S"season
					msgbox, 4100, , I hope this combined them whole directory is %wholedir%, 1
					
					FileCreateDir, %wholedir%
					Sleep 2000
					
					;Msgbox, 4100, , clipboard Contains %clipboard% and aax contains %aax% `n`nHit Tab after highlighting files I want to move, 1
					
					;KeyWait, Tab   ;  CapsLock
					;Sleep 100000000
					;ClipWait
					Clipboard = % gsp()
					Loop, parse, Clipboard, `n, `r
						FileMove, %A_LoopField%, %wholedir%
					;FileMove, %aax%, %wholedir%
					Sleep 1200
					MouseMove, px, py
					Sleep 300
					Send {Wheeldown 1}
					MouseClick, Left, px, py
					;Send {F2}
					;Send {END}{ENTER}
					Return

gsp() {
    Clipboard =
    Send ^c^x
    ;Send ^x
    ClipWait, 2
    ToReturn := Clipboard
    ;Msgbox, 4100, , Clipboard contains for gsp %clipboard%, 2
    Return ToReturn
}

Code: Select all

:*:ttesty::    ;  <--  https://www.autohotkey.com/boards/viewtopic.php?f=6&t=58981&p=311077#p311077 other than the E in episode and the first word after episode number. 
				
				ClipSaved := ClipboardAll
;msgbox, 4100, , Clipboard initially contains `n`n%clipboard%
				haystack = e01,e02,e03,e04,e05,e06,e07,e08,e09,e10,e11,e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22,.,E R
				
				if InStr( Haystack, Needle )
				{
					clipboard := StrReplace(clipboard, ".", " ") ;  this was okay but took off the period before the extension file type which won't work, will have to add back after this to put period back if I still need or want to use this option
					clipboard := StrReplace(clipboard, "e01", " E01")
					clipboard := StrReplace(clipboard, "e02", " E02")
					clipboard := StrReplace(clipboard, "e03", " E03")
					clipboard := StrReplace(clipboard, "e04", " E04")
					clipboard := StrReplace(clipboard, "e05", " E05")
					clipboard := StrReplace(clipboard, "e06", " E06")
					clipboard := StrReplace(clipboard, "e07", " E07")
					clipboard := StrReplace(clipboard, "e08", " E08")
					clipboard := StrReplace(clipboard, "e09", " E09")
					clipboard := StrReplace(clipboard, "e10", " E10")
					clipboard := StrReplace(clipboard, "e11", " E11")
					clipboard := StrReplace(clipboard, "e12", " E12")
					clipboard := StrReplace(clipboard, "e13", " E13")
					clipboard := StrReplace(clipboard, "e14", " E14")
					clipboard := StrReplace(clipboard, "e15", " E15")
					clipboard := StrReplace(clipboard, "e16", " E16")
					clipboard := StrReplace(clipboard, "e17", " E17")
					clipboard := StrReplace(clipboard, "e18", " E18")
					clipboard := StrReplace(clipboard, "e19", " E19")
					clipboard := StrReplace(clipboard, "e20", " E20")
					clipboard := StrReplace(clipboard, "e21", " E21")
					clipboard := StrReplace(clipboard, "e22", " E22")
					clipboard := StrReplace(clipboard, " mp4", ".mp4")
					clipboard := StrReplace(clipboard, " mkv", ".mkv")
					clipboard := StrReplace(clipboard, " avi", ".avi")
					Clipboard := StrReplace(Clipboard, " the", " The")
					Clipboard := StrReplace(Clipboard, " E R", " E.R.")
				} 
				Text = %Clipboard% 

				text:=Titlecase(Text, "en", "titlecase.ini") ; or:
; text:=Titlecase(Text) ; no need in particular to pass on EN en INI if you are using these defaults, or:
				text:=Titlecase(Clipboard) ; you can use the clipboard directly here
				sleep 500
;text=%clipboard%
;msgbox, 4100, , Clipboard`n`n%clipboard%`n`nafter S01 E01 change but before initial capping The
				sleep 500

;Text = %clipboard%
				clipboard := Text
;Msgbox, 4100, , clipboard now contains`n`n`n`n%clipboard% 
;Sleep 500
;msgbox, % clipboard := StrReplace(clipboard, "The", clipboard)
;FixString = %clipboard%
;StringReplace, FixString, FixString,the,The, All
;Clipboard := FixString
				
		;  IF IN HAYSTACK AN EPISODE NUMBER, GO TO THE BELOW, IF NOT BYPASS.
				haystack = e01,e02,e03,e04,e05,e06,e07,e08,e09,e10,e11,e12,e13,e14,e15,e16,e17,e18,e19,e20,e21,e22
				
				if InStr( Haystack, Needle )
				{
					clipboard := StrReplace(clipboard, ".", " ") ;  this was okay but took off the period before the extension file type which won't work, will have to add back after this to put period back if I still need or want to use this option
					clipboard := StrReplace(clipboard, " e01", "E01")
					clipboard := StrReplace(clipboard, " e02", "E02")
					clipboard := StrReplace(clipboard, " e03", "E03")
					clipboard := StrReplace(clipboard, " e04", "E04")
					clipboard := StrReplace(clipboard, " e05", "E05")
					clipboard := StrReplace(clipboard, " e06", "E06")
					clipboard := StrReplace(clipboard, " e07", "E07")
					clipboard := StrReplace(clipboard, " e08", "E08")
					clipboard := StrReplace(clipboard, " e09", "E09")
					clipboard := StrReplace(clipboard, " e10", "E10")
					clipboard := StrReplace(clipboard, " e11", "E11")
					clipboard := StrReplace(clipboard, " e12", "E12")
					clipboard := StrReplace(clipboard, " e13", "E13")
					clipboard := StrReplace(clipboard, " e14", "E14")
					clipboard := StrReplace(clipboard, " e15", "E15")
					clipboard := StrReplace(clipboard, " e16", "E16")
					clipboard := StrReplace(clipboard, " e17", "E17")
					clipboard := StrReplace(clipboard, " e18", "E18")
					clipboard := StrReplace(clipboard, " e19", "E19")
					clipboard := StrReplace(clipboard, " e20", "E20")
					clipboard := StrReplace(clipboard, " e21", "E21")
					clipboard := StrReplace(clipboard, " e22", "E22")
					clipboard := StrReplace(clipboard, " mp4", ".mp4")
					clipboard := StrReplace(clipboard, " mkv", ".mkv")
					clipboard := StrReplace(clipboard, " avi", ".avi")
					Clipboard := StrReplace(Clipboard, " the", " The")
					Clipboard := StrReplace(Clipboard, " E R", " E.R.")
				}
				Sleep 500
				
				haystack = %clipboard% ;  new, was a sleep of 300
;Msgbox, 4100, , Clipboard should be all correct uggghhh`n`nText variable `n`n%Text%`n`n`n`nClipboard variable`n`n`n`n%clipboard% and videoname contains %videoname%;  might have to strip the extension with splitpath here if the extension is causing a problem
;clipboard =
				Msgbox, 4100, , Clipboard should be correct now as with corrected video file name:`n`n`n`n%clipboard%, 1
;Clipboard := ClipSaved
;videoname = %clipboard% ;  was just assigning the initial haystack of e01 etc to clipboard
				Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Marium0505, mcl and 344 guests