2016年1月15日 星期五

AppleScript: Repeat Checking App Status

since: 2016/01/15
update: 2016/01/15


A. 新增 AppleScript:
    1. 開啟 Automator:

   
     2. Automator > 檔案 > 新增 > 應用程式 > 選擇:
    
     3. 工具程式 > 執行 AppleScript > 拖拉到右方

    4. 預設程式碼:
on run {input, parameters}
   
    (* Your script goes here *)
   
    return input
end run


-----------------------------------------------------------------------------------------------

B. 修改 AppleScript:
on run {input, parameters}
   
    #
    # start: variables defined
    #
   
    # script repeat delay time (second)

    set repeatDelayTime to 180
   
    # dateTimeFile Path
    # /Lanli/RD/project/openFrameworks/of_v0.9.0/apps/myApps/myFirstApp/bin/data/

    set dateTimeFilePath to (("Lanli:RD:project:openFrameworks:of_v0.9.0:apps:myApps:myFirstApp:bin:data:") as text)
   
    # dateTimeFile Name
    set dateTimeFileName to "dateTime.txt"
   
    # completed dateTimeFile
    set dateTimeFile to dateTimeFilePath & dateTimeFileName
   
    # allow time difference to sysDateTime & appUpdateTime (second)

    set allowedSecondDiff to 120


    # allowed min free memory (MB)
    set allowedMinFreeMemory to 0
   
    # my app name
    set myAppName to "myFirstApp"

    # reboot system or not
    set reboot to false   

    #
    # end: variables defined
    #

   
    ##########################################################
   
    #
    # start script repeat
    #

    repeat
       
        # delay 180 second  
        # delay 180 
  
        delay repeatDelayTime
       
        # start: repeat task
       

##########################################################       
        # 
        # check 01:  app update status
        #
        # Read lines from file.

        set fileLines to paragraphs of (read file dateTimeFile as «class utf8»)
       
        # Loop over lines read and copy each to the clipboard.
        repeat with perLine in fileLines
            set the clipboard to perLine
            #display alert (the clipboard)
            set appUpdateTime to (the clipboard) as integer
            exit repeat # we just need read one line here
        end repeat
       
        # get sysDateTime
        set sysDateTime to nowTimeFormat() as integer
       
        # check time diff
        if (appUpdateTime + allowedSecondDiff) ≥ sysDateTime then
            # display dialog "you pass" with title "check app update status" buttons {"OK"} default button 1
        else
            # display dialog "reboot" with title "check app update status" buttons {"OK"} default button 1
            set reboot to true
        end if
       
       
        #
        # check 02:  system memory free
        #
       
        # query memory free (MB)

        set vmStats to (text 12 thru -2 of (do shell script "vm_stat | grep 'Pages free'")) * 4096 / 1024 / 1024
        # show free Memory
        #display dialog vmStats with title "Memory Free (MB)" buttons {"OK"} default button 1

       
        # if vmStats <= 0 MB
        if vmStatsallowedMinFreeMemory then
            set reboot to true
        end if
       
        # for test
        #set reboot to false

       
        #
        # reboot system or not
        #

        if reboot is true then
            #display dialog "reboot" with title "reboot system or not" buttons {"OK"} default button 1
           
            #step 1: kill your app 
            tell application "System Events"
                set ProcessList to name of every process
               
                if myAppName is in ProcessList then
                    set ThePID to unix id of process myAppName
                    do shell script "kill -KILL " & ThePID
                end if
            end tell
           
            #step 2: reboot system
            tell application "Finder"
                restart
            end tell
           
        else
            #display dialog "pass" with title "reboot system or not" buttons {"OK"} default button 1
        end if
       
       
        # end: repeat task
       

##########################################################       
        # exit script repeat: for only once execute
        #exit repeat
       
        #   
        # end script repeat
        #  
     
    end repeat
   
    return input
end run



# nowTimeFormat
on nowTimeFormat()
   
    set theDate to current date
    set y to text -4 thru -1 of ("0000" & (year of theDate))
    set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
    set d to text -2 thru -1 of ("00" & (day of theDate))
    set hh to text -2 thru -1 of ("00" & (hours of theDate))
    set mm to text -2 thru -1 of ("00" & (minutes of theDate))
    set ss to text -2 thru -1 of ("00" & (seconds of theDate))
   
    # 2016-01-15-10-56-46
    #return y & "-" & m & "-" & d & "-" & hh & "-" & mm & "-" & ss
    # 20160115105646

    return y & m & d & hh & mm & ss
   
end nowTimeFormat

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。