Multiple checkboxes

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
dmg
Posts: 287
Joined: 02 Oct 2013, 01:43
Location: "Twelve days north of Hopeless and a few degrees south of Freezing to Death"
Contact:

Re: Multiple checkboxes

01 Apr 2014, 21:39

OK. If you put a msgbox at the top of a script and display a var named path it contains nonsense. I have no idea why a variable named path automatically contains something. That is fixed by changing "path" to "filepath", but that does not solve the original problem. It seems my code had the parsing loops operating on the same variables, but the content was being altered by each loop so if you had both options checked the second was not getting the correct data for SplitPath to use.

I have gone over it, and tested it, and threatened it, and this code actually appears to work:

Code: Select all

; WORKS ON FILES AND FOLDERS
; activates XYplorer window, copies selected item(s) to clipboard, performs chosen rename option and renames item(s)

#SingleInstance force
SendMode Input

 IfWinExist, XYplorer
 WinActivate, XYplorer

Gui, Add, Text, h-1 section , Select Rename Option
Gui, Add, Checkbox, vRemoveStart , Remove first 5 characters
Gui, Add, Checkbox, vAddDate , Append today's date
Gui, Add, Button, w70 h22 default, Rename
Gui, Add, Button, xp+80 yp w70 h22 , Cancel
Gui, show,,DeRenamer
return

buttonRename:
Gui, submit, nohide
clipboard := ""
 WinActivate, XYplorer
 Send, ^c
 ClipWait
 cliptemp := clipboard
 saved := cliptemp
If (RemoveStart=1)
    gosub RemoveStart
If (AddDate=1)
    gosub AddDate
    
    gosub, finish
return

buttonCancel:
GuiEscape:
GuiClose:
exitapp

RemoveStart:
temp := ""
Loop, Parse, cliptemp, `n, `r
 {
   splitpath, a_loopfield, name, filepath, ext, basename, drive
   temp .= "`n" filepath "\" SubStr(Name, 6)
 }
cliptemp := ltrim(temp, "`n")
return

AddDate:
temp := ""
FormatTime, dateStamp, YYYYMMDDHH24MISS, -yyyy-MM-dd
Loop, Parse, cliptemp, `n, `r
 {
   splitpath, a_loopfield, name, filepath, ext, basename, drive
   temp .= "`n" filepath "\" BaseName datestamp "." Ext
 }
cliptemp := ltrim(temp, "`n")
return

finish:
 {
   stringsplit, saved, saved, `n, `r
   Loop, Parse, cliptemp, `n, `r
    {
      oldname := saved%a_index%
      FileMoveDir, %oldname%, %A_LoopField% , R
      saved%a_index% := ""
    }
 }
return
Confirm? Deny? :?
"My dear Mr Gyrth, I am never more serious than when I am joking."
~Albert Campion
------------------------------------------------------------------------
Website | Demo scripts | Blog | External contact
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

01 Apr 2014, 22:45

I hope this isn't doing your head in as much as it is mine cos i can't make head nor tail of it!!!

OK, sort of success. It's definitely working at some level but I can only get it to work correctly sometimes. It now applies both checkboxes (yay) but, more often than not, not to everything selected (boo). And there doesn't seem to be any rhyme or reason to what it chooses to rename - it's completely random. Strange!!
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

01 Apr 2014, 22:53

"a msgbox at the top of a script and display a var named path it contains nonsense"

I see what you mean. I tried to "empty" the path var with path := "" immediately before the msgbox command but it still contains nonsense.
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

01 Apr 2014, 23:50

Well I've got it to worked consistently for the last 5 minutes or so with no problems. Don't know what could have been going on before but things are looking good now!!! Fingers crossed :-)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Multiple checkboxes

02 Apr 2014, 15:02

kiwichick wrote:"a msgbox at the top of a script and display a var named path it contains nonsense"

I see what you mean. I tried to "empty" the path var with path := "" immediately before the msgbox command but it still contains nonsense.
"Path" is an environment variable. There are many environment variables that are available in the Windows environment that can be accessed by programs running in that environment.

