It is currently March 29th, 2024, 7:35 am

2 variables in 1 .lua bang

General topics related to Rainmeter.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

2 variables in 1 .lua bang

Post by kyriakos876 »

Hello, I'm working on a skin that opens a link based on users inputs.

Code: Select all

function Search()
   local SearchInput = SKIN:GetVariable('SearchInput')
   local Category = SKIN:GetVariable('Category')
   SKIN:Bang('"https://www.facebook.com/search/'..Category..'/?q='..SearchInput..'"')
end
This script is called when the user clicks on the "SearchBox" meter I have and it is made in order to prevent the Spacebar on the URL. (Don't stay here, there might be another way to prevent the Spacebar, but I also want to ad a "+" character later on)
So if there user types Nick Madel the URL will run with "Nick Madel" Instead of "Nick". I hope you got the purpose of the script.
The "Category" Variable is set not through an input so don't stay there either.
The script above works fine if I have only one Variable at the time on the URL ex.

Code: Select all

function Search()
   local SearchInput = SKIN:GetVariable('SearchInput')
   local Category = SKIN:GetVariable('Category')
   SKIN:Bang('"https://www.facebook.com/search/top/?q='..SearchInput..'"')
end
or

Code: Select all

function Search()
   local SearchInput = SKIN:GetVariable('SearchInput')
   local Category = SKIN:GetVariable('Category')
   SKIN:Bang('"https://www.facebook.com/search/'..Category..'/?q=Nick Madel"')
end
And I was wondering, what am I doing wrong? How can I make it work using both variables at the same URL?
I can always create as many scripts as the categories and have only the search input as variable, but that's lame, ugly and other bad things :P
Thanks for your time!
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: 2 variables in 1 .lua bang

Post by balala »

kyriakos876 wrote:This script is called when the user clicks on the "SearchBox" meter
Probably not when the user clicks on the appropriate search box, better I think when he types a string, then hit enter. I suppose...
kyriakos876 wrote:it is made in order to prevent the Spacebar on the URL. (Don't stay here, there might be another way to prevent the Spacebar, but I also want to ad a "+" character later on)
I'm not sure because I don't know the whole code, but I'd say maybe it could work even without a lua script. I'd try to use some (regular expression) substitution.
Could you post the code of the skin along with the whole code of the lua script?
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: 2 variables in 1 .lua bang

Post by kyriakos876 »

balala wrote:Probably not when the user clicks on the appropriate search box, better I think when he types a string, then hit enter. I suppose...
No no I mean called not executed :D Check below for the skin ^_^
balala wrote: I'm not sure because I don't know the whole code, but I'd say maybe it could work even without a lua script. I'd try to use some (regular expression) substitution.
Could you post the code of the skin along with the whole code of the lua script?
Here is the skin (there are many unnecessary things included, because of reasons :P but try to focus on what I asked if you will)
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: 2 variables in 1 .lua bang

Post by balala »

Try the following: SKIN:Bang('"https://www.facebook.com/search/'..Category..'/?q='..SearchInput..'"').
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: 2 variables in 1 .lua bang

Post by kyriakos876 »

balala wrote:Try the following: SKIN:Bang('"https://www.facebook.com/search/'..Category..'/?q='..SearchInput..'"').
But this is my problem :P If I use that as the bang, what's going to happen is open the URL as:

https://www.facebook.com/search/str/(InputedValue)/keywords_top

What I want is (If say, Category=pages, for example) to get this:

https://www.facebook.com/search/pages/?q=(InputedValue)

And again, this works fine as long as I have only one variable in the URL, either "Category" or "SearchInput" but when using both it simply won't work. Let me know if you find a way around, or if I'm doing something wrong, or not doing something at all. :D
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: 2 variables in 1 .lua bang

Post by balala »

kyriakos876 wrote:But this is my problem :P If I use that as the bang, what's going to happen is open the URL as:

https://www.facebook.com/search/str/(InputedValue)/keywords_top

What I want is (If say, Category=pages, for example) to get this:

https://www.facebook.com/search/pages/?q=(InputedValue)

And again, this works fine as long as I have only one variable in the URL, either "Category" or "SearchInput" but when using both it simply won't work. Let me know if you find a way around, or if I'm doing something wrong, or not doing something at all. :D
None of the posted links doesn't works.
However, the last bang I've posted above works well for me. Here is the whole code of my Search.lua:

Code: Select all

function Search()
	local SearchInput = SKIN:GetVariable('SearchInput')
	local Category = SKIN:GetVariable('Category')
	SearchInput = SearchInput:gsub('+', '%%2B')
	SKIN:Bang('"https://www.facebook.com/search/'..Category..'/?q='..SearchInput..'"')
end
The skin makes the appropriate search, looking for videos, images, people (or whatever), using the entered keywords.
It's weird if there is not working!
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: 2 variables in 1 .lua bang

Post by kyriakos876 »

balala wrote:None of the posted links doesn't works.
However, the last bang I've posted above works well for me. Here is the whole code of my Search.lua:

Code: Select all

function Search()
	local SearchInput = SKIN:GetVariable('SearchInput')
	local Category = SKIN:GetVariable('Category')
	SearchInput = SearchInput:gsub('+', '%%2B')
	SKIN:Bang('"https://www.facebook.com/search/'..Category..'/?q='..SearchInput..'"')
end
The skin makes the appropriate search, looking for videos, images, people (or whatever), using the entered keywords.
It's weird if there is not working!
The links I posted are examples, not actual links. But this is weird... I tried again in case I had something misspelled in my script by copy-pasting your code but the link still won't work.... Here is an actual link if I put as input "Nick" and category "pages":

https://www.facebook.com/search/str/Nick/keywords_top

Which again isn't the desired one.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: 2 variables in 1 .lua bang

Post by kyriakos876 »

Ok that was stupid... It turns out my default browser (Vivaldi) won't open the link correctly for some reason I tried it with Chrome and it worked... Sorry for wasting your time :???:
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: 2 variables in 1 .lua bang

Post by balala »

kyriakos876 wrote:Ok that was stupid... It turns out my default browser (Vivaldi) won't open the link correctly for some reason I tried it with Chrome and it worked... Sorry for wasting your time :???:
No problem, I'm glad if you figured out what the problem was.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: 2 variables in 1 .lua bang

Post by balala »

As I said in my first reply, a such skin could work even without a lua script. Here is a such solution. I removed the [SearchScript] script measure, which isn't needed any more.
Beside this, I also added the following measure:

Code: Select all

[MeasureSearch]
Measure=String
String=#SearchInput#
RegExpSubstitute=1
Substitute="\s+":"+"
DynamicVariables=1
OnChangeAction=["https://www.facebook.com/search/#Category#/?q=[MeasureSearch]"]
When this measure is updated AND its value is changing, it'll open the set URL, in the default browser. In this URL I've used the Category variable (which is set by some !SetVariable bangs, see below) and the value returned by the [MeasureSearch] measure itself. This value is similar with the string typed into the search box, but the spaces are replaced by + sign(s).
Now replace the Command1 option of the [WhereToSearch] measure, to not run the nonexistent script measure, but to update instead the [MeasureSerach] measure: Command1=[!SetVariable "SearchInput" "$UserInput$"][!UpdateMeasure "MeasureSearch"]. This way when you enter a string into the search box and hit enter, the !SetVariable bang will set the value of the SearchInput variable, which will be taken by the [MeasureSearch] measure, the appropriate substitutions will be made and the updated URL will be executed.
Now one more: refreshing a skin is a usually undesired operation: https://forum.rainmeter.net/viewtopic.php?p=119966#p119966
You've used !WriteKeyValue bangs, along with !Refresh bangs in each LeftMouseUpAction option of the [Top], [Pages], [People], [Photos], [Videos] and [Groups] meters. I also rewrote these options:

Code: Select all

[Top]
...
LeftMouseUpAction=[!SetVariable SelectedXA "60"][!SetVariable SelectedXB "19"][!SetVariable SelectedYA "116"][!SetVariable SelectedYB "19"][!SetVariable Category "top"][!UpdateMeter "Selection"][!Redraw]

[Pages]
...
LeftMouseUpAction=[!SetVariable SelectedXA "116"][!SetVariable SelectedXB "19"][!SetVariable SelectedYA "172"][!SetVariable SelectedYB "19"][!SetVariable Category "pages"][!UpdateMeter "Selection"][!Redraw]

[People]
...
LeftMouseUpAction=[!SetVariable SelectedXA "172"][!SetVariable SelectedXB "19"][!SetVariable SelectedYA "228"][!SetVariable SelectedYB "19"][!SetVariable Category "people"][!UpdateMeter "Selection"][!Redraw]

[Photos]
...
LeftMouseUpAction=[!SetVariable SelectedXA "60"][!SetVariable SelectedXB "73"][!SetVariable SelectedYA "116"][!SetVariable SelectedYB "73"][!SetVariable Category "photos"][!UpdateMeter "Selection"][!Redraw]

[Videos]
...
LeftMouseUpAction=[!SetVariable SelectedXA "116"][!SetVariable SelectedXB "73"][!SetVariable SelectedYA "172"][!SetVariable SelectedYB "73"][!SetVariable Category "videos"][!UpdateMeter "Selection"][!Redraw]

[Groups]
...
LeftMouseUpAction=[!SetVariable SelectedXA "172"][!SetVariable SelectedXB "73"][!SetVariable SelectedYA "228"][!SetVariable SelectedYB "73"][!SetVariable Category "groups"][!UpdateMeter "Selection"][!Redraw]
This way there's no need to always refresh the skin, when you click a new search category. But this solution will work, only if you add the DynamicVariables=1 option to the [Selection] meter, where the variables are used.
If you want to also write the newly set values of the variables into the @Resources\Variables.ini file, you just have to add back the appropriate !WriteKeyValue bangs, to each of the above LeftMouseUpAction options, without the [!Refresh] bangs!
Obviously if you follow these changes, you also can remove the Search.lua file from the config, because it's not needed any more.