It is currently March 28th, 2024, 5:40 pm

RegExp Substitution with InputText

Get help with creating, editing & fixing problems with skins
amein14
Posts: 7
Joined: December 2nd, 2017, 6:55 am

RegExp Substitution with InputText

Post by amein14 »

Hello, Im doing book ISBN search bar based on this project (https://jsmorley.deviantart.com/art/GoogleIt-1-0-608009041). My question is there anyway to trim first number from UserInput because im using Barcode scanner which automatically add number "2" infront of the barcode.
For example ISBN number for book below
Actual ISBN: 9781292152103
After barcode scanned into UserInput: 29781292152103

In order to search a valid IBSN number, search bar need to remove number 2 before doing search job. Found some info regarding this on ( https://www.reddit.com/r/Rainmeter/comments/61o4bs/is_there_any_way_to_trim_strings/ ) but failed to integrate into my code. Help much appreciate.

I try to use regex as below, no luck

Code: Select all

[MeasureInput]
Measure=Plugin
Plugin=InputText.dll
X=74
Y=38
W=260
H=26
FontColor=255,255,255,255
SolidColor=30,30,30,255
FontFace=Segoe UI
FontSize=11
AntiAlias=1
FocusDismiss=1
Command1=["https://www.google.com/search?q=$UserInput$#SearchCat#"]
RegExpSubstitute=1 
Substitute=":%s/^.\{0,1\}//"
;Substitute=":%s/^.//"
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RegExp Substitution with InputText

Post by balala »

First of all, the posted Substitute option isn't used correctly. A such option should have the following form: Substitute="TO-SUBSTITUTE":"WITH". With this option, the TO-SUBSTITUTE string will be substituted with WITH. There must two pairs of quote marks: you have to include both strings into one pair.
A second problem is that this way the Substitute option won't work with the input string. For this you should have to transfer the input to a string measure and make there the needed substitution. Something like this:

Code: Select all

[MeasureInput]
Measure=Plugin
Plugin=InputText.dll
X=74
Y=38
W=260
H=26
FontColor=255,255,255,255
SolidColor=30,30,30,255
FontFace=Segoe UI
FontSize=11
AntiAlias=1
FocusDismiss=1
Command1=[!SetVariable ISBN "$UserInput$"][!UpdateMeasure "MeasureString"]

[MeasureString]
Measure=String
String=#ISBN#
RegExpSubstitute=1
Substitute="^\d{1}(\d*)$":"\1"
DynamicVariables=1
OnUpdateAction=["https://www.google.com/search?q=[MeasureString]#SearchCat#"]
UpdateDivider=-1
The UpdateDivider=-1 option of the [MeasureString] measure let the measure to be updated, just when you enter a value through the InputText plugin. In this case, the measure (through the Substitte option) removes the first digit of the input value and uses the rest to make the desired search.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: RegExp Substitution with InputText

Post by SilverAzide »

Perhaps another question to consider is why a "2" is getting prepended to the data? Is the "2" part of the barcode itself? Or is the "2" being prepended by the scanner? Some scanners can be configured to prepend or append data, so perhaps the scanner is the problem and you are fixing the wrong thing (i.e., fixing your skin instead of the scanner config). If the "2" is embedded in the barcode, then fixing your skin using balala's code is the only work-around.
amein14
Posts: 7
Joined: December 2nd, 2017, 6:55 am

Re: RegExp Substitution with InputText

Post by amein14 »

balala wrote:First of all, the posted Substitute option isn't used correctly.
A such option should have the following form: Substitute="TO-SUBSTITUTE":"WITH". With this option, the TO-SUBSTITUTE string will be substituted with WITH. There must two pairs of quote marks: you have to include both strings into one pair.
A second problem is that this way the Substitute option won't work with the input string. For this you should have to transfer the input to a string measure and make there the needed substitution. Something like this:

Code: Select all

[MeasureInput]
Measure=Plugin
Plugin=InputText.dll
X=74
Y=38
W=260
H=26
FontColor=255,255,255,255
SolidColor=30,30,30,255
FontFace=Segoe UI
FontSize=11
AntiAlias=1
FocusDismiss=1
Command1=[!SetVariable ISBN "$UserInput$"][!UpdateMeasure "MeasureString"]

[MeasureString]
Measure=String
String=#ISBN#
RegExpSubstitute=1
Substitute="^\d{1}(\d*)$":"\1"
DynamicVariables=1
OnUpdateAction=["https://www.google.com/search?q=[MeasureString]#SearchCat#"]
UpdateDivider=-1
The UpdateDivider=-1 option of the [MeasureString] measure let the measure to be updated, just when you enter a value through the InputText plugin. In this case, the measure (through the Substitte option) removes the first digit of the input value and uses the rest to make the desired search
.
Thank you so much balala for your reply. Really appreciate it. Script given by you working great!

Out of curiosity, I would like to ask you. Why after I do skin refresh, this link (https://www.google.com/webhp#ISBN) automatically open? WHich part of the code triggered this action?
amein14
Posts: 7
Joined: December 2nd, 2017, 6:55 am

Re: RegExp Substitution with InputText

Post by amein14 »

SilverAzide wrote:Perhaps another question to consider is why a "2" is getting prepended to the data? Is the "2" part of the barcode itself? Or is the "2" being prepended by the scanner? Some scanners can be configured to prepend or append data, so perhaps the scanner is the problem and you are fixing the wrong thing (i.e., fixing your skin instead of the scanner config). If the "2" is embedded in the barcode, then fixing your skin using balala's code is the only work-around.
Yes, you are correct. The barcode scanner being configured to automatically add prefix "2" for every barcode scanned.
This is to differentiate our 3 campus. Number 1 2 and 3 used as prefix. I cannot remove this prefix from barcode scanner because we will using this daily basis. ;-)
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RegExp Substitution with InputText

Post by balala »

amein14 wrote:Why after I do skin refresh, this link (https://www.google.com/webhp#ISBN) automatically open? WHich part of the code triggered this action?
I don't know. It depends on what else do you have in that code. You didn't post the whole code, so I can't tell. Post it to can take a look.
amein14
Posts: 7
Joined: December 2nd, 2017, 6:55 am

Re: RegExp Substitution with InputText

Post by amein14 »

balala wrote:I don't know. It depends on what else do you have in that code. You didn't post the whole code, so I can't tell. Post it to can take a look.
Here my full code.. When refresh the skin, this link will automatically open from web browser

Code: Select all

http://webpac.kdu.edu.my/search/query?match_1=MUST&term_1&theme=kdu#ISBN

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Metadata]
Name=Search
Author=amein
Version=Dec17
Information=Book search

[Variables]
SearchCat=""
TextOff=255,255,255
TextOrange=255,128,10
GoogleBlue=150,150,150

[MeterSkinBack]
Meter=Image
W=300
H=65
SolidColor=0,0,0,100

[MeterLogo]
Meter=Image
X=5
Y=5
ImageName=#@#Images\cutmypic.png

[MeterInputBack]
Meter=Shape
X=5
Y=30
Shape=Rectangle 0,0,290,30,5 | Extend MyModifiers
MyModifiers=Fill Color 30,30,30,200 | StrokeWidth 0 | Stroke Color 30,30,30,200
LeftMouseUpAction=[!CommandMeasure MeasureInput "ExecuteBatch 1"]

[MeasureInput]
Measure=Plugin
Plugin=InputText.dll
X=7
Y=35
W=280
H=23
DefaultValue=""
FontColor=255,255,255,255
SolidColor=30,30,30,255
FontFace=Segoe UI
FontSize=10
AntiAlias=1
FocusDismiss=1
Command1=[!SetVariable ISBN "$UserInput$"][!UpdateMeasure "MeasureString"]

[MeasureString]
Measure=String
String=#ISBN#
RegExpSubstitute=1
Substitute="^\d{1}(\d*)$":"\1"
DynamicVariables=1
OnUpdateAction=["http://webpac.kdu.edu.my/search/query?match_1=MUST&term_1=[MeasureString]#SearchCat#"]
UpdateDivider=-1

[MeterLabel]
Meter=String
X=10
Y=36
FontFace=Product Sans
FontSize=12
Text=Search.....
InlineSetting=Color | #GoogleBlue#
InlinePattern=Search.....
AntiAlias=1


[MeterTitle]
Meter=String
Group=Category
X=40
Y=6
FontFace=Segoe UI
FontSize=11
FontColor=#TextOff#
Text=TITLE
;InlineSetting=.
;InlinePattern=.*
;InlineSetting2=Typography | smcp
;InlinePattern2=.*
;InlineSetting3=Weight | 500
;InlinePattern=.*
AntiAlias=1
DynamicVariables=1
LeftMouseUpAction=[!SetOptionGroup Category FontColor "#*TextOff*#"][!SetOption #CURRENTSECTION# FontColor "#*TextOrange*#"][!SetVariable SearchCat "&field_1=t&facet_loc=10000&sort=relevance&theme=kdu"][!UpdateMeter *][!Redraw]

[MeterISBN]
Meter=String
Group=Category
X=16R
Y=6
FontFace=Segoe UI
FontSize=11
FontColor=#TextOff#
Text=ISBN
;InlineSetting=
;InlinePattern=.*
;InlineSetting2=Typography | smcp
;InlinePattern2=.*
;InlineSetting3=Weight | 500
;InlinePattern=.*
AntiAlias=1
DynamicVariables=1
LeftMouseUpAction=[!SetOptionGroup Category FontColor "#*TextOff*#"][!SetOption #CURRENTSECTION# FontColor "#*TextOrange*#"][!SetVariable SearchCat "&field_1=isbn&facet_loc=10000&sort=relevance&theme=kdu"][!UpdateMeter *][!Redraw]

Another question if possible, could you help me which part of this code need to edit so that when click, text will become "bold". Now only text change color to orange i want to add bold too. Possible to make the text change to bold too?

Code: Select all

[MeterTitle]
Meter=String
Group=Category
X=40
Y=6
FontFace=Segoe UI
FontSize=11
FontColor=#TextOff#
Text=TITLE
;InlineSetting=.
;InlinePattern=.*
;InlineSetting2=Typography | smcp
;InlinePattern2=.*
;InlineSetting3=Weight | 500
;InlinePattern=.*
AntiAlias=1
DynamicVariables=1
LeftMouseUpAction=[!SetOptionGroup Category FontColor "#*TextOff*#"][!SetOption #CURRENTSECTION# FontColor "#*TextOrange*#"][!SetVariable SearchCat "&field_1=t&facet_loc=10000&sort=relevance&theme=kdu"][!UpdateMeter *][!Redraw]
Thank you so much in advance for you help. ;-)
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RegExp Substitution with InputText

Post by balala »

amein14 wrote:Why after I do skin refresh, this link (https://www.google.com/webhp#ISBN) automatically open? WHich part of the code triggered this action?
This is caused by the OnUpdateAction option of the [MeasureString] measure. When the skin is loaded or refreshed, this OnUpdateAction is executed, opening the link. Would you like to avoid this?
amein14 wrote:Another question if possible, could you help me which part of this code need to edit so that when click, text will become "bold". Now only text change color to orange i want to add bold too. Possible to make the text change to bold too?
Beside the existing bangs, add the following one to the LeftMouseUpAction option of the meter: [!SetOption #CURRENTSECTION# StringStyle "BOLD"]. This bang will set the bold style to the meter itself, however, the string won't ever be set back to normal (not bold).
Another possibility would be to set it bold when you push down the button of the mouse, then set it back to normal, when the button is released. Are you interested into something like this?
amein14
Posts: 7
Joined: December 2nd, 2017, 6:55 am

Re: RegExp Substitution with InputText

Post by amein14 »

balala wrote:This is caused by the OnUpdateAction option of the [MeasureString] measure. When the skin is loaded or refreshed, this OnUpdateAction is executed, opening the link. Would you like to avoid this?
Yes, if possible. Because each time computer turn on, that link will open automatically too.
balala wrote:Beside the existing bangs, add the following one to the LeftMouseUpAction option of the meter: [!SetOption #CURRENTSECTION# StringStyle "BOLD"]. This bang will set the bold style to the meter itself, however, the string won't ever be set back to normal (not bold).
Another possibility would be to set it bold when you push down the button of the mouse, then set it back to normal, when the button is released. Are you interested into something like this?
Yes, because im looking something to highlight "selected meter" and differentiate with others. Glad if you could help regarding this. Thank you in advance..
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RegExp Substitution with InputText

Post by balala »

amein14 wrote:Yes, if possible. Because each time computer turn on, that link will open automatically too.
It is, I think. I'd remove the mentioned OnUpdateAction option from the [MeasureString] measure and would complete the Command1 option of the [MeasureInput] measure with a !SetOption bang to set back the same OnUpdateAction option to the [MeasureString] measure when the string is input (see the !SetOption bang below):

Code: Select all

[MeasureInput]
...
Command1=[!SetVariable ISBN "$UserInput$"][!SetOption MeasureString OnUpdateAction """["http://webpac.kdu.edu.my/search/query?match_1=MUST&term_1=[MeasureString]#SearchCat#"]"""][!UpdateMeasure "MeasureString"]
amein14 wrote:Yes, because im looking something to highlight "selected meter" and differentiate with others. Glad if you could help regarding this. Thank you in advance..
Add the following LeftMouseDownAction option to the [MeterTitle] meter: LeftMouseDownAction=[!SetOption #CURRENTSECTION# StringStyle "Bold"][!UpdateMeter "#CURRENTSECTION#"][!Redraw]. The !SetOption bang of this option sets the Bold style to the meter when you're pushing the button. When the button is released, the same Normal style should be set back. For this you have to add a similar bang to the LeftMouseUpAction option of the same meter (beside the existing ones): LeftMouseUpAction=[!SetOptionGroup Category FontColor "#*TextOff*#"][color=#FF0000][!SetOption #CURRENTSECTION# StringStyle "Normal"][/color][!SetOption #CURRENTSECTION# FontColor "#*TextOrange*#"][!SetVariable SearchCat "&field_1=t&facet_loc=10000&sort=relevance&theme=kdu"][!UpdateMeter *][!Redraw].
Or there also is a third possibility, too: to set the string bold when you're hovering the mouse over it, then set it back to normal, when you you're leaving it. Just have to add the appropriate bangs to a newly added MouseOverAction and MouseLeaveAction options. If you can't do this and you're interested, please let me know, to help you.
Post Reply