if (Title contains %exact_match%)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

if (Title contains %exact_match%)

27 Apr 2019, 16:19

Hi there, I've exhausted my search on this one again. I've spent all afternoon on this to no avail.

I have this block of code:

Code: Select all

if Title contains 6.25
msgbox, yes it contains %Title%
else
return
What I need is for the Title to contain *exactly* 6.25
so for example, the line would read something like:

Code: Select all

if Title contains EXACTLY 6.25
msgbox, yes it contains %Title%
else
return
The problem is, there may be another existing Title which is 25, and the messagebox is still appearing, when it I don't want it to. I would like the messagebox to appear only when the title is 6.25
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: if (Title contains %exact_match%)

27 Apr 2019, 17:30

scriptor2016 wrote:
27 Apr 2019, 16:19
Hi scriptor2016,

InStr allows to searches for a given occurrence of a substring in a string, from the left (by default) or the right:

Code: Select all

title := "25"
MsgBox % !!InStr(title, "6.25")
title := "xxx25xxx"
MsgBox % !!InStr(title, "6.25")
title := "6.25"
MsgBox % !!InStr(title, "6.25")
title := "xxx6.25xxx"
MsgBox % !!InStr(title, "6.25")
Hope this helps.
my scripts
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 17:41

thanks! wow this is taxing my brain LOL... please give me a few moments to study this and I will report right back :)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: if (Title contains %exact_match%)

27 Apr 2019, 17:52

