Instal and run scripts, recognize maximized views

This commit is contained in:
Peter 2023-11-09 15:24:22 +08:00
parent fb3d9d5edd
commit db2adcb15e
6 changed files with 41 additions and 14 deletions

View File

@ -6,18 +6,27 @@
Install dependencies: Install dependencies:
```bash ```powershell
python -m venv .venv .\install.ps1
source .venv/bin/activate
python -m pip install -r requirements.txt
``` ```
Run with: Run with:
```bash ```powershell
python .\ltspice-discord.py .\run.ps1
``` ```
Create a shortcut in `shell:startup` to start listening on startup:
Target: `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command [PATH]\ltspice-discord\run.ps1`\
Start in: `[PATH]\ltspice-discord\`
## How does it work
This script extracts file type from the title of any LTspice application then uses it to update the status on Discord.
The code should be short enough to audit if you are worried about data exfiltration, etc.
## Icons ## Icons
Icons adapted from: Icons adapted from:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

4
install.ps1 Normal file
View File

@ -0,0 +1,4 @@
cd $(Split-Path $MyInvocation.MyCommand.Path)
python -m venv .venv
.\.venv\Scripts\Activate.ps1
python -m pip install -r requirements.txt

View File

@ -33,6 +33,14 @@ def handle_ltspice(title: str) -> None:
global last_state global last_state
filename = title.replace("LTspice XVII - ", "") filename = title.replace("LTspice XVII - ", "")
file, ext = os.path.splitext(filename) file, ext = os.path.splitext(filename)
# The window title is of the format "LTspice XVII - [filename.extension]"
# if the view is maximized
if ext and ext[-1] == "]":
ext = ext[:-1]
file = file[1:]
# filename = filename[1:-1]
state = {} state = {}
if ext in (".asc",): # SCHEMATICS if ext in (".asc",): # SCHEMATICS
state = { state = {
@ -116,14 +124,17 @@ def main() -> None:
foreground_hwnd = win32gui.GetForegroundWindow() foreground_hwnd = win32gui.GetForegroundWindow()
pid = win32process.GetWindowThreadProcessId(foreground_hwnd) pid = win32process.GetWindowThreadProcessId(foreground_hwnd)
if pid[-1] >= 0: if pid[-1] >= 0:
active_process = psutil.Process(pid[-1]) try:
if active_process.name() in WINDOW_HANDLERS.keys(): active_process = psutil.Process(pid[-1])
handler = WINDOW_HANDLERS[active_process.name()] if active_process.name() in WINDOW_HANDLERS.keys():
if handler(win32gui.GetWindowText(foreground_hwnd)): handler = WINDOW_HANDLERS[active_process.name()]
last_program = { if handler(win32gui.GetWindowText(foreground_hwnd)):
"pid": active_process.pid, last_program = {
"path": active_process.name(), "pid": active_process.pid,
} "path": active_process.name(),
}
except psutil.NoSuchProcess:
pass
if last_program["pid"] is not None and last_program[ if last_program["pid"] is not None and last_program[
"pid" "pid"
] not in find_active_processes(last_program["path"]): ] not in find_active_processes(last_program["path"]):

3
run.ps1 Normal file
View File

@ -0,0 +1,3 @@
cd $(Split-Path $MyInvocation.MyCommand.Path)
.\.venv\Scripts\Activate.ps1
Start-Process "python" -ArgumentList "ltspice-discord.py" -WindowStyle Hidden