[AHK]用大模型写ahk脚本

问题背景

遇到程序在运行,但是在屏幕上看不到的窘境,于是想用AHK来实现一键在主屏幕上居中显示。

解决思路

手撸是不可能手撸的,我有豆包我有cursor,于是想看看她俩到底能力咋样。

提示词

复制代码
用AHK v2实现:热键Win+C 使当前窗口显示在主屏幕中间

先登场的是豆包

原始代码如下

复制代码
#c:: ; #代表Windows键,这里定义了Win+C的热键组合
{
    WinGetPos, x, y, width, height, A ; 获取当前活动窗口(A表示活动窗口)的位置和尺寸信息
    SysGet, MonitorWorkArea, MonitorWorkArea ; 获取主屏幕的工作区域(去除任务栏等占用的区域)信息
    newX := (MonitorWorkAreaRight - MonitorWorkAreaLeft - width) / 2 ; 计算新的X坐标,让窗口在水平方向居中
    newY := (MonitorWorkAreaBottom - MonitorWorkAreaTop - height) / 2 ; 计算新的Y坐标,让窗口在垂直方向居中
    WinMove, A,, newX, newY ; 将活动窗口(A)移动到计算好的新坐标位置,实现居中显示
}

点评,代码根本就不是AHK v2的语法,明显就是AHK v1的,试了一下的确能用,好吧 应该是豆包还不擅长AHK v2

后登场的是Cursor用的是claude-3.5-sonnet

代码说明很人性化

  • 代码说明:
  1. #Requires AutoHotkey v2.0 - 指定需要 AHK v2 运行环境

  2. #SingleInstance Force - 确保脚本只运行一个实例

  3. #c:: - 定义热键为 Win+C

  4. WinExist("A") - 获取当前活动窗口的句柄

  5. WinGetPos() - 获取窗口的当前位置和大小

  6. MonitorGetPrimary() - 获取主显示器

  7. MonitorGetWorkArea() - 获取显示器的工作区域(排除任务栏)

  • 计算新的窗口位置,使其在主屏幕中居中
  • WinMove() - 移动窗口到新位置

使用方法:

  1. 保存并运行脚本
  • 选择要居中的窗口
  • 按下 Win+C
  • 窗口将自动移动到主屏幕中间

原始代码

复制代码
#Requires AutoHotkey v2.0
#SingleInstance Force

; Win+C 将当前窗口居中显示在主屏幕
#c:: {
    ; 获取当前活动窗口
    activeWin := WinExist("A")
    
    ; 获取窗口大小
    winPos := WinGetPos()
    winWidth := winPos.Width
    winHeight := winPos.Height
    
    ; 获取主屏幕分辨率
    mainScreen := MonitorGetPrimary()
    monitorInfo := MonitorGetWorkArea(mainScreen)
    
    ; 计算居中位置
    newX := monitorInfo.Left + (monitorInfo.Right - monitorInfo.Left - winWidth) // 2
    newY := monitorInfo.Top + (monitorInfo.Bottom - monitorInfo.Top - winHeight) // 2
    
    ; 移动窗口到居中位置
    WinMove(newX, newY,,, activeWin)
}

点评,这个脚本看着很现代,不过有点太超前了,据我了解AHK 2.1-alpha.14 也还不支持 winPos := WinGetPos() 这种返回值为对象的方式,所以 winWidth := winPos.Width , winHeight := winPos.Height 报错是必然的。

稍微改下:

复制代码
#Requires AutoHotkey v2.0
#SingleInstance Force

; Win+C 将当前窗口居中显示在主屏幕
#c:: {
    ; 获取当前活动窗口
    activeWin := WinExist("A")
    ; 获取窗口大小
     WinGetPos( &X, &Y, &winWidth, &winHeight, activeWin)
    ; 获取主屏幕分辨率
    mainScreen := MonitorGetPrimary()
    monitorInfo := MonitorGetWorkArea(mainScreen)
    IsExisting := MonitorGetWorkArea(mainScreen, &Left, &Top, &Right, &Bottom)
    ; 计算居中位置
    newX := Left + (Right - Left - winWidth) // 2
    newY := Top + (Bottom - Top - winHeight) // 2
    ; 移动窗口到居中位置
    WinMove(newX, newY,,, activeWin)
}

可以在AHK v2 中运行了。

总结

大模型已经很强大了,只要你会提问,日常小脚本可以不用亲自动手了。君子动口不动手,老话说得好。

相关推荐
奔波儿灞和灞波儿奔3 小时前
CursorFreeVIP 使用流程说明(macOS)
cursor
围巾哥萧尘2 天前
VideoSpeedMaster:谷歌Chrome视频加速器插件开发与发布实战🧣
cursor
AryaNimbus3 天前
你不知道的Cursor系列:如何使用Cursor同时开发多项目?
前端·ai编程·cursor
!win !4 天前
Trae/Vs Code/Cursor命令行无法跑npm命令
开发工具·vs code·cursor·trae
攀登的牵牛花7 天前
Claude Code 通关秘籍国内玩家免费上车指南
ai编程·claude·cursor
这人是玩数学的8 天前
在 Cursor 中规范化生成 UI 稿实践
前端·ai编程·cursor
coder_pig8 天前
👦抠腚男孩的AI学习之旅 | 4、如何借助AI,高效学习,实现快速"跨域"
aigc·ai编程·cursor
风云信步8 天前
微软开源 GitHub Copilot VS code plugin 源码分析 (二) copilot-instructions.md 文件的应用逻辑
aigc·ai编程·cursor
大模型教程9 天前
Cursor 快速入门指南:从安装到核心功能
程序员·llm·cursor
jifei9 天前
有了Cursor,为什么还要买摸着Cursor过河的Trae?
cursor·trae