Remove characters from string

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

Remove characters from string

Post by scriptor2016 » 04 Dec 2021, 23:46

Hi everyone

Winter's back... which means I'm gonna be indoors for a good 4 or 5 months. Which means I'm excited to brush off the rust and spend some more time scripting with AHK (yay)..and of course try and re-familiarise myself with everything haha.

Which brings me to my question.

I'm trying to remove a number of characters from a string. And I was able to do this earlier, but for some reason I can't get it going any more. Here's part of the code:

Code: Select all

winactivate, ahk_class Photoshop
winwaitactive, ahk_class Photoshop
wingettitle, title, A

;at this point, %title% will be something like      Untitled-1 @ 24% (Layer 1, RGB/8) *
what I'm trying to do is end up with this:

Untitled-1

So basically I need to get rid of the @ and everything after it, as well as the empty space before it.

So, I tried all of this:

Code: Select all

winactivate, ahk_class Photoshop
winwaitactive, ahk_class Photoshop
wingettitle, title, A

title_trimmed=
string = %title%
Loop, parse, string
{
If ( A_LoopField = "@")
{
Break
}
}
title_trimmed .= A_LoopField

;next, remove the resulting trailing empty space which was left over in the trimmed_title variable:
vText = %title_trimmed%
result := % "" RTrim(vText) "" ;this trims the ending empty spaces only
title_trimmed=%result%
some of this is snippets I've saved to my hard drive over the years and I've tailored it to what I need. But of course, title_trimmed is returning no results.

Would anyone happen to have any ideas how to tweak this? Thanks in advance

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Remove characters from string

Post by flyingDman » 04 Dec 2021, 23:57

What about this?:

Code: Select all

var := "Untitled-1 @ 24% (Layer 1, RGB/8) *"
msgbox % strsplit(var, " ").1
14.3 & 1.3.7

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 00:01

thanks flyingD..

problem is, I think I might need to go something like this:

Code: Select all

var := title ;I don't know the title, the only way to get it is using [b]wingettitle, title[/b] 
msgbox % strsplit(var, " ").1
..I hope that makes sense. I'm not able write out the title name because the only way for me to get it is via wingettitle

I hope I am making sense... ;-p

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 00:09

so I've narrowed it down a bit:

Code: Select all

winactivate, ahk_class Photoshop
winwaitactive, ahk_class Photoshop
wingettitle, title, A

;now get rid of everything in the title after and including the @
Loop, parse, %title%
{
If ( A_LoopField = "@")
{
Break
}
}
msgbox, title is now %title%
..but the script is now throwing an error message:

Error: The following variable name contains an illegal character:
"Untitled-1 @ 24% (Layer 1, RGB/8) *"


..and the error is occurring on the following line:
Loop, parse, %title%

gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Remove characters from string

Post by gregster » 05 Dec 2021, 00:13

https://www.autohotkey.com/docs/commands/LoopParse.htm wrote:InputVar

The name of the variable whose contents will be analyzed. Do not enclose the name in percent signs unless you want the contents of the variable to be used as the name.

[v1.1.21+]: This parameter can be an % expression, but the percent-space prefix must be used.

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 00:17

thanks again... here it is updated:

Code: Select all

winactivate, ahk_class Photoshop
winwaitactive, ahk_class Photoshop
wingettitle, title, A

