Windows 借助 Snipaste 实现定时截图

发布时间 2023-06-29 16:08:44作者: 一杯清酒邀明月

一、介绍

借助 Snipaste 软件实现连续定时截取屏幕。

截图间隔(可自己修改):5s

截图时间段(可自己修改):早上 8, 9, 10, 11;下午 13, 14, 15, 16, 17, 18

辅助软件(需要自行下载):https://www.snipaste.com/

辅助脚本(见下文):snipscreen.ps1

前期准备:为了脚本能在系统中找到 Snipaste 程序,需提前将 Snipaste 路径(含有 snipaste.exe 的文件夹路径)添加至系统环境变量的 Path 中

截图保存位置:在snipscreen.ps1 脚本所在的路径内会自动按当前日期创建文件夹,截取的图片按序号存在上述文件夹内,为方便后期查看截图,推荐将 snipscreen.ps1 脚本放在一个新文件夹内。

二、脚本代码

创建脚本文件 "snipscreen.ps1",右键编辑编写代码:

 1 while (1) {
 2    clear
 3 
 4    $current_dir = Get-Date -Format "yyyyMMdd" 
 5    $current_path = $PSScriptRoot + "\" +$current_dir
 6 
 7    # 创建文件夹
 8    if(!(Test-Path $current_path)){
 9        New-Item -Path $current_path -ItemType Directory
10    }
11 
12    # 检查当前文件夹的图片数量 
13    $filetype = "*.png" 
14    $file_count = [System.IO.Directory]::GetFiles("$current_path", "$filetype").Count
15 
16    $file_path = $current_path + "\" + $current_dir + "-" + $file_count + ".png"
17 
18    # 指定截图的时间段
19    $hour = Get-Date -UFormat %H
20    if(($hour -eq "08") -or ($hour -eq "09") -or ($hour -eq "10")`
21        -or ($hour -eq "11") -or ($hour -eq "13") -or ($hour -eq "14")`
22        -or ($hour -eq "15") -or ($hour -eq "16") -or ($hour -eq "17")`
23        -or ($hour -eq "18")) 
24    {
25        Write-Output $file_path
26 
27        # 调用 snipaste.exe 需要将其路径提前添加至环境变量的 Path 中
28        Snipaste.exe snip --full -o $file_path
29    }
30    else 
31    {
32        Write-Output "截图已停止"
33    }
34 
35    Start-Sleep –s 5 # 程序休眠 5s
36 }

注意:如果截图时出现一直出现通知消息,鼠标移至通知消息框上,按照下面的方式将通知关闭。

三、各命令或语法含义

$xxx: 代表 PowerShell 中的变量,使用 $xxx 取值或者赋值

Test-Path$current_path: 检查当前路径是否存在

Get-Date: 获取当前时间,-UFormat 指定输出格式,%H表示输出 24小时制下的小时,类型是字符串。更多说明文档里有说。

New-Item -Path $current_path -ItemType Directory

New-Item,指创建一个新元素,-ItemType Directory 指定元素的类型为文件夹,文档里有详细描述。

Snipaste.exe snip --full -o $file_path

Snipaste.exe 表示 Snipaste 的可执行程序,snip --full -o $file_path 指的是截取全屏并将文件保存到指定文件夹内。

$PSScriptRoot 表示当前脚本所在的路径。

附录

PowerShell 语法:

语句 - PowerShell​learn.microsoft.com/zh-cn/powershell/scripting/lang-spec/chapter-08?view=powershell-7.2

Snipaste 命令行语法:

Snipaste​docs.snipaste.com/command-line-options