Загрузка данных


@echo off
powershell -NoProfile -ExecutionPolicy Bypass -Command "$p='%~f0'; $s=Get-Content -Raw -LiteralPath $p; $code=($s -split '# POWERSHELL_START',2)[1]; Invoke-Expression $code"
pause
exit /b

# POWERSHELL_START

$ErrorActionPreference = "SilentlyContinue"

$url = "https://kompege.ru"

# Desktop
$desktop = [Environment]::GetFolderPath("Desktop")

# Projects folder
$projects = Join-Path $desktop "Projects"
if (!(Test-Path -LiteralPath $projects)) {
    New-Item -ItemType Directory -Path $projects | Out-Null
}

# Create file: yyyy-MM-dd_1.py, yyyy-MM-dd_2.py, etc.
$today = Get-Date -Format "yyyy-MM-dd"
$n = 1

do {
    $pyFile = Join-Path $projects ("{0}_{1}.py" -f $today, $n)
    $n++
} while (Test-Path -LiteralPath $pyFile)

New-Item -ItemType File -Path $pyFile | Out-Null

# Window moving tools
Add-Type @"
using System;
using System.Runtime.InteropServices;

public class WinApi {
    [DllImport("user32.dll")]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
"@

Add-Type -AssemblyName System.Windows.Forms

$screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea

$leftX = $screen.Left
$topY = $screen.Top
$halfWidth = [int]($screen.Width / 2)
$height = [int]$screen.Height
$rightX = $leftX + $halfWidth

# Open Chrome on the left
$chromePaths = @(
    "chrome.exe",
    "C:\Program Files\Google\Chrome\Application\chrome.exe",
    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
    "$env:LOCALAPPDATA\Google\Chrome\Application\chrome.exe"
)

$chromeOpened = $false

foreach ($chromePath in $chromePaths) {
    if (!$chromeOpened) {
        if ($chromePath -eq "chrome.exe" -or (Test-Path $chromePath)) {
            Start-Process $chromePath -ArgumentList "--new-window", $url
            $chromeOpened = $true
        }
    }
}

Start-Sleep -Seconds 3

$chromeWindow = Get-Process chrome |
    Where-Object { $_.MainWindowHandle -ne 0 } |
    Sort-Object StartTime -Descending |
    Select-Object -First 1

if ($chromeWindow) {
    [WinApi]::ShowWindow($chromeWindow.MainWindowHandle, 1) | Out-Null
    [WinApi]::MoveWindow($chromeWindow.MainWindowHandle, $leftX, $topY, $halfWidth, $height, $true) | Out-Null
}

# Open created .py file in IDLE on the right
$idleOpened = $false

$pythonCommands = @(
    "pyw.exe",
    "py.exe",
    "pythonw.exe",
    "python.exe"
)

foreach ($cmd in $pythonCommands) {
    if (!$idleOpened) {
        $found = Get-Command $cmd -ErrorAction SilentlyContinue
        if ($found) {
            Start-Process $cmd -ArgumentList "-m", "idlelib", "`"$pyFile`""
            $idleOpened = $true
        }
    }
}

if (!$idleOpened) {
    Start-Process notepad.exe -ArgumentList "`"$pyFile`""
    exit
}

Start-Sleep -Seconds 5

$idleWindow = Get-Process pythonw, python, pyw, py |
    Where-Object { $_.MainWindowHandle -ne 0 } |
    Sort-Object StartTime -Descending |
    Select-Object -First 1

if ($idleWindow) {
    [WinApi]::ShowWindow($idleWindow.MainWindowHandle, 1) | Out-Null
    [WinApi]::MoveWindow($idleWindow.MainWindowHandle, $rightX, $topY, $halfWidth, $height, $true) | Out-Null
}

Write-Host "Created:"
Write-Host $pyFile