我只想执行一次特定事件

I want to Perform particular event only once

我有一个按钮,它执行特定的任务。我只想在第二次单击鼠标时执行特定任务一次 我不想执行特定任务。现在,在完成任务后,我只是禁用按钮,它是否是解决上述问题的任何替代方法。我正在使用以下代码

global e
on mouseUp 
   replace "$" with "{\XXdollarXX}" in field "MytextField" 
   put the text of field "MytextField" into ss 
   put "" into yy 
   put 0 into tmp 
   repeat with i = 1 to the number of chars in ss 
      if char i of ss contains "$" then 
         add 1 to tmp 
         if tmp = 1 then 
            put CR & char i of ss after yy 
         else 
            put char i of ss && CR after yy 
            put 0 into tmp 
         end if 
      else 
         put char i of ss after yy 
      end if 
   end repeat 
   put yy into the field "MytextField"
   disable me
end mouseUp

我认为你的问题是脚本的持续时间。如果一个脚本需要很长时间才能 运行,用户可能会第二次点击,认为什么事都没发生。通常,这会执行脚本两次。您可以使用 wait with messages 和本地声明的变量来防止这种情况。

local lBusy
on mouseUp
     // first provide a way to force-unlock the script
     if the shiftKey is down then put false into lBusy
     if lBusy is true then
          // warn that the script is running
          beep
          answer error "Please wait for the script to finish."
     else
          // lock the script
          put true into lBusy
          repeat with x = 1 to 100000
               // just do something that takes a lot of time
               add 1 to mySampleVar
               put "The current value is" && mySampleVar // msg box
               // make the loop non-blocking
               wait 0 millisecs with messages
          end repeat
          // unlock the script
          put false into lBusy
     end if
end mouseUp

脚本启动时,lBusy 设置为 true。在脚本的末尾,lBusy 再次设置为 false。只要 lBusytrue,如果您单击按钮,脚本就不会 运行。

如果您的按钮都使用 "mouseUp" 消息开始它们的操作,请将它们分组并将该处理程序放在组脚本中。然后你可以区分目标按钮和 运行 适当的代码,与每个按钮相关。但是由于所有操作都在一个处理程序中,您可以锁定来自该组中任何兄弟姐妹的任何此类消息。