如何编写一个苹果脚本,让鼠标空闲后移动到某个位置?

问题描述 投票:0回答:1

我正在寻找一种在 MacBook 上闲置一段时间后移动鼠标的方法,由于某些限制,操作系统中的空闲设置无法访问,某些移动鼠标的软件也无法访问。

applescript mousemove osascript idle-timer
1个回答
0
投票

经过几个小时的尝试,我设法获得了一段有效的代码:

  • 应使用
    osascript
  • 执行
  • 需要安装
    cliclick
    (可以使用
    brew
    安装)
set initialPosition to {10, 300}
set moveDistance to 10
set idleThreshold to 60 -- Idle threshold in seconds

set previousPosition to missing value
set idleTime to 0

-- Function to get the current mouse position using cliclick
on getMousePosition()
    set mousePosition to {}
    try
        set mousePosition to (do shell script "/usr/local/bin/cliclick p")
        set {mouseX, mouseY} to text items of mousePosition
        return {mouseX as integer, mouseY as integer}
    on error errMsg
        return missing value
    end try
end getMousePosition

repeat
    set currentMousePosition to getMousePosition()
    
    if currentMousePosition is not equal to previousPosition then
        set idleTime to 0
        set previousPosition to currentMousePosition
    else
        set idleTime to idleTime + 5 -- Increment idle time by the delay value
    end if
    
    if idleTime > idleThreshold then
        set currentX to item 1 of initialPosition
        set currentY to item 2 of initialPosition
        
        -- Move mouse up by 10 pixels
        do shell script "/usr/local/bin/cliclick m:" & currentX & "," & (currentY + moveDistance)
        delay 1 -- Adjust delay as needed
        
        -- Move mouse down by 10 pixels
        do shell script "/usr/local/bin/cliclick m:" & currentX & "," & (currentY - moveDistance)
        delay 1 -- Adjust delay as needed
        
        -- Reset idle time
        set idleTime to 0
    end if
    
    delay 5 -- Check mouse position every 5 seconds
end repeat

© www.soinside.com 2019 - 2024. All rights reserved.