- Can't you use in?
- 25 should not match contains 6.25.
- 25 could match contains 6,25 (because it's a comma separated list).
- if (var1 contains var2), with parentheses, is not yet supported in AHK v1/v2, it is effectively if (var1 var3 var2).
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 17:56

Ok, your help is getting me somewhere (wink)

Here is what I have now and it is working:

Code: Select all

winactivate, ahk_class Photoshop
WinGetTitle, Title, A
;if the Title is    MyDocument.jpg @ 6.25% (RGB/8) *    then the messagebox returns 1
;if the Title is    MyDocument.jpg @ 25% (RGB/8) *    then the messagebox returns 0
titleNOW = %Title%

MsgBox % !!InStr(titleNOW, "6.25")
..so that part is now perfect!

My next question:

If the messagebox is 0, how do I carry out the next actions, and the same for if the messaegbox is 1, how do I carry out those next actions?





Kind of like,

IfMessageBox = 1
{
do this
}
else
IfMessagebox=0
{
do that
}
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: if (Title contains %exact_match%)

27 Apr 2019, 18:10

If this is in reference to a window title, you can simply make sure at the time that you're assigning the variable that the title has "6.25" in it. That way, you don't even have to check for it later:

Code: Select all

SetTitleMatchMode, 2  ; Match a string anywhere in the title
WinGetTitle, Title, 6.25  ; Get the title of a window that has "6.25" in it
Last edited by Osprey on 28 Apr 2019, 03:18, edited 1 time in total.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: if (Title contains %exact_match%)

27 Apr 2019, 18:12

InStr, being function returns a value when it is called. Here it is meaningful - it returns the position of an occurrence of the specified subtring in the string being searched.
A function call is specific kind of (sub)expression.
As per the docs (ibid.):
. You can store the result of an expression by means of the := operator.
. In addition, an expression can be used in a parameter that does not directly support it (e.g. the MsgBox's first parameter as demonstrated in my first reply) by
preceding the expression with a percent sign and a space or tab.
. If (expression) (note the parentheses) evaluates an expression and executes the following statement only if the result is true.

Code: Select all

if (!!InStr(titleNOW, "6.25")) {
	; ....
}
; OR
; pseudoBoolean := InStr(titleNOW, "6.25")
boolean := !!InStr(titleNOW, "6.25")
if (boolean) {
	; ....
}
my scripts
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 18:42

The two existing two window titles are:

MyDocument.jpg @ 6.25% (RGB/8) *
MyDocument.jpg @ 25% (RGB/8) *

The messagebox pops up no matter which of the two window titles are active.

Code: Select all

winactivate, ahk_class Photoshop
WinGetTitle, Title, A
titleNOW = %Title%
if (!!InStr(titleNOW, "6.25")) 
{
msgbox, 6.25 is in the Title!
}

else

{
return
}
this is close now, and again thanks to you guys for reviewing this. My brain can't seem to keep track of everything in this one lol. What is wrong in this code?
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 18:54

sorry, hang on a sec here... looks like it might be working


my brain is on overload right now
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 19:23

Okay, I have isolated the problem, but this requires a genius to figure it out (I'm being serious)


if the title is
MyDocument.jpg @ 6.25% (RGB/8) *
then the hotkey 'a' will always return *YES*

if the title is
MyDocument.jpg @ 25% (RGB/8) *
then the hotkey 'a' will always return *NO*

Code: Select all

a::
winactivate, ahk_class Photoshop
WinGetTitle, Title, A
titleNOW = %Title%
if (!!InStr(titleNOW, "6.25")) 
{
msgbox, *YES*, 6.25 is the Title!
}

else

{
msgbox, *NO*, 6.25 is not the Title!
}
..so the above block of code works.



the problem lies somewhere below:

if the title is
MyDocument.jpg @ 6.25% (RGB/8) *
then the hotkey 'b' returns *YES*, but it should return *NO* - this is the problem

if the title is
MyDocument.jpg @ 25% (RGB/8) *
then the hotkey 'b' returns *YES*

Code: Select all

b::
winactivate, ahk_class Photoshop
WinGetTitle, Title, A
titleNOW = %Title%
if (!!InStr(titleNOW, "25")) 
{
msgbox, *YES*, 25 is the Title! ;*THIS* is the problem - it should return *NO*
}

else

{
msgbox, *NO*, 25 is not the Title!
}

..so the error is isolated in the above block of code where I inserted the comment.

This is likely one the most difficult scenarios I've faced and my brain just "scrambles" when trying to sort out this logic. It's probably going to take a genius to figure it out but if someone could take a look at it I would be forever greatful. I will continue to work on this but man, I've already burned most of my day on this one LOL

thanks :)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: if (Title contains %exact_match%)

27 Apr 2019, 19:43

Is this not sufficient?
if InStr(titleNOW, " 25")
Or to be safer, in case the filename contains ' 25':
if InStr(titleNOW, "@ 25% (RGB/8) *")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 19:57

let me try it out, gimme a sec..
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 20:06

I think this might be it:

Code: Select all

a::
winactivate, ahk_class Photoshop
WinGetTitle, Title, A
titleNOW = %Title%
;if (!!InStr(titleNOW, "6.25")) 
if InStr(titleNOW, "@ 6.25% (RGB/8) *")
{
msgbox, *YES*, 6.25 is the Title!
}

else

{
msgbox, *NO*, 6.25 is not the Title!
}
return




b::
winactivate, ahk_class Photoshop
WinGetTitle, Title, A
titleNOW = %Title%
;if (!!InStr(titleNOW, "25")) 
if InStr(titleNOW, "@ 25% (RGB/8) *")
{
msgbox, *YES*, 25 is the Title! ;*THIS* is the problem - it should return *NO*
}

else

{
msgbox, *NO*, 25 is not the Title!
}
I'm not even sure I know what is changed here but it seems to be working properly. Hotkey a and Hotkey b are returning the proper messages now. I'll continue testing it out this evening.


jeeswg - you guys are geniuses. Part of the 'upper echelon' of society who have those high-end IQs. I mean it - in order to understand coding like this requires a seriously intelligent person - I've mentioned Coding as a career path for my young son, but man oh man - you gotta be really smart to understand this stuff. I imagine a programmer can make a serious salary in their life, because this stuff is like a doctor - not for everyone and requires an ultimate degree of intelligence. My brain is sore- and I spent my whole Saturday afternoon on this!! I'm just a hobbyist - so, yeah ;)

---regards :)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: if (Title contains %exact_match%)

27 Apr 2019, 20:40

Btw, to make it more reliable:

Code: Select all

if !hWnd := WinExist("ahk_class Photoshop")
	return
WinActivate, % "ahk_id " hWnd
WinGetTitle, Title, % "ahk_id " hWnd
If there is a matching window, it is activated, and its title is retrieved. Otherwise, a return is done.

Re. your other point hehe:
"Uncle" Bob Martin - "The Future of Programming" - YouTube
https://www.youtube.com/watch?v=ecIWPzGEbFc#t=57m11
code is: assignment statements, if statements and while loops
Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 21:23

