applescript_superior

Binary Counter

Apr 15th, 2021 (edited)
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Created by applescript_superior on 17 April 2021.
  2. # The script counts in binary to any integer specified by the user with the use of some cool algorithms.
  3. # Algorithms (methods) of finding the binary number from an integer can be found here: https://www.instructables.com/How-to-Convert-Numbers-to-Binary/
  4.  
  5. ## enter in parameters for script to use
  6. set start_number to 1
  7. set end_number to 32
  8. set delay_time to 0.25
  9. set zero_alias to "0"
  10. set one_alias to "1"
  11. set line_width to 1
  12. set iteration_counter to true
  13. set complete_string to true
  14. ## insert any file path you want the cache file to be saved to (path cannot contain a space or special character)
  15. set cache_file_path to "Users:user:Desktop:cache.txt"
  16. set cache_file_posix_path to my convert_to_posix_path(cache_file_path)
  17. ##
  18.  
  19. ##
  20. global start_number
  21.  
  22. set version_number to "1.3.4"
  23. set program_title to "Binary Display"
  24. set iteration to 0
  25. global iteration
  26.  
  27. set line_return to ASCII character 10
  28. global line_return
  29. set cache_file to open for access file cache_file_path with write permission
  30. global cache_file
  31.  
  32. set tm_start to 0
  33. global tm_start
  34. set ts_start to 0
  35. global ts_start
  36.  
  37. set tm_end to 0
  38. global tm_end
  39. set ts_end to 0
  40. global ts_end
  41. ##
  42.  
  43. ##
  44. if iteration_counter = true then
  45.     set iteration_counter_string to "Enabled"
  46. else if iteration_counter = false then
  47.     set iteration_counter_string to "Disabled"
  48. end if
  49.  
  50. set complete_string_warning to false
  51. if (((number of characters of zero_alias) > 1) or ((number of characters of one_alias) > 1)) then
  52.     set complete_string to false
  53.     set complete_string_warning to true
  54.     set complete_string_warning_message to "** Complete String has been automatically disabled because the aliases you chose exceed one character. This feature will be implemented in a later update as a bug fix. Apologies for the inconvenience. **"
  55. end if
  56.  
  57. if complete_string = true then
  58.     set complete_string_string to "Enabled"
  59. else if complete_string = false then
  60.     set complete_string_string to "Disabled"
  61. end if
  62. ##
  63.  
  64. ##
  65. tell application "Terminal"
  66.     activate
  67.     do script "tail -f " & cache_file_posix_path & "" in window 1
  68. end tell
  69. delay 1
  70. ##
  71.  
  72. ##
  73. try
  74.     write "" & line_return to cache_file
  75.     write program_title & " v" & version_number & line_return to cache_file
  76.     write "(C)2021; applescript_superior" & line_return & line_return to cache_file
  77.    
  78.     write "Start Number:      | " & start_number & line_return to cache_file
  79.     write "End Number:        | " & end_number & line_return to cache_file
  80.     write "Delay Time:        | " & (delay_time * 1000) & " ms" & line_return to cache_file
  81.     write "Zero Alias:        | \"" & zero_alias & "\"" & line_return to cache_file
  82.     write "One Alias:         | \"" & one_alias & "\"" & line_return to cache_file
  83.     write "Line Width:        | " & line_width & line_return to cache_file
  84.     write "Iteration Counter: | " & iteration_counter_string & line_return to cache_file
  85.     write "Complete String:   | " & complete_string_string & line_return & line_return to cache_file
  86.     if complete_string_warning = true then
  87.         write complete_string_warning_message & line_return & line_return to cache_file
  88.         delay 4
  89.     end if
  90.     delay 1
  91.    
  92.     write "[3]" & line_return to cache_file
  93.     delay 1
  94.     write "[2]" & line_return to cache_file
  95.     delay 1
  96.     write "[1]" & line_return to cache_file
  97.     delay 1
  98.     write line_return to cache_file
  99. on error
  100.     my close_script()
  101. end try
  102. ##
  103.  
  104. ##
  105. set x to end_number
  106. set remainder to 0
  107. set remainder_list to {}
  108. repeat
  109.     set remainder to (round ((x / 2) - (round (x / 2) rounding down)) rounding up)
  110.     if remainder = 0 then
  111.         set remainder to zero_alias
  112.     else if remainder = 1 then
  113.         set remainder to one_alias
  114.     end if
  115.     set x to (round (x / 2) rounding down)
  116.     set remainder_list to (remainder_list & remainder)
  117.    
  118.     if x < 1 then
  119.         exit repeat
  120.     end if
  121. end repeat
  122. set total_digits to (number of items of (reverse of remainder_list))
  123. ##
  124.  
  125. ##
  126. try
  127.     repeat with end_number from start_number to end_number
  128.         set remainder to 0
  129.         set remainder_list to {}
  130.        
  131.         set iteration to (iteration + 1)
  132.        
  133.         repeat
  134.             set remainder to (round ((end_number / 2) - (round (end_number / 2) rounding down)) rounding up)
  135.             if remainder = 0 then
  136.                 set remainder to zero_alias
  137.             else if remainder = 1 then
  138.                 set remainder to one_alias
  139.             end if
  140.             set end_number to (round (end_number / 2) rounding down)
  141.             set remainder_list to (remainder_list & remainder)
  142.            
  143.             if end_number < 1 then
  144.                 exit repeat
  145.             end if
  146.         end repeat
  147.        
  148.         set output to (reverse of remainder_list) as string
  149.        
  150.         if complete_string = true then
  151.             set digits_needed to (total_digits - (number of characters of output))
  152.             repeat digits_needed times
  153.                 set output to (zero_alias & output) as string
  154.             end repeat
  155.         end if
  156.        
  157.         delay delay_time
  158.        
  159.         set eof of cache_file to 0
  160.        
  161.         repeat line_width times
  162.             write output to cache_file
  163.             if iteration_counter = true then
  164.                 write " [" & iteration & "]" & line_return to cache_file
  165.             else
  166.                 write line_return to cache_file
  167.             end if
  168.         end repeat
  169.     end repeat
  170. on error
  171.     set iteration to (iteration - 1)
  172.     my close_script()
  173. end try
  174. ##
  175.  
  176. ##
  177. my close_script()
  178.  
  179. on close_script()
  180.     write "OK; " & start_number & " to " & iteration & line_return & line_return to cache_file
  181.    
  182.     tell application "Terminal"
  183.         do shell script "killall tail"
  184.     end tell
  185.    
  186.     set eof of cache_file to 0
  187.     close access cache_file
  188.     error number -128
  189. end close_script
  190. ##
  191.  
  192. ##
  193. on convert_to_posix_path(input_path)
  194.     set search_string to ":"
  195.     set replace_string to "/"
  196.    
  197.     set AppleScript's text item delimiters to search_string
  198.     set input_path_items to every text item of input_path
  199.     set AppleScript's text item delimiters to replace_string
  200.     set input_path to input_path_items as string
  201.     set AppleScript's text item delimiters to ""
  202.    
  203.     set input_path to replace_string & input_path
  204.    
  205.     return input_path
  206. end convert_to_posix_path
  207. ##
Add Comment
Please, Sign In to add comment