How to create a shortcut to start, minimize and bring programs to the foreground

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
1
down vote

favorite
1












I just switched to Linux and I am setting up my laptop. In windows, I use an AutoHotkey shortcut, to do the following:

- Open "chrome" when it is not active.

- Minimize "chrome" when it in the foreground.

- Bring it to the foreground when the window is not active.



I use the following code in "Autokey" which works nicely but I would love it if I can do this with a simple bash script:



#AutoKey script to toggle any windowed application, Nautilus as the example. Requires xdotool and wmctrl.
import subprocess
command = 'wmctrl -lx'
output = system.exec_command(command, getOutput=True)

if 'google-chrome' in output:
winClass = window.get_active_class()
if winClass == 'google-chrome.Google-chrome':
system.exec_command("xdotool windowminimize $(xdotool getactivewindow)")
else:
system.exec_command("wmctrl -x -a google-chrome")

else:
system.exec_command("google-chrome")
#end script


Now I tried to translate this to bash code but I get stuck in checking if chrome is running in the foreground. I had the following pseudocode in mind but I can not find the right shell commands:



if chrome is not open
open chrome
else
if chrome is on foreground
minimize chrome
else
bring chrome to foreground
end


I tried to do obtain this behaviour in Linux by using a shell script in the Linux "shortcut" application. But up till now, I was not able to find a way to check if Google-chrome is in the foreground. I tried using the "xdotools" package but this doesn't seem to work:



if ($(xdotool search --name "Google Chrome") -eq $(xdotool getactivewindow))
xdotool windowminimize $(xdotool getactivewindow)
else
wmctrl -x -a google-chrome
end


Do you maybe have some tips on what is the best way to achieve this? I now have the following ingredients:



To open google-chrome I use:



google-chrome &


To minimize google-chrome I use:



xdotool windowminimize $(xdotool getactivewindow)


To maximize google-chrome I use:



wmctrl -x -a google-chrome


And I think I need to use something like this to check if chrome is on foreground:



wmctrl -lx
xdotool search --name "Google Chrome"
xdotool getactivewindow
enter code here


Thanks a lot in advance,

Greetings,
Rick,



System information:
Desktop: Hp Zbook G3 studio
Distributor ID: Ubuntu
Description: Ubuntu Bionic Beaver (development branch)
Release: 18.04
Codename: bionic







