0

How's everyone doing?

So, if I run xdotool getactivewindow getwindowname, it gives the full title of the current window, for example:

fish home/kibe/Documents — Konsole

Blablablabla - Stack Overflow - Google Chrome

The thing is, I only want the application name (Konsole and Google Chrome).

I can easily do it in Python, as such:

def getAppTitle (fullStr):
    lastDashIndex = None
    for i in range(len(fullStr)):
        if fullStr[i] == '-' or fullStr[i] == '—':
            lastDashIndex = i
    return fullStr[lastDashIndex+2:] if lastDashIndex else fullStr

print(getAppTitle('blablabla - blablabla - ApplicationName'))
# returns ApplicationName

I have been trying to do the same in shell script but I can't do it for the life of me. Also, for some reasons, some applications use "-" (normal dash) and others "—" (em dash).

How can I do that in shell?

2
  • 1
    Do you need to trim the space in front the ' Konsole'? Commented Oct 6, 2020 at 23:27
  • @thanasisp i thought i did but it seems to work fantastically well; man, thank you so much!!! Commented Oct 6, 2020 at 23:32

1 Answer 1

3

You have to use this 'em dash' or dash as the field separator and print the last field:

xdotool getactivewindow getwindowname | awk -F"—|-" '{print $NF}'

I am not sure where this 'em dash' comes from, I had to copy paste it for the above command.


Maybe better, use two characters as the FS, any dash and a space, to get the same as your script, with the space trimmed.

xdotool getactivewindow getwindowname | awk -F"— |- " '{print $NF}'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.