0

I would like to run my python script while I'm working on other things. For example, I want to run the python script while I'm watching a movie. But I don't want to exit full screen and go to the interactive shell and then go back.

I tried to create a batch file and run from the command line. But it's still not as neat and straight-forward as single-button functionality.

If I want to check the time while I'm watching a movie on hulu website.

python script:

import datetime

datetime.datetime.now()

I wonder if I can set a hotkey so that while I'm on other applications, I can just press the key and then python script will run itself in the background.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Muchdecal
  • 177
  • 4
  • 8
  • Possible duplicate of https://stackoverflow.com/questions/48042158/run-python-script-quickly-via-hotkey – Zionsof May 22 '19 at 05:03

1 Answers1

0

Try using keyboard module (allows us to assign custom shortcut keys):

import pyttsx3
import datetime
import keyboard

def time():
    engine = pyttsx3.init()
    hours = int(datetime.datetime.now().time().hour)%12
    if not hours:
        hours = 12
    engine.say("the time is " + str(hours) + " " + str(datetime.datetime.now().time().minute)) 
    engine.runAndWait()


def keyPress():
    keyboard.add_hotkey('+', lambda: time())
    keyboard.wait()

keyPress() 

WHAT THIS PROGRAM DOES:-

This program a first creates a custom shortcut (assigns it to keyboard key +(change this to whatever hotkey combination you want)). Upon pressing this key, a function called time() gets called, which makes use of windows TTS, to tell your the current time (via voice functionality like siri, alisa etc).

HOW DOES THIS WORK:-

I made use of external libraries keyboard(necessary) and pyttsx3(optional), where the module keyboard allows us to define our custom hotkeys and create a event handler for keyboard events, and pyttsx3 makes use of windows Text to Speech feature to read some text via speech (aka voice). I made use of voice functionality, to not interrupt your flow while your are watching something online.

HOW TO SET IT UP:-

  1. Copy the code, and save it as a .pyw file (notice the w in the end). The difference between a regular .py an a .pyw file is that, .py invokes console (~commandline) equivalent tool for execution and executes in foreground, on the other hand a .pyw file runs in the background at all times (i.e the console won't show up). So it won't require you to manually open the console at all times for executing the script as the script will be running in the background at all time.
  2. Copy this file (example.pyw) in the startup folder of your OS (any file in this folder will automatically execute once the operating system has started). What this will do is when your OS, will boot your example.pyw file will automatically start executing in the background, so you won't have to manually launch it at every system startup. And since it is in the background it won't interfere with your work. (if you're on windows OS, you can access your startup folder by typing shell:common startup on your run and pressing enter) (adding files to this folder requires root privileges)

HOW TO USE IT:-

While using your system press + (for my case), and your OS will tell you what the current time is. (though it requires your OS to have correct time)

PROS:-

  • Upon using this for long time I can tell you I never encountered a single application in which the custom defined hotkey won't work.

  • Slim to None time required after keypress, for command execution.

  • A lot resistant to spam of hotkey's. e.x. You may press the
    hotkey 100 times but the calls won't be made unless the previous
    command has completed execution.

CONS:-

  • Calling the script for the first time after Bootup, may cause a little latency in command execution (but only for once, i.e subsequent calls would be really fast)

P.S:-

It is optional to use pyttsx3, I used it as it makes checking time a lot more easier then reading text or something (at least for me).

Secondly, this process could be made more efficient if you want.

RECOMMENDATIONS:-

Alternatively, If you are familiar with AHK (AutoHotKey) then, doing what i just told on a .ahk script would be a piece of cake, and it's scripts are even faster than my Python one. (Honestly I would definitely Recommend you to use AutoHotKey as it is a really robust language, when it comes to make efficient Operating System scripts)

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
  • It worked. Thank you. Is there a way to deactivate the script? Since it's running in the background, I can't find anything to click in order to terminate the program. – Muchdecal May 22 '19 at 12:19
  • You can open your *Task Manager* and select the `python.pyw` application and select **End Task** to terminate it. – Vasu Deo.S May 22 '19 at 22:14