In general #NoEnv should be included in new scripts that do not need to access these environment variables to avoid this problem and also to improve performance. Many of these environment variables can still be accessed in AHK by use of the built in variables that generally start with A_ and all of them can still be accessed by using EnvGet to implicitly retrieve them.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

02 Apr 2014, 18:27

FanaticGuru wrote:"Path" is an environment variable. There are many environment variables that are available in the Windows environment that can be accessed by programs running in that environment.

In general #NoEnv should be included in new scripts that do not need to access these environment variables to avoid this problem and also to improve performance. Many of these environment variables can still be accessed in AHK by use of the built in variables that generally start with A_ and all of them can still be accessed by using EnvGet to implicitly retrieve them.
Oh der, of course!!!! I knew that :-) Knew it, but I haven't consciously used #NoEnv yet so it wasn't in mind - it's usually just part of another script and I don't think anymore about it (or what it's doing) apart from registering that it's there. Thanks for the wakeup, FantastcGuru!!!
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

03 Apr 2014, 17:56

OK so, as per usual, just when I think I can get the next step on my own, I hit a brick wall lol.

How do I assign a variable to StartingPos and Length in the SubStr() function? In my script above I would ultimately use an inputbox to determine the number of characters to remove from the start (or end) of a file/foldername. But newname := SubStr(Name, %startpos%, %length%) doesn't work. I've also tried using StringTrimLeft/Right but that doesn't work either. Or, more likely, I'm not using them correctly :-) I wondered whether using RegExReplace might be the way to go?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Multiple checkboxes

03 Apr 2014, 19:04

kiwichick wrote:But newname := SubStr(Name, %startpos%, %length%) doesn't work.
I have not looked at your script but in the above you do not need the %%.

newname := SubStr(Name, startpos, length)

Knowing when to use the %% and when not is really the trickiest thing to remember in AHK. It is plagued by some legacy issues that make it inconsistent.

I assume AutoHotkey v2 will clean up a lot of these issues. I would prefer that everywhere expect an expression and literal text always needing to be "quoted". It might require a few more keystrokes here and there but would save me a lot of headaches when I remember it wrong.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

03 Apr 2014, 19:30

FanaticGuru wrote:I have not looked at your script but in the above you do not need the %%.
newname := SubStr(Name, startpos, length)
I thought I had tried that but I think I must have been using an alternate version of the script and that's why I couldn't get it go. Thanks heaps!!!
FanaticGuru wrote: Knowing when to use the %% and when not is really the trickiest thing to remember in AHK. I would prefer that everywhere expect an expression and literal text always needing to be "quoted".
Yes!!! It trips me up all the time. Some kind of consistency would be nice (specially for us relative newbies). Fingers crossed for V2 :-)
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Multiple checkboxes

03 Apr 2014, 20:18

kiwichick wrote:
FanaticGuru wrote: Knowing when to use the %% and when not is really the trickiest thing to remember in AHK. I would prefer that everywhere expect an expression and literal text always needing to be "quoted".
Yes!!! It trips me up all the time. Some kind of consistency would be nice (specially for us relative newbies). Fingers crossed for V2 :-)
As a rule though functions() are expecting an expression (ie. a variable or something to be evaluated before it uses it).
So %% are not generally need for variables in functions but "" are needed for literal text.

Commands are expecting a variable where only a variable would make sense like the OutputVariable or InputVariable so no %% needed there.
Everywhere else in a command where either a literal or a variable would make sense it is expecting a literal so no "" are needed and if you want to use a variable you need to use %%.
FileAppend, Another line.`n, C:\My Documents\Test.txt
FileAppend, %NextLine%, %File%

In a command where a literal is expected you can put one % with a space after it to tell the command to evaluate what is coming next.
FileAppend, End of File, % Path "\Test.txt"

There are exceptions, special cases, tricks, etc. but those are general guide lines I use.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
kiwichick
Posts: 139
Joined: 21 Jan 2014, 22:03

Re: Multiple checkboxes

03 Apr 2014, 22:02

Yeah takes a bit of getting the head round :?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, jaka1, RussF and 310 guests