Skip to content

Add focus() function to set application to be in focus in foreground [done] #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Vic-Lau opened this issue May 22, 2023 · 40 comments
Closed
Labels

Comments

@Vic-Lau
Copy link

Vic-Lau commented May 22, 2023

Hi @kensoh Mr.Kensoh, As the title:

  1. Chrome version: 113.0.5672.127, Sometimes r.close() can't close chrome, How can I ensure that chrome will be closed?
  2. How can I ensure that chrome keep top on desktop?
  3. How to switch between multiple windows in chrome?
@Vic-Lau Vic-Lau changed the title Sometimes can't close chrome and chrome activate keep top on desktop. Some about chrome problems. May 23, 2023
@kensoh
Copy link
Member

kensoh commented Jun 7, 2023

Hi @Vic-Lau!

  1. I haven't come across this feedback by users, is this still happening for you? When r.close() is run, it will run a script that kills off all TagUI RPA related processes, including Chrome. The next time this happens, look for the tagui folder src/end_processes.cmd under r.tagui_location() then double click that batch file to see if it helps to close. If not, maybe on your PC there is some unique configuration that the batch file can't kill Chrome.

  2. By default, Chrome will be launched and be on the top of desktop. Some ideas would be see if you can set Chrome to be always on top using Chrome settings or plugin. Or you can use visual automation to click on the Chrome item in taskbar to force it to be in the foreground.

  3. If you are using normal XPath, using popup() and switch between different tabs. If not, you can use visual automation to click on the tab you want. For creating multiple tabs in the first place, you need to either click a link that opens another tab, or use dom() to force a new tab to open. See example here - RPA Challenge - code design and optimisation to run 20-30X faster #120 (comment) (look for the dom() function in this example)

@kensoh kensoh added the query label Jun 7, 2023
@kensoh kensoh changed the title Some about chrome problems. Some Chrome questions - some suggested ideas, solutions and example Jun 7, 2023
@kensoh kensoh changed the title Some Chrome questions - some suggested ideas, solutions and example Some Chrome questions - some suggested ideas, solutions and example to try Jun 7, 2023
@Vic-Lau Vic-Lau changed the title Some Chrome questions - some suggested ideas, solutions and example to try Some Chrome query - some suggested ideas, solutions and example to try Jun 7, 2023
@Vic-Lau
Copy link
Author

Vic-Lau commented Jun 7, 2023

Hi @kensoh Mr.kensoh, Please forgive my description, these are not problems, just some suggestions~ :

  1. I still get this problem occasionally, I'll keep testing, maybe I'll modify end_processes.cmd.
  2. For example, every time I open chrome and want to maximize the window, I will execute r.keyboard('[alt][space]' r.keyboard('x')), but if chrome is not activated (not only keep top, but also activated), the code is invalid, I think similar situations will often appear in complex businesses. I have considered using visual automation to deal with it, but it will be very cumbersome. Have you considered adding the function of window activation?
  3. I understand your solution. Actually query 3 is similar to query 2. Chrome opens multiple tabs, When I want to switch and activate the tab I wanted, popup() can't do it. In addition, dom() is forced to open and Instead of switching. Have you considered adding a Tab ID parameter to popup() to activate the function of the tab?

@kensoh
Copy link
Member

kensoh commented Jul 4, 2023

Hi @Vic-Lau, how is it for 1?

For 2, I don't think Chrome API allows making the Chrome browser in focus, that has to be done from OS level. Can you try to r.wait() for some time before doing the keyboard shortcuts? Alternatively, consider editing the tagui_config.txt file to set the browser window size. File found in the tagui/src or .tagui/src folder under r.tagui_location()

For 3, popup() can only set the context but it doesn't make it appear in front, I don't think that can be controlled from Chrome API. Thus only keyboard to send Alt+Tab or using visual automation to click onto it will bring it in front. Automation can still continue without the tab being in front.

@kensoh
Copy link
Member

kensoh commented Jul 4, 2023

Closing issue for now because there isn't anything actionable, but do let me know your thoughts so I can see if something can be improved within the constraints of Chrome API or other workaround.