I'm watching the youtube link right now. Just amazing. I wish I had went into this line of work after high school, I'm sure I might have had a chance :(


Oh well, maybe my kids can go into it, it'd make me proud :)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: if (Title contains %exact_match%)

27 Apr 2019, 21:36

OT
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 22:08

regarding your last point, I also enjoy this forum - the users here are incredibly friendly an helpful. Never has a request of mine gone unreplied to. Each and every time, somebody is here to help, and almost always instantly.

Sometimes I hesitate to post here because I think I'm asking too many questions, but rest assured I do my research before asking anything. If I feel like I've exhausted my search, then I will ask for assistance. I also feel like I would like to help other users more often, but my knowledge level quite frankly isn't that high and I probably cant offer the best advice in comparison to the veterans such as yourself - you guys can offer the best advice, so it's best left to you.

Anyhow, I appreciate this forum a lot, it really is a welcoming place and we're lucky to have it :)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: if (Title contains %exact_match%)

27 Apr 2019, 22:08

@topic Whilst taking into account Osprey and jeeswg suggestions and since your code assumes that ahk_class Photoshop exists the moment you press the hotkey, you could do this:

Code: Select all

a::
SetTitleMatchMode, 2
if (WinExist("ahk_class Photoshop", "@ 25% (RGB/8) *")) {
	WinActivate
	; ...
} else {
	WinActivate, ahk_class Photoshop
	; ...
}
return
@offtopic It is not primarily a question of intelligence; the 'brain' has very little to do with the question, I think; genius code like in a state of grace - it is a question of culture: as I see it, being able to adress, reformulate problem through an algorithmic mindset (that espouses the language of computers: 'assignment statements, if statements and while loops' as quoted by jeeswg) concretly being formulated, implemented through a specific programming language. One of the key words of culture is: 'absence'; coding, is also a specific culture of the abstraction. This is only in this sense that is not, at the root, for everyone like you said - like me and, maybe, you scriptor2016.
Hopefully your kids can go into it, ask the question: to be or not to be coder? hopefully it can thrive in coding if so - and also, maybe, whether it is a representative of the male gender or not...
my scripts
scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: if (Title contains %exact_match%)

27 Apr 2019, 22:32

I just think that programmers/coding who do this for a living and have been doing it for decades are a very small percentage of the population - and by far, the most intelligent portion of the population - even more so than say, a Doctor or a Lawyer for example. I think you guys are on a much higher level. It's not just memorizing all the endless little typos and order of operations depending on the language, but also being able to imagine/visualize the entire project before you lay down the first character in the script.

It's like, you look at your smart phones - and you ask, who the heck programmed all this stuff? And how? It's you guys. And it boggles my mind to no end; I am in awe when I think about it. Try showing some code to 1,000 people and only a few of them will have any idea what it is.

There are countless times I've worked for days on an AHK script, only to realize I should have done it a different way from the start. I just can't imagine the whole project, I have to go step-by step and trial-and-error it. I am definitely not cut out to be a pro by any means, but as a simple hobbyist who enjoys working primarily with easy GUI creations and macro shortcuts, I am so happy to have found this software and this forum :)
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: if (Title contains %exact_match%)

28 Apr 2019, 03:48

scriptor2016 wrote:
27 Apr 2019, 22:32
I just think that programmers/coding who do this for a living and have been doing it for decades are a very small percentage of the population - and by far, the most intelligent portion of the population - even more so than say, a Doctor or a Lawyer for example. I think you guys are on a much higher level. It's not just memorizing all the endless little typos and order of operations depending on the language, but also being able to imagine/visualize the entire project before you lay down the first character in the script.
The compliments are appreciated, but I think that you give us a little too much credit. I wish that I could visualize an entire project at the very start. That isn't how I operate at all. I just start scripting and add things as they come to me. For example, I have a project that I've been working on for months and am almost done with that is far more elaborate than I could've imagined when I started. In fact, if I had imagined it, I might've been too intimidated to have even started it.

Even in the professional world, programmers usually don't visualize the whole project beforehand. That's the job of project managers, who then task the programmers with writing the code to carry out their designs, and the programmers often don't work on a whole project, just a portion of it (like the user interface portion or the driver portion).

I hate to generalize, but programmers generally like to dive right in and start coding. We tend to be a trial and error bunch. Doctors and lawyers don't have that luxury (because an error could kill a person or cause an injustice). I could never do what they do because I'm not that patient and I screw up dozens of times for every time that I code something that actually works. My brother is an eye doctor, but I hate to imagine how many people I would blind if I went into that line of work. With code, nothing gets hurt when I screw up... except my pride ;).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, hugojans and 93 guests