如何在苹果脚本(脚本编辑器)中为重复操作添加简单的计数器

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

基本上我有一个脚本,我使用脚本编辑器在我的Mac上运行,以防止我的角色因AFK而被踢。这是我现在所拥有的。

delay 5
repeat 10000 times
    tell application "System Events"

        key down "w"
        delay (random number from 0.5 to 1)
        key up "w"

        key down "s"
        delay (random number from 0.5 to 1)
        key up "s"
        delay (random number from 20 to 30)
    end tell
end repeat

基本上我只是想添加一个计数器,这样我就可以看到它已经运行了多长时间。理想情况下,它会显示已用的总时间,而不是脚本重复的次数,但两者都可以。

applescript
1个回答
0
投票

两种计数方式(脚本重复的次数和时间)都涉及使用累加器变量,请注意

delay
以秒为单位运行:

set numTimesRepeated to 0
set amountTimeRunning to 0

set delayValue to 5
delay delayValue
set amountTimeRunning to amountTimeRunning + delayValue

repeat 10000 times
    tell application "System Events"
        
        key down "w"
        set delayValue to (random number from 0.5 to 1)
        delay delayValue
        set amountTimeRunning to amountTimeRunning + delayValue
        key up "w"
        
        key down "s"
        set delayValue to (random number from 0.5 to 1)
        delay delayValue
        set amountTimeRunning to amountTimeRunning + delayValue
        key up "s"
        
        set delayValue to (random number from 20 to 30)
        delay delayValue
        set amountTimeRunning to amountTimeRunning + delayValue
        
    end tell

    set numTimesRepeated to numTimesRepeated + 1
end repeat

log "numTimesRepeated: " & numTimesRepeated
log "amountTimeRunning: " & (amountTimeRunning / 60) & " minutes"
© www.soinside.com 2019 - 2024. All rights reserved.