share|improve this question


























    up vote
    1
    down vote

    favorite
    1












    I just switched to Linux and I am setting up my laptop. In windows, I use an AutoHotkey shortcut, to do the following:

    - Open "chrome" when it is not active.

    - Minimize "chrome" when it in the foreground.

    - Bring it to the foreground when the window is not active.



    I use the following code in "Autokey" which works nicely but I would love it if I can do this with a simple bash script:



    #AutoKey script to toggle any windowed application, Nautilus as the example. Requires xdotool and wmctrl.
    import subprocess
    command = 'wmctrl -lx'
    output = system.exec_command(command, getOutput=True)

    if 'google-chrome' in output:
    winClass = window.get_active_class()
    if winClass == 'google-chrome.Google-chrome':
    system.exec_command("xdotool windowminimize $(xdotool getactivewindow)")
    else:
    system.exec_command("wmctrl -x -a google-chrome")

    else:
    system.exec_command("google-chrome")
    #end script


    Now I tried to translate this to bash code but I get stuck in checking if chrome is running in the foreground. I had the following pseudocode in mind but I can not find the right shell commands:



    if chrome is not open
    open chrome
    else
    if chrome is on foreground
    minimize chrome
    else
    bring chrome to foreground
    end


    I tried to do obtain this behaviour in Linux by using a shell script in the Linux "shortcut" application. But up till now, I was not able to find a way to check if Google-chrome is in the foreground. I tried using the "xdotools" package but this doesn't seem to work:



    if ($(xdotool search --name "Google Chrome") -eq $(xdotool getactivewindow))
    xdotool windowminimize $(xdotool getactivewindow)
    else
    wmctrl -x -a google-chrome
    end


    Do you maybe have some tips on what is the best way to achieve this? I now have the following ingredients:



    To open google-chrome I use:



    google-chrome &


    To minimize google-chrome I use:



    xdotool windowminimize $(xdotool getactivewindow)


    To maximize google-chrome I use:



    wmctrl -x -a google-chrome


    And I think I need to use something like this to check if chrome is on foreground:



    wmctrl -lx
    xdotool search --name "Google Chrome"
    xdotool getactivewindow
    enter code here


    Thanks a lot in advance,

    Greetings,
    Rick,



    System information:
    Desktop: Hp Zbook G3 studio
    Distributor ID: Ubuntu
    Description: Ubuntu Bionic Beaver (development branch)
    Release: 18.04
    Codename: bionic







    share|improve this question
























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I just switched to Linux and I am setting up my laptop. In windows, I use an AutoHotkey shortcut, to do the following:

      - Open "chrome" when it is not active.

      - Minimize "chrome" when it in the foreground.

      - Bring it to the foreground when the window is not active.



      I use the following code in "Autokey" which works nicely but I would love it if I can do this with a simple bash script:



      #AutoKey script to toggle any windowed application, Nautilus as the example. Requires xdotool and wmctrl.
      import subprocess
      command = 'wmctrl -lx'
      output = system.exec_command(command, getOutput=True)

      if 'google-chrome' in output:
      winClass = window.get_active_class()
      if winClass == 'google-chrome.Google-chrome':
      system.exec_command("xdotool windowminimize $(xdotool getactivewindow)")
      else:
      system.exec_command("wmctrl -x -a google-chrome")

      else:
      system.exec_command("google-chrome")
      #end script


      Now I tried to translate this to bash code but I get stuck in checking if chrome is running in the foreground. I had the following pseudocode in mind but I can not find the right shell commands:



      if chrome is not open
      open chrome
      else
      if chrome is on foreground
      minimize chrome
      else
      bring chrome to foreground
      end


      I tried to do obtain this behaviour in Linux by using a shell script in the Linux "shortcut" application. But up till now, I was not able to find a way to check if Google-chrome is in the foreground. I tried using the "xdotools" package but this doesn't seem to work:



      if ($(xdotool search --name "Google Chrome") -eq $(xdotool getactivewindow))
      xdotool windowminimize $(xdotool getactivewindow)
      else
      wmctrl -x -a google-chrome
      end


      Do you maybe have some tips on what is the best way to achieve this? I now have the following ingredients:



      To open google-chrome I use:



      google-chrome &


      To minimize google-chrome I use:



      xdotool windowminimize $(xdotool getactivewindow)


      To maximize google-chrome I use:



      wmctrl -x -a google-chrome


      And I think I need to use something like this to check if chrome is on foreground:



      wmctrl -lx
      xdotool search --name "Google Chrome"
      xdotool getactivewindow
      enter code here


      Thanks a lot in advance,

      Greetings,
      Rick,



      System information:
      Desktop: Hp Zbook G3 studio
      Distributor ID: Ubuntu
      Description: Ubuntu Bionic Beaver (development branch)
      Release: 18.04
      Codename: bionic







      share|improve this question














      I just switched to Linux and I am setting up my laptop. In windows, I use an AutoHotkey shortcut, to do the following:

      - Open "chrome" when it is not active.

      - Minimize "chrome" when it in the foreground.

      - Bring it to the foreground when the window is not active.



      I use the following code in "Autokey" which works nicely but I would love it if I can do this with a simple bash script:



      #AutoKey script to toggle any windowed application, Nautilus as the example. Requires xdotool and wmctrl.
      import subprocess
      command = 'wmctrl -lx'
      output = system.exec_command(command, getOutput=True)

      if 'google-chrome' in output:
      winClass = window.get_active_class()
      if winClass == 'google-chrome.Google-chrome':
      system.exec_command("xdotool windowminimize $(xdotool getactivewindow)")
      else:
      system.exec_command("wmctrl -x -a google-chrome")

      else:
      system.exec_command("google-chrome")
      #end script


      Now I tried to translate this to bash code but I get stuck in checking if chrome is running in the foreground. I had the following pseudocode in mind but I can not find the right shell commands:



      if chrome is not open
      open chrome
      else
      if chrome is on foreground
      minimize chrome
      else
      bring chrome to foreground
      end


      I tried to do obtain this behaviour in Linux by using a shell script in the Linux "shortcut" application. But up till now, I was not able to find a way to check if Google-chrome is in the foreground. I tried using the "xdotools" package but this doesn't seem to work:



      if ($(xdotool search --name "Google Chrome") -eq $(xdotool getactivewindow))
      xdotool windowminimize $(xdotool getactivewindow)
      else
      wmctrl -x -a google-chrome
      end


      Do you maybe have some tips on what is the best way to achieve this? I now have the following ingredients:



      To open google-chrome I use:



      google-chrome &


      To minimize google-chrome I use:



      xdotool windowminimize $(xdotool getactivewindow)


      To maximize google-chrome I use:



      wmctrl -x -a google-chrome


      And I think I need to use something like this to check if chrome is on foreground:



      wmctrl -lx
      xdotool search --name "Google Chrome"
      xdotool getactivewindow
      enter code here


      Thanks a lot in advance,

      Greetings,
      Rick,



      System information:
      Desktop: Hp Zbook G3 studio
      Distributor ID: Ubuntu
      Description: Ubuntu Bionic Beaver (development branch)
      Release: 18.04
      Codename: bionic









      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 29 at 13:44

























      asked Apr 26 at 20:10









      arcety

      316




      316




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I received the answer to my question from a user on another forum.



          #!/bin/bash

          chromefocus=$(xdotool getwindowfocus getwindowname | grep -c "Google Chrome")

          if [ "$chromefocus" -gt "0" ]; then
          xdotool windowminimize $(xdotool getactivewindow)
          else
          wmctrl -xa "google-chrome.Google-chrome" || /usr/bin/google-chrome
          fi





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "89"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1028527%2fhow-to-create-a-shortcut-to-start-minimize-and-bring-programs-to-the-foreground%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            I received the answer to my question from a user on another forum.



            #!/bin/bash

            chromefocus=$(xdotool getwindowfocus getwindowname | grep -c "Google Chrome")

            if [ "$chromefocus" -gt "0" ]; then
            xdotool windowminimize $(xdotool getactivewindow)
            else
            wmctrl -xa "google-chrome.Google-chrome" || /usr/bin/google-chrome
            fi





            share|improve this answer
























              up vote
              1
              down vote



              accepted










              I received the answer to my question from a user on another forum.



              #!/bin/bash

              chromefocus=$(xdotool getwindowfocus getwindowname | grep -c "Google Chrome")

              if [ "$chromefocus" -gt "0" ]; then
              xdotool windowminimize $(xdotool getactivewindow)
              else
              wmctrl -xa "google-chrome.Google-chrome" || /usr/bin/google-chrome
              fi





              share|improve this answer






















                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                I received the answer to my question from a user on another forum.



                #!/bin/bash

                chromefocus=$(xdotool getwindowfocus getwindowname | grep -c "Google Chrome")

                if [ "$chromefocus" -gt "0" ]; then
                xdotool windowminimize $(xdotool getactivewindow)
                else
                wmctrl -xa "google-chrome.Google-chrome" || /usr/bin/google-chrome
                fi





                share|improve this answer












                I received the answer to my question from a user on another forum.



                #!/bin/bash

                chromefocus=$(xdotool getwindowfocus getwindowname | grep -c "Google Chrome")

                if [ "$chromefocus" -gt "0" ]; then
                xdotool windowminimize $(xdotool getactivewindow)
                else
                wmctrl -xa "google-chrome.Google-chrome" || /usr/bin/google-chrome
                fi






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 30 at 17:48









                arcety

                316




                316



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1028527%2fhow-to-create-a-shortcut-to-start-minimize-and-bring-programs-to-the-foreground%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    pylint3 and pip3 broken

                    Missing snmpget and snmpwalk

                    How to enroll fingerprints to Ubuntu 17.10 with VFS491