msgbox, title is %title% ;this is indeed capturing the title 
;now get rid of everything in the title after and including the @
Loop, parse, title
{
If ( A_LoopField = "@")
{
Break
}
}
msgbox, title is now %title%
but strange enough there are still no results. I had this working earlier today, too... but I messed around with my code all afternoon and screwed it up somehow lol... I don't have enough undo's to get back to where it was working :(

I'm studying this line-for-line and can't for the life of me figure out why it isn't working

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 00:32

alright I think I got it:

Code: Select all

winactivate, ahk_class Photoshop
winwaitactive, ahk_class Photoshop
wingettitle, title, A

;now get rid of everything in the title after and including the @
title_trimmed=
string = %title%
Loop, parse, string
{
If (A_LoopField = "@")
{
Break
}
title_trimmed .= A_LoopField

;next, remove the resulting trailing empty space which was left over in the trimmed_title variable:
vText = %title_trimmed%
result := % "" RTrim(vText) "" ;this trims the ending empty spaces only
title_trimmed=%result%
}

msgbox result is %result%
..and this seems to do the trick. But it seems like a roundabout way to do it..

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Remove characters from string

Post by flyingDman » 05 Dec 2021, 01:06

One more time. Try to run this: (assuming that title is the result of wingettitle, title, A

Code: Select all

msgbox % strsplit(title, " ").1
14.3 & 1.3.7

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 01:18

Updated code:

Code: Select all

winactivate, ahk_class Photoshop
winwaitactive, ahk_class Photoshop
wingettitle, title, A ;the full title is:   Untitled-1 @ 25% (RGB/8) *

msgbox % strsplit(title, " ").1
and the messagebox is returning:
Untitled-1

so yes- it looks like that is working - excellent, thank you :) :)

question - is it looking for the first instance of an empty space, and then deleting everything after it (including the empty space itself)?

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Remove characters from string

Post by boiler » 05 Dec 2021, 02:38

scriptor2016 wrote: question - is it looking for the first instance of an empty space, and then deleting everything after it (including the empty space itself)?
It's not really deleting it. It's creating an array elements by breaking the string every place a space occurs, and the spaces aren't included in the results. Then he's showing the first element of the resulting array.

I think it would be better to split the string on a space followed by the @ character, because if the title part before the @ contains a space, you won't get the whole thing. So the slight change would be:

Code: Select all

title := "My image @ 24% (Layer 1, RGB/8) *"
msgbox % strsplit(title, " @").1
Result is My image instead of My.

Of course, if it's not possible for it to contain a space, then you wouldn't have to worry about it, but it seems that it would be possible, even if you wouldn't normally include a space by practice.

In case your title could include @ characters, then you need to capture everything up to the last one that appears, like this:

Code: Select all

title := "My summer @ camp @ 24% (Layer 1, RGB/8) *"
MsgBox, % RegExReplace(title, ".*\K @.*")
Result is My summer @ camp.

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 03:32

ok thanks to you both - let me digest this for now. I'll include whichever approach is best for the code I'm working on... I've been at this all day so will call it a night for now and will check back in tomorrow... have a nice evening :)

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 16:52

Code: Select all

title := "My summer @ camp @ 24% (Layer 1, RGB/8) *"
MsgBox, % RegExReplace(title, ".*\K @.*")
alright perfect it looks like this one will be good.

question again, is it possible to replace the @ with a . and have it still work? something like this:

Code: Select all

title := "My summer.camp @ 24% (Layer 1, RGB/8) *"
..so basically now it would get rid of the . and everything after it


oh and also, how do we place a character in a small box when posting a message here?

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: Remove characters from string

Post by boiler » 05 Dec 2021, 21:49

scriptor2016 wrote: question again, is it possible to replace the @ with a . and have it still work? something like this:

Code: Select all

title := "My summer.camp @ 24% (Layer 1, RGB/8) *"
..so basically now it would get rid of the . and everything after it
Yes, this removes everything following the last . that appears and the . itself:

Code: Select all

title := "1. My summer.camp @ 24% (Layer 1, RGB/8) *"
MsgBox, % RegExReplace(title, ".*\K\..*")

scriptor2016 wrote: oh and also, how do we place a character in a small box when posting a message here?
Highlight the character(s) and click the c button above the area where you type replies.

scriptor2016
Posts: 844
Joined: 21 Dec 2015, 02:34

Re: Remove characters from string

Post by scriptor2016 » 05 Dec 2021, 22:01

excellent thanks again guys, all of this code will come in handy for what I need. I've been able to tweak it as well when other circumstances arise.

and it's nice to know how to enclose a character in a box as well !

Post Reply

Return to “Ask for Help (v1)”