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:
```bash
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
```powershell
.\install.ps1
```
Run with:
```bash
python .\ltspice-discord.py
```powershell
.\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 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
filename = title.replace("LTspice XVII - ", "")
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 = {}
if ext in (".asc",): # SCHEMATICS
state = {
@ -116,14 +124,17 @@ def main() -> None:
foreground_hwnd = win32gui.GetForegroundWindow()
pid = win32process.GetWindowThreadProcessId(foreground_hwnd)
if pid[-1] >= 0:
active_process = psutil.Process(pid[-1])
if active_process.name() in WINDOW_HANDLERS.keys():
handler = WINDOW_HANDLERS[active_process.name()]
if handler(win32gui.GetWindowText(foreground_hwnd)):
last_program = {
"pid": active_process.pid,
"path": active_process.name(),
}
try:
active_process = psutil.Process(pid[-1])
if active_process.name() in WINDOW_HANDLERS.keys():
handler = WINDOW_HANDLERS[active_process.name()]
if handler(win32gui.GetWindowText(foreground_hwnd)):
last_program = {
"pid": active_process.pid,
"path": active_process.name(),
}
except psutil.NoSuchProcess:
pass
if last_program["pid"] is not None and last_program[
"pid"
] 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