0
votes

I'm trying to make a useful system to automatically increase numbers in selected text. For example, when I working in Zbrush on the project named "Example_character012" I have a folder with 23 files from "Example_character012_prj_01.zpr" to "Example_character012_prj_23.zpr". When I need to save next iteration I'm pressing "ctrl+s" and then regular saving screen opens in the last working directory with preselected filename text, in this scenario it would be "Example_character012_prj_23.zpr". I need to copy this text, select the last number(23) increase it by one, and past it all the text back to the text-field. In this case it would be "Example_character012_prj_24.zpr". This kind of script would be really useful in all kinds of scenario, mainly with saving in different programs. I tried to made it myself and come up with this:

^!Numpad1::
      Sleep 100
      Send ^c
      Sleep 10
      RegExMatch(clipboard,"(\d+)(?!.*\d)",Number)
      NewNumber:=Number1+1
      OldNumber:=NewNumber-1
      Clipboard:=RegExReplace(clipboard, OldNumber, NewNumber)
      Sleep 10
      Send ^v
      Sleep 10
      Send +{Home}

Return

^!Numpad0::
      Sleep 100
      Send ^c
      Sleep 10
      RegExMatch(clipboard,"(\d+)(?!.*\d)",Number)
      NewNumber:=Number1-1
      OldNumber:=NewNumber+1
      Clipboard:=RegExReplace(clipboard, OldNumber, NewNumber)
      Sleep 10
      Send ^v
      Sleep 10
      Send +{Home}

Return

Ctrl+alt+Num1 for increase and Ctrl+alt+Num0 for decrease. It's work fine with in the simplest scenario, but have significant problems I can't solve myself:

  1. It only works in english keyboard-layout. When I use it in russian layout - it puts "cv" instead of pasting(If I paste manually afterwards it put the correct text)
  2. Sometimes it increases not only the last number "Example_character012_prj_01.zpr" changing to "Example_character022_prj_02.zpr" instead of "Example_character012_prj_02.zpr"
  3. Zeros working incorrectly "Example_prj_09.zpr" changing to "Example_prj_010.zpr" instead of "Example_prj_10.zpr"
  4. After pasting it selects the whole line instead of only what just have been pasted
  5. If it's possible, it would be great to make it work without affecting clipboard at all.
  6. Sometimes some keys like "ctrl" just get stuck until being clicked manually for some reason.

Edit: Got some help at Autohotkey forums, current version of script below. Only questions 4 and 5 left.

^!Numpad1::
^!Numpad0::
      Sleep 50
      Send {Ctrl up}^{Ins}
      Sleep 10
      RegExMatch(Clipboard, "^(.*)(?=_)_(\d+)(\....)$", Match)
      Len := StrLen(Match2)
      if InStr(A_ThisHotkey, 1)
            Match2++
      else
            Match2--
      if (StrLen(Match2) < Len)
            Match2 := Format("{:0" Len "}", Match2)
      Clipboard := Match1 "_" Match2 Match3
      Sleep 10
      Send +{Ins}
      Sleep 10
      Send +{Home}
Return
1
I can only help with the regex. Why don't you just use (\d+)\.? That should fix 2. and 3.Fallenhero
Sorry, I forgot to mention, I also want to change names inside the programs itself, so that "Floor2_Object_29" would convert to "Floor2_Object_30". Last number, but without dots and extensions. Pretty much I need the last number in the selected text.user1682929
then maybe this regex? (\d+)(?:[^\d]*$)Fallenhero
Yes, this one seems to work correctly! Thanks!user1682929

1 Answers

0
votes

The reason your CTRL key is misbehaving is maybe because:

If you have a hotkey like

^F12::
    Send {a}

What actually happens when you hit CTRL+F12 is that AHK releases CTRL, sends a, then re-hits CTRL.

This can get more complicated, eg in your scenario.

Try prefixing your Send commands with {Blind} eg Send {Blind}^v to suppress this behavior.

This may or may not work (ie if the hotkey is shift+ctrl+something but the hotkey sends ctrl+v, then what would actually get sent is ctrl+shift+v) but if it does still work, it may make CTRL start behaving itself more.