@kensoh kensoh closed this as completed Jul 4, 2023
@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 5, 2023

Hi @kensoh Mr.Kensoh emm... 1 is not clear yet~~~, 2 and 3 I have also compared some RPA products in the past few days. I think it is correct that this Query is closed, because although 2 and 3 are solved in some RPA products, But TagUI as an RPA engine or the RPA language, It only needs to provide the most basic implementation, and it should not involve product requirement solutions, so I will consider using TagUI and Python to encapsulation general method to implement similar requirement in the future, So thank you for your reply. Best Wishes~

Closing issue for now because there isn't anything actionable, but do let me know your thoughts so I can see if something can be improved within the constraints of Chrome API or other workaround.

@kensoh
Copy link
Member

kensoh commented Jul 5, 2023

What other RPA products did you try? Many RPA tools are based on Windows, so the complexity is lesser than solution like rpa package that can run on Windows, Mac, Linux.

I think you can use r.run() to run some Windows command to set an application to be in focus. Check out the link below and try, should be doable.

https://stackoverflow.com/questions/35988863/using-command-line-batch-to-switch-to-focus-on-app

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 5, 2023

Aliyun-RPA,iS-RPA etc... iS-RPA link: https://support.i-search.com.cn/article/1608274445197
I will try it, Thx~

What other RPA products did you try? Many RPA tools are based on Windows, so the complexity is lesser than solution like rpa package that can run on Windows, Mac, Linux.

I think you can use r.run() to run some Windows command to set an application to be in focus. Check out the link below and try, should be doable.

https://stackoverflow.com/questions/35988863/using-command-line-batch-to-switch-to-focus-on-app

@kensoh
Copy link
Member

kensoh commented Jul 5, 2023

Do both aliyun rpa and is rpa run on mac and Linux too? That will increase software complexity by a lot to stay consistent across OSes. But anyway, hard to compete with China commercial products. China tech products that can survive the national competition would have to be very very good.

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 6, 2023

No, Both only support Windows OS, So TagUI has better scalability, Also very awesome👍!
I'm not trying to compete, Just because the activation function is good.

@kensoh
Copy link
Member

kensoh commented Jul 6, 2023

Mac and Windows both seem to support this from OS level commands without adding dependencies. I'll take a look to see if it makes sense to add something.

@kensoh kensoh reopened this Jul 6, 2023
@kensoh kensoh closed this as completed Jul 6, 2023
@kensoh
Copy link
Member

kensoh commented Jul 6, 2023

Linux seems to require users to have wmctrl installed

@kensoh
Copy link
Member

kensoh commented Jul 6, 2023

Adding dev note Mac is quite straightforward, though only activate window, not select tab
os.system('osascript -e \'tell application "Google Chrome" to activate\' > /dev/null 2>&1')

  • returns 0 if there is such a window, 256 otherwise
  • not case sensitive, need full name of app

@kensoh
Copy link
Member

kensoh commented Jul 6, 2023

For Windows, to try using sendKeys.bat by possibly uploading to https://github.com/tebelorg/Tump/releases/tag/v1.0.0 and downloading on first run on Windows and store in tagui_location() + '/tagui/src' folder. Need to check if below works

os.system('sendkeys.bat "Google Chrome" "" > nul 2>&1')

Verified to work. Returns 0 if valid app name and 1 if otherwise. Not case sensitive. Need full name of app.

@kensoh kensoh changed the title Some Chrome query - some suggested ideas, solutions and example to try New function() to set application to be in focus in foreground - to create Jul 6, 2023
@kensoh kensoh added feature and removed query labels Jul 6, 2023
@kensoh kensoh reopened this Jul 6, 2023
kensoh added a commit that referenced this issue Jul 7, 2023
@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 7, 2023

Thank you for your understanding and support, Best Wishes~

@kensoh kensoh changed the title New function() to set application to be in focus in foreground - to create Add focus() function to set application to be in focus in foreground - to create Jul 7, 2023
@kensoh kensoh changed the title Add focus() function to set application to be in focus in foreground - to create Add focus() function to set application to be in focus in foreground [done] Jul 7, 2023
@kensoh
Copy link
Member

