顯示具有 AppleScript 標籤的文章。 顯示所有文章
顯示具有 AppleScript 標籤的文章。 顯示所有文章

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 vmStats ≤ allowedMinFreeMemory 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

2015年6月14日 星期日

AppleScript: Checking memory every 30 sec. If it is about to out of memory then restart OS

since: 2015/06/14
update: 2015/06/14


A. 撰寫 Applescript:
    1. 開啟 Automator:

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

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




    5. 修改如下:
on run {input, parameters}
   
    repeat
       

        # query memory free (MB)
        set vmStats to (text 12 thru -2 of (do shell script "vm_stat | grep 'Pages free'")) * 4096 / 1024 / 1024
        

        # if vmStats <= 500 MB
        if vmStats <= 500 then
       
            # show
dialog message (for debug used)           
            #display dialog vmStats with title "
Memory Free (MB)" buttons {"OK"} default button 1
           
            #
kill your app   
            tell application "System Events"
                set ProcessList to name of every process
                    #
"my_app" is your app name                   
                    if "my_app" is in ProcessList then
                        set ThePID to unix id of process "my_app"
                        # kill
"my_app" process                      
                        do shell script "kill -KILL " & ThePID
                    end if
            end tell
   
            # restart the OS
            tell application "Finder"
                restart
            end tell
   
        end if
       
        #
delay 30 second       
        delay 30
       
    end repeat

   
    return input
end run

2015年6月10日 星期三

Processing: Get Free Memory Info With AppleScript

since: 2015/06/10
update: 2015/06/10

reference:
1. Troubleshooting · processing/processing Wiki

2. Applescript show free memory | Apple Support Communities
3. AppleScript: Essential Sub-Routines
4. io - How do I write to a text file using AppleScript? - Stack Overflow

A. 撰寫 Applescript:
    1. 開啟 Automator:

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

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




    5. 修改如下:
on run {input, parameters}
   
   
    # get the number of free pages using 'vm_stat' and multiply by the page size
    set vmStats to (text 12 thru -2 of (do shell script "vm_stat | grep 'Pages free'")) * 4096
    # display dialog vmStats with title "Memory usage" buttons {"OK"} default button 1
   
    # write to file
    set someText to number_to_string(vmStats)
   
    set textFile to "/Lanli/RD/Projects/Processing/memory_test/free_mem.txt"
    do shell script "echo  " & quoted form of someText & " >  " & quoted form of textFile

   
   
    return input
end run


# A sub-routine that converts numeric values to strings
on number_to_string(this_number)
    set this_number to this_number as string
    if this_number contains "E+" then
        set x to the offset of "." in this_number
        set y to the offset of "+" in this_number
        set z to the offset of "E" in this_number
        set the decimal_adjust to characters (y - (length of this_number)) thru ¬
            -1 of this_number as string as number
        if x is not 0 then
            set the first_part to characters 1 thru (x - 1) of this_number as string
        else
            set the first_part to ""
        end if
        set the second_part to characters (x + 1) thru (z - 1) of this_number as string
        set the converted_number to the first_part
        repeat with i from 1 to the decimal_adjust
            try
                set the converted_number to ¬
                    the converted_number & character i of the second_part
            on error
                set the converted_number to the converted_number & "0"
            end try
        end repeat
        return the converted_number
    else
        return this_number
    end if
end number_to_string


    6. 檔案 > 儲存: 
        /Lanli/RD/Projects/Processing/memory_test/free_mem.app

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

B. 開啟 Processing 撰寫程式如下:
import java.util.Date;

String words1;
String words2;
String words3;
String words4;

long allocated;
long free;
long maximum;
long checkTimer;

String[] config;
float free_mem;

void setup() {
  frameRate(15);
  size(400, 400);
  textSize(24);
 
  words1 = "";
  words2 = "";
  words3 = "";
  words4 = "";
 
  checkTimer = (new Date()).getTime();

  //  File[] roots = File.listRoots();
  //
  //  // For each filesystem root, print some info 
  //  for (File root : roots) {
  //    println("File system root: " + root.getAbsolutePath());
  //    println("Total space (bytes): " + root.getTotalSpace());
  //    println("Free space (bytes): " + root.getFreeSpace());
  //    println("Usable space (bytes): " + root.getUsableSpace());
  //  }

}

void draw() {
  background(0);

  if ((new Date()).getTime() - checkTimer >= 2000) {
    checkTimer = (new Date()).getTime();

    open("/Lanli/RD/Projects/Processing/memory_test/free_mem.app");
    //delay(250);
    config = loadStrings("free_mem.txt"); 
    free_mem = float(config[0]) / 1024 / 1024; 

    // The amount of memory allocated so far (usually the -Xms setting)
    allocated = Runtime.getRuntime().totalMemory() / 1024 /1024;

    // Free memory out of the amount allocated (value above minus used)
    free = Runtime.getRuntime().freeMemory() / 1024 /1024;

    // The maximum amount of memory that can eventually be consumed
    // by this application. This is the value set by the Preferences
    // dialog box to increase the memory settings for an application.

    maximum = Runtime.getRuntime().maxMemory() / 1024 / 1024; 

    words1 = "app allocated = "+ allocated + " MB";
    words2 = "app free = "+ free + " MB";
    words3 = "app maximum = "+ maximum + " MB";
    words4 = "sys free_mem = "+ free_mem + " MB";
  }

  text(words1, 10, 25, 540, 300);
  text(words2, 10, 125, 540, 300);
  text(words3, 10, 225, 540, 300);
  text(words4, 10, 325, 540, 300);

  System.gc();
}

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

C. 執行結果:
       備註: 實體記憶體 (8.00 GB) - 記憶體用量 (2.45 GB) - 快取(2.59 GB)
                 = 2.96 GB 接近 3015.1016 MB / 1024 = 2.944 GB

2015年5月28日 星期四

Processing: Call AppleScript to Kill Process & Restart System

since: 2015/05/28
update: 2015/05/28


A. 撰寫 AppleScript:
    1. 開啟 Automator:

    2. Automator > 檔案 > 新增 > 應用程式 > 選擇:

    3. 工具程式 > 執行 AppleScript > 拖拉到右方

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

    5. 修改如下:
on run {input, parameters}

    tell application "System Events"
        set ProcessList to name of every process
       
        if "APP_client" is in ProcessList then
            set ThePID to unix id of process "APP_client"
            do shell script "kill -KILL " & ThePID
        end if
       
        if "APP_server" is in ProcessList then
            set ThePID to unix id of process "
APP_server"
            do shell script "kill -KILL " & ThePID
        end if  
     
    end tell
   
    tell application "Finder"
        restart
    end tell   
   
    return input
end run


    6. 檔案 > 儲存: 
        /Lanli/RD/myScript.app

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

B. 開啟 Processing 撰寫程式如下:
void setup() {
  size(200, 200);
}

void draw() {
  // draw() must be present for mousePressed() to work
}

void mousePressed() {
  open("/Lanli/RD/myScript.app");
  exit();
}