I want a script that will switch to or launch a URL in a web page just like the script I have that will switch to or launch a local application

407 views
Skip to first unread message

Yekutiel ben Heshel

unread,
Feb 3, 2020, 6:49:40 PM2/3/20
to autokey-users
I want a script that will switch to or launch a URL in a web page just like the script I have that will switch to or launch a local application .

The following script works as I intend it to work.

It will start Audacious if it is unopen or switch to Audacious if it is open.

import subprocess
command = 'wmctrl -l'
output = system.exec_command(command, getOutput=True)

if 'Audacious' in output:
    window.activate('Audacious',switchDesktop=True)

else:
    subprocess.Popen(["/usr/bin/audacious"])



However, the following script does not work as I intend it to work.

The following script will switch to a window with "To Do" in the window title if such a window exists and then launch https://www.youtube.com/ (which is the URL I am using for testing purposes, but not the URL I ultimately want it to launch) in Google Chrome. In such a case I do want the following script to switch to a window with "To Do" in the window title if such a window exists but in such a case I do not want it the following script to launch https://www.youtube.com/

Furthermore, the following script will not run (will give an error message) if a window does not exist with "To Do" in the window title
  
import subprocess

command = 'wmctrl -a "To Do"'
output = system.exec_command(command, getOutput=True)
if "'To Do'" in output:
    window.activate("'To Do'",switchDesktop=True)
    
else:
    subprocess.Popen(["google-chrome-stable", "https://www.youtube.com/"])

Do you have any advice for me regarding the problem indicated above?

Thanks,

Yekutiel

Joe

unread,
Feb 4, 2020, 9:25:55 AM2/4/20
to autoke...@googlegroups.com
Having no experience with this, I flailed with it for quite some time
and came up with the attached AutoKey scripts which seem to work.

The main difference between my scripts and yours is that they use try:
to see if something works and run the exception: code if it doesn't.
This prevents them from bombing out when the initial action fails.

I'm sure there's a better way to do it.

I don't have chrome installed, so you'll have to change "vivaldi" to
"google-chrome-stable" before running them.

ybh3 seems a lot simpler.

ybh3 uses the chrome switch --new-window. If it didn't use that, then it
opened the webpage as a new tab in an already open browser window. That
would be OK, except we have no idea what the title of the existing
window is, so we can't switch to it (without more Python foo.)

Weirdness:

For testing, I opened a kate window editing a junk file named "To
Do.txt". The script happily switches to it when it is present. Since
this isn't a browser window, this might not be the behavior you want.
You may have to do additional testing to see if the window you find is
running the program you desire. We can look into that if necessary.

In ybh3, the second except: shouldn't be necessary, but taking it out
makes the window command a syntax error. This is just me not knowing
much Python.

Joe

On 2/3/20 6:49 PM, Yekutiel ben Heshel wrote:
> I want a script that will switch to or launch a URL in a web page just
> like the script I have that will switch to or launch a local application .
>
> The following script *_works_* as I intend it to work.
>
> It will start Audacious if it is unopen or switch to Audacious if it
> is open.
>
> import subprocess
> command = 'wmctrl -l'
> output = system.exec_command(command, getOutput=True)
>
> if 'Audacious' in output:
>     window.activate('Audacious',switchDesktop=True)
>
> else:
>     subprocess.Popen(["/usr/bin/audacious"])
>
>
>
>
> However, the following script *_does not work_* as I intend it to work.
>
> The following script will switch to a window with "To Do" in the
> window title *_if such a window exists_* and then launch
> https://www.youtube.com/ (which is the URL I am using for testing
> purposes, but not the URL I ultimately want it to launch) in Google
> Chrome. In such a case I *_do want_* the following script to switch to
> a window with "To Do" in the window title *_if such a window
> exists_* but in such a case I *_do not want_* it the following script
> to launch https://www.youtube.com/
>
> Furthermore, the following script will not run (will give an error
> message) *_if a window does not exist_* with "To Do" in the window title
>   
>
> import subprocess
>
> command = 'wmctrl -a "To Do"'
> output = system.exec_command(command, getOutput=True)
> if "'To Do'" in output:
>     window.activate("'To Do'",switchDesktop=True)
>     
> else:
>     subprocess.Popen(["google-chrome-stable",
> "https://www.youtube.com/"])
>
>
> Do you have any advice for me regarding the problem indicated above?
>
> Thanks,
>
> Yekutiel
> --
> You received this message because you are subscribed to the Google
> Groups "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autokey-user...@googlegroups.com
> <mailto:autokey-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autokey-users/947eb7af-c501-4204-8981-fdb8a0a6da0c%40googlegroups.com
> <https://groups.google.com/d/msgid/autokey-users/947eb7af-c501-4204-8981-fdb8a0a6da0c%40googlegroups.com?utm_medium=email&utm_source=footer>.
ybh3.py
ybh.py

Yekutiel ben Heshel

unread,
Feb 4, 2020, 4:12:47 PM2/4/20
to autokey-users
Joe,
 
Thank you very much for spending your time to work on this problem for me. I appreciate that very much. I am often amazed that small amounts of computer code can take large amount of time to produce.  I try to remind myself that E=MC2 (that is, energy equals mass times the speed of light squared) is a very simple formula. Yet it took a non-trivial amount of time to arrive at.

 
Regarding the script that you sent me named ybh.py….
 
Please see the attachment to this message named, ybh_version_b.py, which, of course, is a modified version of ybh.py. I suppose you can test it very easily in a browser of your choosing such as Vivaldi, Chromium, Pale Moon, etc.
 
ybh_version_b.py actually works properly for me except for the following minor annoyance. In case ebay.com is open in a browser window then ybh_version_b.py will behave as I would like: the script will switch to the browser window with ebay.com. Yet—after switching correctly to that window which already had ebay.com open in it—an AutoKey error message pops up which, is extraneous, at least from my perspective.
 
In other words, this AutoKey error message does not appear to interfere with the functionality of the script yet, obviously, it is a minor annoyance. After all, who wants error messages popping up unnecessarily?
 
 
Regarding the script what you sent me named ybh3.py...
 
Please see the attachment to this message named, ybh3_version_b.py, which, unsurprisingly, is a modified version of ybh3.py.
 
In case ebay.com is currently open—in a window in the web browser I am testing this with (google-chrome-stable)—ybh3_version_b.py will switch to that window as I would like. However, in case ebay.com is currently not open (that is, in case ebay.com is currently closed) ybh3_version_b.py fails to open a new window in my web browser with ebay.com.
 
Finally, I am baffled that Google Chrome lacks two simple features: one that would allow users to lock the contents of a browser window (thereby preventing a user from adding or removing tabs to or from that window), and another that would prevent a browser window from being closed without user consent (which, for example, would be analogous to the dialog box that normally pops up when a user tries to close a window in a document in a text editor that has unsaved text in it). 
 
Around five or six years ago I used to run an add-on in Pale Moon that had this sort of functionality. That was before Firefox changed their policies regarding add-ons which rendered most add-ons inoperable.
 
Thanks,
 
Yekutiel
ybh_version_b.py
ybh3_version_b.py

Joe

unread,
Feb 10, 2020, 8:43:41 PM2/10/20
to autoke...@googlegroups.com
Inline.

Joe

On 2/4/20 4:12 PM, Yekutiel ben Heshel wrote:
> Joe,
>  
> *Thank you very much* for spending your time to work on this problem
> for me. I appreciate that very much. I am often amazed that small
> amounts of computer code can take large amount of time to produce.  I
> try to remind myself that E=MC2 (that is, energy equals mass times the
> speed of light squared) is a very simple formula. Yet it took a
> non-trivial amount of time to arrive at.
>
>  
> Regarding the script that you sent me named *ybh.py*….
>  
> Please see the attachment to this message named, *ybh_version_b.py*,
> which, of course, is a modified version of ybh.py. I suppose you can
> test it very easily in a browser of your choosing such as Vivaldi,
> Chromium, Pale Moon, etc.
>  
> *ybh_version_b.py* actually works properly for me except for the
> following minor annoyance. In case ebay.com is open in a browser
> window then ybh_version_b.py will behave as I would like: the script
> will switch to the browser window with ebay.com. Yet—after switching
> correctly to that window which already had ebay.com open in it—an
> AutoKey error message pops up which, is extraneous, at least from my
> perspective.

There are two issues with your version of the script:

1) You didn't see/recognize the comment in it (lines starting with a #)
which said you were supposed to edit it replacing "YOUR_WEB_BROWSER"
with the actual name of your browser.

2) Blocks of code are defined by indenting them in Python.

The following 3 lines (no matter what your email program does to them
here - that's why I sent it as an attachment) :

# Please replace YOUR_WEB_BROWSER below with the web browser installed
on your machine which you want this script to use to open the URL below.
For example, you might replace YOUR_WEB_BROWSER below with "vivaldi" or
"Firefox" or "google-chrome-stable"

subprocess.Popen(["YOUR_WEB_BROWSER", "--new-window",
"https://www.ebay.com/"])

need to each be indented four spaces so they become part of the else:
and are only executed if the condition of the if statement fails.

The way they are now, you have an empty else: (which is probably an
error by itself) and the next two statements are always executed (if it
doesn't die on the empty else:.)

I don't see the original code, so I don't know if I screwed it up of if
you inadvertently changed it.

>  
> In other words, this AutoKey error message does not appear to
> interfere with the functionality of the script yet, obviously, it is a
> minor annoyance. After all, who wants error messages popping up
> unnecessarily?
>  
>  
> Regarding the script what you sent me named *ybh3.py*...
>  
> Please see the attachment to this message named, *ybh3_version_b.py*,
> which, unsurprisingly, is a modified version of ybh3.py.
>  
> In case ebay.com is currently open—in a window in the web browser I am
> testing this with (google-chrome-stable)—ybh3_version_b.py will switch
> to that window as I would like. However, in case ebay.com is currently
> not open (that is, in case ebay.com is currently closed)
> ybh3_version_b.py fails to open a new window in my web browser with
> ebay.com.
>  

ybh3_version_b.py has the same sorts of errors as the other script.
YOUR_WEB_BROWSER needs to be replaced with the name of your actual
browser. The comment line is probably fine, but would be better if it
were indented eight spaces to match indentation of the surrounding code.

There's another bug in there that looks like I did it. The two activate
commands are looking for "ebay", but the subprocess command is opening
https://www.youtube.com/ . They should all be either ebay or youtube -
not a mix of the two.

**
> Finally, I am baffled that Google Chrome lacks two simple features:
> one that would allow users to lock the contents of a browser window
> (thereby preventing a user from adding or removing tabs to or from
> that window), and another that would prevent a browser window from
> being closed without user consent (which, for example, would be
> analogous to the dialog box that normally pops up when a user tries to
> close a window in a document in a text editor that has unsaved text in
> it). 
>  
> Around five or six years ago I used to run an add-on in Pale Moon that
> had this sort of functionality. That was before Firefox changed their
> policies regarding add-ons which rendered most add-ons inoperable.
Warning: I'm a big Vivaldi fan!

I don't use a lot of tabs at once, but if you do, check out the Vivaldi
browser. It can use most Chrome extensions and it has very powerful
tools for working with tabs. It also doesn't mine all of your data and
send it home to Google. I've been using it for at least three years now
and love it. Until I switched to it, I didn't particularly like any
browser after Firefox 3.x when they started breaking more things than
they fixed.

I just looked in Vivaldi. It has two full screens worth of settings just
for tabs, but not the two you want. You can pin tabs (maybe what you
want) and control what it does if you try to close the last tab in a
window. When I close my last tab, I have it set to display my home page
instead of exiting.

It also has general options you can set to have it ask  what you want it
to do when you try to close a tab or when you try to exit the browser.

The community is very friendly and helpful. The developers are also very
responsive and, if you make a feature request that enough other users
like, there's a fair chance it will get implemented if it's not super
hard to do.

The aim is to make it the most configurable/customizable browser
available - which they have achieved. They try to make it so you don't
need any add-ons, but they allow you to add them if you do. It's the
exact opposite of some other browsers which have a sort of minimalist
vibe where what you get is all you get and you'd better like it that way.

Even though it has tons of options, its defaults are pretty intelligent,
so you can use it "out of the box" effectively without having to
customize everything first and having to learn what all the options do.

>  
> Thanks,
>  
> Yekutiel
>
>
>
> Le lundi 3 février 2020 15:49:40 UTC-8, Yekutiel ben Heshel a écrit :
>
> I want a script that will switch to or launch a URL in a web page
> just like the script I have that will switch to or launch a local
> application .
>
> The following script *_works_* as I intend it to work.
>
> It will start Audacious if it is unopen or switch to Audacious if
> it is open.
>
> import subprocess
> command = 'wmctrl -l'
> output = system.exec_command(command, getOutput=True)
>
> if 'Audacious' in output:
>     window.activate('Audacious',switchDesktop=True)
>
> else:
>     subprocess.Popen(["/usr/bin/audacious"])
>
>
>
>
> However, the following script *_does not work_* as I intend it to
> work.
>
> The following script will switch to a window with "To Do" in the
> window title *_if such a window exists_* and then launch
> https://www.youtube.com/ (which is the URL I am using for testing
> purposes, but not the URL I ultimately want it to launch) in
> Google Chrome. In such a case I *_do want_* the following script
> to switch to a window with "To Do" in the window title *_if such a
> window exists_* but in such a case I *_do not want_* it the
> following script to launch https://www.youtube.com/
>
> Furthermore, the following script will not run (will give an error
> message) *_if a window does not exist_* with "To Do" in the window
> title
>   
>
> import subprocess
>
> command = 'wmctrl -a "To Do"'
> output = system.exec_command(command, getOutput=True)
> if "'To Do'" in output:
>     window.activate("'To Do'",switchDesktop=True)
>     
> else:
>     subprocess.Popen(["google-chrome-stable",
> "https://www.youtube.com/"])
>
>
> Do you have any advice for me regarding the problem indicated above?
>
> Thanks,
>
> Yekutiel
>
> --
> You received this message because you are subscribed to the Google
> Groups "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autokey-user...@googlegroups.com
> <mailto:autokey-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autokey-users/16ba8a7a-bb5b-4c03-837d-a53daa41fa3d%40googlegroups.com
> <https://groups.google.com/d/msgid/autokey-users/16ba8a7a-bb5b-4c03-837d-a53daa41fa3d%40googlegroups.com?utm_medium=email&utm_source=footer>.

Yekutiel ben Heshel

unread,
Feb 10, 2020, 9:18:04 PM2/10/20
to autokey-users
Joe,

Thanks for taking the time to get back to me. I appreciate that.

I think you misunderstood me.

Sure, yes, of course, I understood that I was supposed to replace YOUR_WEB_BROWSER with my web browser. Frankly, I presumed that would be obvious from the following comment which I added...

# Please replace YOUR_WEB_BROWSER below with the web browser installed
on your machine which you want this script to use to open the URL below.
For example, you might replace YOUR_WEB_BROWSER below with "vivaldi" or
"Firefox" or "google-chrome-stable"

Again, the comment above is not a comment you added. I added the comment above. Frankly, I'm surprised that was unclear to you. Instead of your instructions "outside of the script" (which you seem to favor) I put instructions inside script. That's all. In my versions, I used "eBay.com" instead of "YouTube.com" this time to help avoid confusing the versions.

If you come up with a version of the script that works properly (that is, a version of the script that does not create an extraneous error message) please let me know. I would be very interested in using it.

On the other hand, if your answer is, "Sorry Yekutiel, I spent many hours on this but I wasn't able to get rid of the extraneous error message. I don't want to spend any more time on this project. See, I'm not really an advanced Python developer. I simply dabble a little bit with Python", then so be it.

As for me, I'm not a developer. I essentially don't write code. I don't want to become enmeshed in detailed technical discussions.
 
Thanks,

Yekutiel

Le lundi 3 février 2020 15:49:40 UTC-8, Yekutiel ben Heshel a écrit :
ybh3_version_b.py
ybh_version_b.py

Joe

unread,
Feb 11, 2020, 8:12:38 PM2/11/20
to autoke...@googlegroups.com
Sorry for the misunderstanding.

What I need to see (preferably as attachments so they don't get
reformatted) is each actual script that you ran. I also need the error
message(s) you are getting.

A copy and paste from a terminal or a screenshot from a GUI would be good.

All I see so far is code that is broken (either by me or by you.) I
tried to fix what you showed me. (I didn't immediately see my original
code that I sent you - for comparison.)

One of my all time favorite quotes (from a pop lyric) is "Communication
is the problem to the answer." :)

As for the comments (#), when I'm giving someone something to try, I
like to keep it to the minimum needed so that there are no distractions,
etc. When I'm working on my own code or a "product" to be given to
someone as a working whole, I include lots of comments (and often,
commented out/disabled debugging code too).

I can't tell you how many times I've looked at a piece of working code
and thought, "that can't be right" and was saved by some comments that
told me why it was right. It's amazing how much damage you can do when
you try to fix something that isn't broken. :)

Joe

On 2/10/20 9:18 PM, Yekutiel ben Heshel wrote:
> Joe,
>
> Thanks for taking the time to get back to me. I appreciate that.
>
> I think you misunderstood me.
>
> Sure, yes, of course, I understood that I was supposed to replace
> *_YOUR_WEB_BROWSER_* with my web browser. Frankly, I presumed that
> would be obvious from the following comment which *_I added_*...
>
> # Please replace YOUR_WEB_BROWSER below with the web browser installed
> on your machine which you want this script to use to open the URL below.
> For example, you might replace YOUR_WEB_BROWSER below with "vivaldi" or
> "Firefox" or "google-chrome-stable"
>
> Again, the comment above is *_not_* a comment you added. *_I added the
> comment above_*. Frankly, I'm surprised that was unclear to you.
> Instead of your instructions "outside of the script" (which you seem
> to favor) I put instructions inside script. That's all. In my
> versions, I used "eBay.com" instead of "YouTube.com" this time to help
> avoid confusing the versions.
>
> If you come up with a version of the script that works properly (that
> is, a version of the script that does not create an extraneous error
> message) please let me know. I would be very interested in using it.
>
> On the other hand, if your answer is, "Sorry Yekutiel, I spent many
> hours on this but I wasn't able to get rid of the extraneous error
> message. I don't want to spend any more time on this project. See, I'm
> not really an advanced Python developer. I simply dabble a little bit
> with Python", then so be it.
>
> As for me, I'm not a developer. I essentially don't write code. I
> don't want to become enmeshed in detailed technical discussions.
>  
> Thanks,
>
> Yekutiel
>
> Le lundi 3 février 2020 15:49:40 UTC-8, Yekutiel ben Heshel a écrit :
>
> I want a script that will switch to or launch a URL in a web page
> just like the script I have that will switch to or launch a local
> application .
>
> The following script *_works_* as I intend it to work.
>
> It will start Audacious if it is unopen or switch to Audacious if
> it is open.
>
> import subprocess
> command = 'wmctrl -l'
> output = system.exec_command(command, getOutput=True)
>
> if 'Audacious' in output:
>     window.activate('Audacious',switchDesktop=True)
>
> else:
>     subprocess.Popen(["/usr/bin/audacious"])
>
>
>
>
> However, the following script *_does not work_* as I intend it to
> work.
>
> The following script will switch to a window with "To Do" in the
> window title *_if such a window exists_* and then launch
> https://www.youtube.com/ (which is the URL I am using for testing
> purposes, but not the URL I ultimately want it to launch) in
> Google Chrome. In such a case I *_do want_* the following script
> to switch to a window with "To Do" in the window title *_if such a
> window exists_* but in such a case I *_do not want_* it the
> following script to launch https://www.youtube.com/
>
> Furthermore, the following script will not run (will give an error
> message) *_if a window does not exist_* with "To Do" in the window
> title
>   
>
> import subprocess
>
> command = 'wmctrl -a "To Do"'
> output = system.exec_command(command, getOutput=True)
> if "'To Do'" in output:
>     window.activate("'To Do'",switchDesktop=True)
>     
> else:
>     subprocess.Popen(["google-chrome-stable",
> "https://www.youtube.com/"])
>
>
> Do you have any advice for me regarding the problem indicated above?
>
> Thanks,
>
> Yekutiel
>
> --
> You received this message because you are subscribed to the Google
> Groups "autokey-users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to autokey-user...@googlegroups.com
> <mailto:autokey-user...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/autokey-users/8ec33dad-fd86-4aea-89d4-373e372ad4e9%40googlegroups.com
> <https://groups.google.com/d/msgid/autokey-users/8ec33dad-fd86-4aea-89d4-373e372ad4e9%40googlegroups.com?utm_medium=email&utm_source=footer>.

Yekutiel ben Heshel

unread,
Feb 11, 2020, 9:12:48 PM2/11/20
to autokey-users
Joe, 
 
I really do appreciate your time. Thank you very much. I'm sorry but I’d prefer not to spend any more time working on this project. 
 
Thanks,
 
Yekutiel



Le lundi 3 février 2020 15:49:40 UTC-8, Yekutiel ben Heshel a écrit :
Reply all
Reply to author
Forward
0 new messages