kensoh commented Jul 7, 2023

@Vic-Lau you try install v1.49 then you can use the following, for example, to make Google Chrome in focus:

r.focus('Google Chrome')

I'm running a promotion this year when a feature suggestion is accepted there is 100 USD gift voucher. Shall you pick a vendor so that I can get the gift voucher for you for this new feature suggestion? Thanks for the suggestion!!

@kensoh
Copy link
Member

kensoh commented Jul 7, 2023

Hi @Vic-Lau yes paypal is ok. You can pick a vendor or make some voucher purchase or online purchase around 100 USD, then share screenshot of purchase to me either here or other channels. You can also choose more expensive things above 100 USD and pay the rest yourself.

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 7, 2023

@kensoh Mr.Kensoh, I'm not clear, Is it possible to transfer money directly? Or, for example I have to buy something on Amazon and send you a screenshot of order? Because I have never done this in China.

@kensoh
Copy link
Member

kensoh commented Jul 7, 2023

Yes buy something or some voucher online so that I can transfer you. If the appreciation gift is cash, it can attract other wrong users to raise irrelevant bugs and feature suggestions to this project for cash money.

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 7, 2023

Oh I see. Can I choose an online shopping mall in China? Such as Taobao, Jingdong.

@kensoh
Copy link
Member

kensoh commented Jul 7, 2023

Yes of course! Any vendor or website is ok you can choose. I use Taobao too last time when in Singapore.

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 7, 2023

haha, Taobao is very good~ OK, I will take my family on a trip to Japan, Here is the screenshot of the order~ Thx

@kensoh
Copy link
Member

kensoh commented Jul 7, 2023

Oh nice, is this the visa document that you need to buy to visit Japan? Cool, I'll transfer you over the weekend

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 8, 2023

Yes, we will apply for a Japanese visa through a travel agency and we will go to Fukuoka to taste food, Thank you for your reward~ lol~

@kensoh
Copy link
Member

kensoh commented Jul 8, 2023

Hi @Vic-Lau i've sent you to your paypal email. There is some issue there is no CNY option in the target currency.

So I transferred you 111 USD instead. 111 USD because Paypal is a greedy company and will deduct about 5.5% from what you I send you as middleman fee. And from USD when you convert to CNY I think paypal from googling charge another 4.5% to you. So 111 USD will end up to be 100 USD worth by the time the money actually reach your bank account.

@kensoh
Copy link
Member

kensoh commented Jul 8, 2023

(And 111 USD is not even the amount I get charged, I get charged around 115 USD because they also charge commission for converting my base currency to USD .... T_T Anyway that's paypal. There's another app called TransferWise / Wise with good conversion commission and fees but is that common in China?)

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 8, 2023

Oh no... Paypal is so greedy!!! I want to return it, Do you have Alipay?

(And 111 USD is not even the amount I get charged, I get charged around 115 USD because they also charge commission for converting my base currency to USD .... T_T Anyway that's paypal. There's another app called TransferWise / Wise with good conversion commission and fees but is that common in China?)

@kensoh
Copy link
Member

kensoh commented Jul 8, 2023

I don't have Alipay. No need, don't try to return because I don't think I will get back 115 USD. Don't worry about it :)

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 8, 2023

OK~ Thx~
PS: Can you delete our private screenshots by the way?

这段话用中文,我的FB账号申诉回来了,添加了你的好友,如果可以的话。哈哈,再次感谢大神的慷慨~

@kensoh
Copy link
Member

kensoh commented Jul 8, 2023

I deleted now, the only sensitive info is your email, u need to edit to delete your message earlier with your email.

@kensoh
Copy link
Member

kensoh commented Jul 8, 2023

Yes will add you back!

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 12, 2023

Hi @kensoh Mr.Kensoh, focus() is working!!! Thx. But there is a little problem, I use pack() to upgrade, everything is ok except need to download https://github.com/tebelorg/Tump/releases/download/v1.0.0/sendKeys.bat, would you consider putting sendKeys.bat into pack()?

@kensoh
Copy link
Member

kensoh commented Jul 12, 2023

