当导出为.app时,applescript屏幕截图自动程序错误

当导出为.app时,applescript屏幕截图自动程序错误,applescript,screenshot,handler,Applescript,Screenshot,Handler,因此,我正在制作一个应用程序,它可以在一段可定制的时间内定期拍摄屏幕截图。我很难理解它,因为当我在applescript编辑器中运行应用程序时,一切都很好——但是当我将它导出到.app时,事情有问题,如果用户单击应用程序图标,它将触发“重复”代码,如果用户尝试退出,它的行为会不稳定 我在其他帖子中读到,在空闲状态下可以工作,但我不知道如何在指定的时间后结束空闲处理程序 有人知道我的难题的解决办法吗?谢谢大家! on run set frequencyList to {2, 5, 10, 15}

因此,我正在制作一个应用程序,它可以在一段可定制的时间内定期拍摄屏幕截图。我很难理解它,因为当我在applescript编辑器中运行应用程序时,一切都很好——但是当我将它导出到.app时,事情有问题,如果用户单击应用程序图标,它将触发“重复”代码,如果用户尝试退出,它的行为会不稳定

我在其他帖子中读到,在空闲状态下可以工作,但我不知道如何在指定的时间后结束空闲处理程序

有人知道我的难题的解决办法吗?谢谢大家!

on run

set frequencyList to {2, 5, 10, 15}

set durationList to {30, 60, 90, 120, 150, 180}

choose from list durationList with prompt "how long (in minutes) would you like to snap screenshots for?"
set chosenDuration to result as number
set chosenDurationSeconds to chosenDuration * 60


choose from list frequencyList with prompt "how often (in minutes) would you like to snap a screenshot?"
set chosenFrequency to result as number
set chosenFrequencySeconds to chosenFrequency * 60

set repeatNumber to chosenDurationSeconds / chosenFrequencySeconds

display dialog "Great! I'll take " & repeatNumber & " photos over the span of " & chosenDuration & " minutes."
display dialog "Next, choose where you want these to end up."

set FolderPath to POSIX path of (choose folder) as string

display dialog "Sounds good! Just let me run in the background, and I'll snap away until the time is up."

delay 1

repeat repeatNumber times
    set theCurrentDate to current date
    set shellCommand to "/usr/sbin/screencapture \"" & FolderPath & "Screen Shot" & theCurrentDate & ".png\""
    do shell script shellCommand
    delay chosenFrequencySeconds
end repeat

结束运行(end run)

以下是如何使用“on idle”处理程序编写代码(我没有测试它)。记住将其保存为“保持打开应用程序”。请注意,您告诉空闲处理程序在处理程序的最后一行中以秒为单位重复。还请注意,我们在计算日期退出应用程序,而不是重复多次。实际上,您可以创建一个计数器来计数空闲处理程序的循环,但是date方法更容易。最后请注意,我们有全局变量。这使得在运行时处理程序中启动的变量可用于空闲时处理程序

祝你好运

global quitTime, chosenFrequencySeconds, FolderPath

on run
    set frequencyList to {2, 5, 10, 15}
    set durationList to {30, 60, 90, 120, 150, 180}

    choose from list durationList with prompt "how long (in minutes) would you like to snap screenshots for?"
    set chosenDuration to result as number
    set quitTime to (current date) + chosenDuration * minutes

    choose from list frequencyList with prompt "how often (in minutes) would you like to snap a screenshot?"
    set chosenFrequency to result as number
    set chosenFrequencySeconds to chosenFrequency * 60

    display dialog "Great! I'll take " & repeatNumber & " photos over the span of " & chosenDuration & " minutes."
    display dialog "Next, choose where you want these to end up."

    set FolderPath to POSIX path of (choose folder) as string

    display dialog "Sounds good! Just let me run in the background, and I'll snap away until the time is up."

    delay 1
end run

on idle
    set theCurrentDate to current date
    set shellCommand to "/usr/sbin/screencapture " & quoted form of (FolderPath & "Screen Shot" & (theCurrentDate as text) & ".png")
    do shell script shellCommand

    if theCurrentDate is greater than or equal to quitTime then tell me to quit
    return chosenFrequencySeconds
end idle

也许您应该尝试实现一个
重新打开
处理程序。我已经使用了regulus的代码,它对我很有效,如果小程序安装在dock中,那么它会第一次启动,如果稍后单击它,您会收到一条消息,它已经在运行。
reopen
处理程序可能有一些多余的代码,比如测试应用程序是否正在运行并激活它,因为只有当应用程序已经运行时才应该触发reopen处理程序。但我将把这个留给您,至少它现在不会执行run处理程序。:)


非常感谢!这是我错过的告诉我放弃的部分:)这真的很有趣!非常感谢。
  global quitTime, chosenFrequencySeconds, FolderPath

  on run
    set frequencyList to {2, 5, 10, 15}
    set durationList to {30, 60, 90, 120, 150, 180}

    choose from list durationList with prompt "how long (in minutes) would you like to snap screenshots for?"
    set chosenDuration to result as number
    set quitTime to (current date) + chosenDuration * minutes

    choose from list frequencyList with prompt "how often (in minutes) would you like to snap a screenshot?"
    set chosenFrequency to result as number
    set chosenFrequencySeconds to chosenFrequency * 60

    display dialog "Great! I'll take " & chosenFrequency & " photos over the span of " & chosenDuration & " minutes."
    display dialog "Next, choose where you want these to end up."

    set FolderPath to POSIX path of (choose folder) as string

    display dialog "Sounds good! Just let me run in the background, and I'll snap away until the time is up."

    delay 1
  end run

  on idle
    set theCurrentDate to current date
    set shellCommand to "/usr/sbin/screencapture " & quoted form of (FolderPath & "Screen Shot" & (theCurrentDate as text) & ".png")
    do shell script shellCommand

    if theCurrentDate is greater than or equal to quitTime then tell me to quit
    return chosenFrequencySeconds
  end idle

  on reopen
    if running of me then
        tell me
            activate
            display alert "Already running"
        end tell
    end if
  end reopen