2013-03-16 80 views
1

我不知道我在做什麼錯誤,我也不知道如何使用NSTimer ...我只是想運行一個簡單的應用程序,當你點擊運行...它循環一個腳本的部分(這是我想獲得的NSTimer做,然後當你單擊停止...停止NSTimer在Applescript OBJC不工作

到目前爲止,我有這個...

current application's NSTimer's timerWithTimeInterval_target_selector_userInfo_repeats_(1, me, "runOLEDWMESerial:", missing value, yes) 
on runOLEDWMESerial_(sender) 
    set player_active to false 
    set theString to "Error;00:00:00:00" 
    tell application "Finder" 
     if (get name of every process) contains "NicePlayer" then set player_active to true 
    end tell 
    if player_active then 
     try 
      tell application "NicePlayer" 
       if ((count of movies) is not 0) then 
        tell front playlist 
         set theName to name 
         set curSeconds to elapsed time as integer 
         set theSeconds to duration as integer 
         set theHours to theSeconds div 3600 
         set theSeconds to theSeconds - theHours * 3600 
         set theMinutes to theSeconds div 60 
         set theSeconds to theSeconds - theMinutes * 60 
         set theDuration to "" 
         if theHours > 0 then 
          if theHours < 10 then set theHours to "0" & theHours 
          set theDuration to theHours & ":" 
         end if 
         if theMinutes < 10 then set theMinutes to "0" & theMinutes 
         if theSeconds < 10 then set theSeconds to "0" & theSeconds 
         set theDuration to theDuration & theMinutes & ":" & theSeconds 
         set curHours to curSeconds div 3600 
         set curSeconds to curSeconds - curHours * 3600 
         set curMinutes to curSeconds div 60 
         set curSeconds to curSeconds - curMinutes * 60 
         set curTime to "" 
         if curHours > 0 then 
          if curHours < 10 then set curHours to "0" & curHours 
          set curDuration to curHours & ":" 
         end if 
         if curMinutes < 10 then set curMinutes to "0" & curMinutes 
         if curSeconds < 10 then set curSeconds to "0" & curSeconds 
         set curTime to curTime & curMinutes & ":" & curSeconds 
         set theString to theName & ";" & curTime & ";" & theDuration 
        end tell 
       end if 
      end tell 
     on error errmsg 
      set theString to "Error2;00:00;00:00" 
     end try 
    end if 
    textName's setStringValue_(theName) 
    textTime's setStringValue_(curTime & "/" & theDuration) 
end runOLEDWMESerial_ 

是的,我確定我的代碼是醜陋的,但本質上這將通過串行端口發送數據,這是這裏的主要目標,但是現在,我只是想讓窗口迴應正在播放的內容......

因此,runOLEDWMESerial綁定到運行按鈕...當您單擊運行按鈕時,循環觸發一次,但然後停止...如果我在runOLEDWMESERIAL_(發件人)的所有內容中添加一個延遲(1)它的工作原理,但也變得無法在ui響應,我無法阻止它...所以有人可以幫助我理解爲什麼NSTimer不工作

回答

0

我其實已經想通了......我有它backwords ... runOLEDWMESerial_是按鈕......但是如果你這樣做......我也必須刪除試用部分,因爲它不允許它正常工作

on ClickButton_(sender) 
    current application's NSTimer's timerWithTimeInterval_target_selector_userInfo_repeats_(1, me, "runOLEDWMESerial:", missing value, yes) 
end Clickbutton_ 
    on runOLEDWMESerial_() 
    set player_active to false 
    set theString to "Error;00:00:00:00" 
    tell application "Finder" 
     if (get name of every process) contains "NicePlayer" then set player_active to true 
    end tell 
    if player_active then 

     tell application "NicePlayer" 
      if ((count of movies) is not 0) then 
       tell front playlist 
        set theName to name 
        set curSeconds to elapsed time as integer 
        set theSeconds to duration as integer 
        set theHours to theSeconds div 3600 
        set theSeconds to theSeconds - theHours * 3600 
        set theMinutes to theSeconds div 60 
        set theSeconds to theSeconds - theMinutes * 60 
        set theDuration to "" 
        if theHours > 0 then 
         if theHours < 10 then set theHours to "0" & theHours 
         set theDuration to theHours & ":" 
        end if 
        if theMinutes < 10 then set theMinutes to "0" & theMinutes 
        if theSeconds < 10 then set theSeconds to "0" & theSeconds 
        set theDuration to theDuration & theMinutes & ":" & theSeconds 
        set curHours to curSeconds div 3600 
        set curSeconds to curSeconds - curHours * 3600 
        set curMinutes to curSeconds div 60 
        set curSeconds to curSeconds - curMinutes * 60 
        set curTime to "" 
        if curHours > 0 then 
         if curHours < 10 then set curHours to "0" & curHours 
         set curDuration to curHours & ":" 
        end if 
        if curMinutes < 10 then set curMinutes to "0" & curMinutes 
        if curSeconds < 10 then set curSeconds to "0" & curSeconds 
        set curTime to curTime & curMinutes & ":" & curSeconds 
        set theString to theName & ";" & curTime & ";" & theDuration 
       end tell 
      end if 
     end tell 
    end if 
    textName's setStringValue_(theName) 
    textTime's setStringValue_(curTime & "/" & theDuration) 
end runOLEDWMESerial_