Hi @Vic-Lau I understand what you mean. Let me think over it what is the best way to distribute that file with pack()

@kensoh
Copy link
Member

kensoh commented Jul 13, 2023

In the meantime, you can just copy the sendKeys file over to your destination PC. As long as the file is in the same working directory where you run your Python script, it should work without trying to download. I'll think over what is elegant way to ship with pack() or update().

It's a bit tricky because this is an executable I'm cautious not to put it together with other source files. Otherwise you can get into issues with unique user environment situation like having a space in path name and it can potentially break the sendKeys function for those users. Escaping with double quotes could also cause problem because that command is meant to use with double quotes around the application name.

@kensoh
Copy link
Member

kensoh commented Jul 13, 2023

Another option could be embedding that file as part of the Python package, just like below javascript file. But this will add another 60 lines of code to tagui.py (3% more lines of code). So asking all users from Linux, Mac, Windows who are not using this focus() function to pay this price is somewhat a bad choice.

https://github.com/tebelorg/RPA-Python/blob/1137987b119f7a70be89b1e9c88e8af66e4582e0/tagui.py#L56C1-L56C1

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 14, 2023

I think copy the sendKeys file is acceptable and I suggest explain clearly where the file is stored.

@kensoh
Copy link
Member

kensoh commented Jul 14, 2023

Will add a comment here so folks looking at release notes or search for issues will know

@kensoh
Copy link
Member

kensoh commented Jul 14, 2023

Added in RPA for Python v1.49 and available with pip install rpa --upgrade

To use the feature on Windows or Mac, use r.focus('full name of application')
For example to make Chrome in front and in focus, use r.focus('Google Chrome')

Note - For Windows users using r.pack() or r.update() to install or update to a target computer without internet, you have to copy the sendKeys.bat manually as well to your target computer. The file will be auto-downloaded on your source computer with internet the first time you use the focus() function, and stored in the same folder as your working directory or Python script. On the target computer, put the file in the same folder as rpa.py and your Python automation script.

@Vic-Lau
Copy link
Author

Vic-Lau commented Jul 27, 2023

Hi @kensoh Mr.Kensoh,

After I tested the sendKeys.bat, I confirmed the sendKeys.bat have some compatibility issues, it doesn't work on some Windows OS, So I implemented focus() use Python pywin32 (pip install pywin32). hope for it have some value.

focus.zip

import win32com
import win32com.client
import win32gui
import win32con

"""Get all windows hwnd titiles.

Returns:
    titles (dict): all titles dict.
"""
def get_titles():
    titles = {}
    def callback(hwnd, extra):
        if(win32gui.IsWindow(hwnd) and
            win32gui.IsWindowEnabled(hwnd) and
            win32gui.IsWindowVisible(hwnd)):
            titles.update({hwnd: win32gui.GetWindowText(hwnd)})
        return True
    win32gui.EnumWindows(callback, None)
    print(titles)
    return titles


"""Set application to be in focus in foregroud.

Args:
    title (str): application title.
"""
def focus(title):
    assert title, "title is not null."
    titles = get_titles()
    for h, t in titles.items():
        if t:
            if title in t:
                print(str(h) + ": " + t)
                
                # solved error: (0, 'SetForegroundWindow', 'No error message is available').
                shell = win32com.client.Dispatch("WScript.Shell")
                shell.SendKeys("%")
                
                # foregroud window.
                win32gui.SetForegroundWindow(h)
                
                # window maximize.
                win32gui.ShowWindow(h, win32con.SW_MAXIMIZE)
                

if __name__ == "__main__":
    focus("Chrome")

@kensoh
Copy link
Member

kensoh commented Aug 3, 2023

Thanks @Vic-Lau! This is good! I'll find a place to weave this into the github main page readme. (but not make it default because this requires installing another python package)

@kensoh
Copy link
Member

kensoh commented Aug 5, 2023

Thanks @Vic-Lau ! Added a segment at API reference - https://github.com/tebelorg/RPA-Python#api-reference

"Use focus() to make Windows/Mac application windows to be in focus (see here for pywin32 alternative)."

@kensoh kensoh closed this as completed Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants