Skip to content

Instantly share code, notes, and snippets.

@jca02266
Last active November 3, 2021 23:57
Show Gist options
  • Save jca02266/2ee4563fd6192b351c0c045f209e03eb to your computer and use it in GitHub Desktop.
Save jca02266/2ee4563fd6192b351c0c045f209e03eb to your computer and use it in GitHub Desktop.
朝やることリスト(powershell) (BOM付UTF-8かShift_JISで保存すること)
powershell -WindowStyle Hidden -ExecutionPolicy RemoteSigned -File "%~dpn0.ps1" morningCheckList
param($ident)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
function GetScreenSize {
$ScreenWidth = 1200
$ScreenHeight = 900
[System.Windows.Forms.Screen]::AllScreens | % {
if ($_.Primary -eq $true) {
$ScreenWidth = $_.WorkingArea.Width
$ScreenHeight = $_.WorkingArea.Height
}
}
$ScreenWidth
$ScreenHeight
}
function MakeForm {
$form = New-Object System.Windows.Forms.Form
$form.Text = '朝やることリスト'
$form.StartPosition = 'Manual'
$form.Padding = 10
$form.AutoSize = $true
$panel = New-Object Windows.Forms.FlowLayoutPanel
$panel.FlowDirection = [Windows.Forms.FlowDirection]::TopDown
$panel.Dock = [Windows.Forms.DockStyle]::Fill
$panel.AutoSize = $true
# label
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'タスクを実行したら右のボタンをチェック'
$panel.Controls.Add($label)
$form.Controls.Add($panel)
$form | Add-Member -MemberType ScriptMethod -Name ShowMe -Value {
$Size = $this.Size
$ScreenWidth, $ScreenHeight = GetScreenSize
$this.Location = "$($ScreenWidth - $Size.Width),$($ScreenHeight - $Size.Height)"
$this.Topmost = $true
$this.ShowDialog()
}
$form | Add-Member -MemberType ScriptMethod -Name AddCheckBox -Value {
param($text, $panel)
$panelChild = New-Object Windows.Forms.FlowLayoutPanel
$panelChild.FlowDirection = [Windows.Forms.FlowDirection]::LeftToRight
$panelChild.WrapContents = $false
$panelChild.AutoSize = $true
$panelChild.Padding = 0
$panelChild.Margin = 0
$panelChild.Dock = [Windows.Forms.DockStyle]::Fill
$button1 = New-Object System.Windows.Forms.Button
$button1.Size = "30,30"
$button1.Text = "Do"
$button1.FlatStyle = "System"
$button2 = New-Object System.Windows.Forms.CheckBox
$button2.Size = "260,30"
$button2.Text = $text
$button2.FlatStyle = "Flat"
$button2.TextAlign = "MiddleCenter"
$button2.Appearance = "Button"
$panel.Controls.Add($panelChild)
$panelChild.Controls.Add($button1)
$panelChild.Controls.Add($button2)
$panelChild, $button1, $button2
}
$form | Add-Member -MemberType ScriptMethod -Name AddButton -Value {
param($text, $cmd = $null)
$parentPanel = $this.Controls[0]
$panel, $cmdButton, $checkButton = $form.AddCheckBox($text, $parentPanel)
$checked = $false
$checkButton.Add_Click({
param($sender,$e)
$script:checked = ! $script:checked
if ($checked) {
$checkButton.BackColor = "#95f542"
$ByteData = [byte[]]@(0x27, 0x14) # U+2714: Heavy Check Mark
$cmdButton.Text = [System.Text.Encoding]::BigEndianUnicode.GetString($ByteData)
} else {
$checkButton.BackColor = [System.Drawing.SystemColors]::Control
$cmdButton.Text = "Do"
}
}.GetNewClosure())
if ($cmd) {
$cmdButton.Enabled = $true
} else {
$cmdButton.Enabled = $false
}
if ($cmd) {
$cmdButton.Add_Click({
& $cmd
}.GetNewClosure())
}
}
$form
}
function main {
# プロセス名と引数の一致判定により二重起動を防止する
# (新しいプロセスが古いプロセスを終了させる)
Get-WmiObject Win32_Process -Filter "name = 'powershell.exe'" | % {
$last_arg = (-split $_.CommandLine)[-1]
if ($_.ProcessId -ne $pid -and $last_arg -eq $ident) {
Stop-Process $_.ProcessId
}
}
$form = MakeForm
# AddButton(第一引数, [第二引数])
# 第一引数: タスク名/やること
# 第二引数(省略可): コマンドボタン押下時に実行するスクリプト(powershellのブロックまたはコマンド名)
function AddButton {
param($text, $cmd = $null)
$form.AddButton($text, $cmd)
}
# 複雑なコマンドの例
AddButton "勤務開始連絡" {
. {
# 連絡テキストをクリップボードにコピー
$OutputEncoding = [Console]::OutputEncoding
"$(Get-Date -Format("M/d")) テレワークを開始します" | clip
}
# Teamsを起動/前面に表示
& "$env:ProgramData\$env:UserName\Microsoft\Teams\Update.exe" '--processStart' 'Teams.exe' '--process-start-args' '--profile=AAD'
}
# URLを開く例
AddButton "Google" { start "http://google.co.jp" }
# Excel を開く例
AddButton "勤務表入力" "$env:userprofile\Desktop\勤務表.xlsx"
# テキストファイルを開く例
AddButton "ToDo確認" "$env:userprofile\Desktop\ToDo.txt"
# コマンドなし
AddButton "メール確認"
$result = $form.ShowMe()
}
main
@jca02266
Copy link
Author

movie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment