所以,我正在创建一个应用程序,它会定期截取屏幕截图,并且可以定制一段时间。我很难用它,因为当我在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
结束
答案 0 :(得分:2)
以下是使用“on idle”处理程序编写代码的方法(我没有对其进行测试)。请记住将其保存为“保持打开状态的应用程序”。请注意,您告诉on idle处理程序在处理程序的最后一行中以秒为单位重复。另请注意,我们在计算日期退出应用程序而不是重复多次。你基本上可以创建一个计数器来计算空闲处理程序的循环,但日期方法更容易。最后请注意我们有全局变量。这使得在运行处理程序中启动的变量可用于on idle处理程序。
祝你好运。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
答案 1 :(得分:0)
也许您应该尝试实现reopen
处理程序。我已经用regulus的代码完成了它,它对我有用,如果applet安装在dock中,那么它会启动第一个时间,如果稍后点击它,你会收到一条消息它已经在运行了。 reopen
处理程序可能有一些superfluos代码,比如测试应用程序是否正在运行,并激活它,因为只有在应用程序已经运行时才会触发重新打开处理程序。但我会为你留下,至少它现在不会执行运行处理程序。 :)
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