applescript_superior

Binary Translator (x8-3.2)

Aug 23rd, 2020 (edited)
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Please note that this is just the parent script in a compiled application. This script utilizes property lists to lookup the associated ASCII code. Running this by itself will result in several errors.
  2.  
  3. -- Dropbox link to full application: https://www.dropbox.com/s/7kyt2caa0i3xbo0/Binary%20Translator%20%28x8-3.2%29.zip?dl=0
  4.  
  5. -- Initializes variables within closed handlers and sets paths to libraries (0)
  6. global byte
  7. global x1
  8. global x2
  9. global x3
  10. global x4
  11. global x5
  12. global x6
  13. global x7
  14. global x8
  15.  
  16. global decimal
  17. global decimalClass
  18. global decimalClassFormal
  19. global code
  20.  
  21. global x1a
  22. global x2a
  23. global x1b
  24. global x2b
  25. global x1c
  26. global x2c
  27. global r1
  28. global r2
  29. global r3
  30. global hex
  31.  
  32. global x1d
  33. global x2d
  34. global x1e
  35. global x2e
  36. global x1f
  37. global x2f
  38. global r4
  39. global r5
  40. global r6
  41. global octal
  42.  
  43. global encoding
  44. global action
  45. global title
  46. set title to "Binary Translator (x8-3.2)"
  47. set encoding to "Mixed"
  48.  
  49. global syscodesPath
  50. global printcodesPath
  51. global extracodesPath
  52. global hexcodesPath
  53. set syscodesPath to (path to me as string) & "Contents:Resources:library:syscodes.plist"
  54. set printcodesPath to (path to me as string) & "Contents:Resources:library:printcodes.plist"
  55. set extracodesPath to (path to me as string) & "Contents:Resources:library:extracodes.plist"
  56. set hexcodesPath to (path to me as string) & "Contents:Resources:library:hexcodes.plist"
  57.  
  58. global restorePath
  59. set restorePath to (path to me as string) & "Contents:Resources:restore.app"
  60.  
  61. on byteInput()
  62.     -- Inputs the byte (1)
  63.     set inputByte to ((round (random number from 0 to 1)) & (round (random number from 0 to 1)) & (round (random number from 0 to 1)) & (round (random number from 0 to 1)) & (round (random number from 0 to 1)) & (round (random number from 0 to 1)) & (round (random number from 0 to 1)) & (round (random number from 0 to 1)))
  64.     set inputByte to (inputByte) as string
  65.     set byte to text returned of (display dialog "Input Byte (x8)" default answer inputByte with icon 1 buttons {"Cancel", "Translate"} default button 2 with title title) as string
  66.     translateBytetoDecimal()
  67. end byteInput
  68. byteInput()
  69.  
  70. on translateBytetoDecimal()
  71.     -- Checks that the byte is neither greater than or less than eight digits in length (2)
  72.     if ((number of characters of byte) is greater than 8) or ((number of characters of byte) is less than 8) then
  73.         beep
  74.         display alert "An error occured." message "The number of digits cannot be less than or greater than eight. Please try again." & return & return & "Previous Input: " & byte & return & "(Error -001)" as critical buttons {"OK"} default button 1
  75.         byteInput()
  76.     end if
  77.    
  78.     -- Separates the byte into individual digits (3)
  79.     set x8 to character 1 of byte
  80.     set x7 to character 2 of byte
  81.     set x6 to character 3 of byte
  82.     set x5 to character 4 of byte
  83.     set x4 to character 5 of byte
  84.     set x3 to character 6 of byte
  85.     set x2 to character 7 of byte
  86.     set x1 to character 8 of byte
  87.    
  88.     -- Translates digit one according to the ASCII table (4)
  89.     if x1 = "1" then
  90.         set x1 to 1
  91.     else if x1 = "0" then
  92.         set x1 to 0
  93.     end if
  94.    
  95.     -- Translates digit two according to the ASCII table (5)
  96.     if x2 = "1" then
  97.         set x2 to 2
  98.     else if x2 = "0" then
  99.         set x2 to 0
  100.     end if
  101.    
  102.     -- Translates digit three according to the ASCII table (6)
  103.     if x3 = "1" then
  104.         set x3 to 4
  105.     else if x3 = "0" then
  106.         set x3 to 0
  107.     end if
  108.    
  109.     -- Translates digit four according to the ASCII table (7)
  110.     if x4 = "1" then
  111.         set x4 to 8
  112.     else if x4 = "0" then
  113.         set x4 to 0
  114.     end if
  115.    
  116.     -- Translates digit five according to the ASCII table (8)
  117.     if x5 = "1" then
  118.         set x5 to 16
  119.     else if x5 = "0" then
  120.         set x5 to 0
  121.     end if
  122.    
  123.     -- Translates digit six according to the ASCII table (9)
  124.     if x6 = "1" then
  125.         set x6 to 32
  126.     else if x6 = "0" then
  127.         set x6 to 0
  128.     end if
  129.    
  130.     -- Translates digit seven according to the ASCII table (10)
  131.     if x7 = "1" then
  132.         set x7 to 64
  133.     else if x7 = "0" then
  134.         set x7 to 0
  135.     end if
  136.    
  137.     -- Translates digit eight according to the ASCII table (11)
  138.     if x8 = "1" then
  139.         set x8 to 128
  140.     else if x8 = "0" then
  141.         set x8 to 0
  142.     end if
  143.    
  144.     -- Adds the resulting integers together to create the decimal (12)
  145.     set decimal to (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8) as number
  146.    
  147.     -- Determines the class of the decimal (13)
  148.     if (decimal ≥ 0 and decimal < 32) then
  149.         set decimalClass to "syscodes" as string
  150.         set decimalClassFormal to "System Codes"
  151.     else if (decimal > 31 and decimal < 128) then
  152.         set decimalClass to "printcodes" as string
  153.         set decimalClassFormal to "Printable Codes"
  154.     else if (decimal > 127 and decimal < 256) then
  155.         set decimalClass to "extracodes" as string
  156.         set decimalClassFormal to "Special Codes"
  157.     end if
  158.    
  159.     -- Refers to the appropriate lookup table to locate the associtated code (14)
  160.     set decimal to (decimal) as string
  161.     try
  162.         if decimalClass = "syscodes" then
  163.             tell application "System Events"
  164.                 tell property list file syscodesPath
  165.                     set code to (value of property list item decimal)
  166.                 end tell
  167.             end tell
  168.         else if decimalClass = "printcodes" then
  169.             tell application "System Events"
  170.                 tell property list file printcodesPath
  171.                     set code to (value of property list item decimal)
  172.                 end tell
  173.             end tell
  174.         else if decimalClass = "extracodes" then
  175.             tell application "System Events"
  176.                 tell property list file extracodesPath
  177.                     set code to (value of property list item decimal)
  178.                 end tell
  179.             end tell
  180.         end if
  181.     on error
  182.         locationError()
  183.     end try
  184.     translateDecimaltoHex()
  185. end translateBytetoDecimal
  186. translateBytetoDecimal()
  187.  
  188. on translateDecimaltoHex()
  189.     -- Third character of hex
  190.     set x1a to (decimal / 16)
  191.     set x2a to (round (decimal / 16) rounding down)
  192.     set r3 to (round (x1a - x2a) * 16)
  193.    
  194.     -- Second character of hex
  195.     set x1b to (x2a / 16)
  196.     set x2b to (round (x1a / 16) rounding down)
  197.     set r2 to (round (x1b - x2b) * 16)
  198.    
  199.     -- First character of hex
  200.     set x1c to (x2b / 16)
  201.     set x2c to (round (x1b / 16) rounding down)
  202.     set r1 to (round (x1c - x2c) * 16)
  203.    
  204.     -- Converts third character to hexadecimal character
  205.     try
  206.         tell application "System Events"
  207.             set r3 to (r3) as string
  208.             tell property list file hexcodesPath
  209.                 set r3 to (value of property list item r3)
  210.             end tell
  211.         end tell
  212.        
  213.         -- Converts second character to hexadecimal character
  214.         tell application "System Events"
  215.             set r2 to (r2) as string
  216.             tell property list file hexcodesPath
  217.                 set r2 to (value of property list item r2)
  218.             end tell
  219.         end tell
  220.        
  221.         -- Converts first character to hexadecimal character
  222.         tell application "System Events"
  223.             set r1 to (r1) as string
  224.             tell property list file hexcodesPath
  225.                 set r1 to (value of property list item r1)
  226.             end tell
  227.         end tell
  228.     on error
  229.         locationError()
  230.     end try
  231.    
  232.     -- Compiles hexadecimal
  233.     set hex to (r1 & r2 & r3)
  234.     set hex to (hex) as string
  235.     translateDecimaltoOctal()
  236. end translateDecimaltoHex
  237. translateDecimaltoHex()
  238.  
  239. on translateDecimaltoOctal()
  240.     -- Third character of octal
  241.     set x1d to (decimal / 8)
  242.     set x2d to (round (decimal / 8) rounding down)
  243.     set r6 to (round (x1d - x2d) * 8)
  244.    
  245.     -- Second character of octal
  246.     set x1e to (x2d / 8)
  247.     set x2e to (round (x1d / 8) rounding down)
  248.     set r5 to (round (x1e - x2e) * 8)
  249.    
  250.     -- First character of octal
  251.     set x1f to (x2e / 8)
  252.     set x2f to (round (x1e / 8) rounding down)
  253.     set r4 to (round (x1f - x2f) * 8)
  254.    
  255.     -- Compiles octal
  256.     set octal to (r4 & r5 & r6)
  257.     set octal to (octal) as string
  258. end translateDecimaltoOctal
  259. translateDecimaltoOctal()
  260.  
  261. -- Outputs the translations plus a couple easter eggs (15)
  262. repeat
  263.     set action to button returned of (display alert byte message "Code: " & code & return & "Encoding: " & encoding & return & "Decimal: " & decimal & return & "Decimal Class: " & decimalClassFormal & return & "Hexadecimal: " & hex & return & "Octal: " & octal buttons {"Credits & Information", "Exit", "Back to Input"} default button 3)
  264.     if action = "Credits & Information" then
  265.         repeat
  266.             set action to button returned of (display alert title message "Developed by Blake Zito" & return & "AppleScript 2.7 (Script Editor)" & return & return & "Description:" & return & "Translates 8-bit binary into its decimal, hexadecimal, octal, and character forms using standard 8-bit ASCII encoding without the use of any primary lookup tables." buttons {"Experimental Features", "Back to Output"} default button 2)
  267.             if action = "Experimental Features" then
  268.                 repeat
  269.                     set theResult to (display dialog "Enter Password (D-J-S-B)" default answer "" with icon 1 buttons {"Back to Credits & Information", "Unlock"} default button 2 with title "Experimental Features" with hidden answer)
  270.                     set answer to (text returned of theResult) as string
  271.                     set action to (button returned of theResult) as string
  272.                     if action = "Back to Credits & Information" then
  273.                         exit repeat
  274.                     else if action = "Unlock" then
  275.                         tell application "System Events"
  276.                             tell property list file hexcodesPath
  277.                                 set greekMessage to (value of property list item "16")
  278.                                 if answer = (value of property list item "17") then
  279.                                     display alert "4 8 15 16 23 42" message greekMessage buttons {"Λορεμ"} default button 1
  280.                                     open location "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
  281.                                 else
  282.                                     beep
  283.                                 end if
  284.                             end tell
  285.                         end tell
  286.                     end if
  287.                     quit
  288.                 end repeat
  289.             else if action = "Back to Output" then
  290.                 exit repeat
  291.             end if
  292.         end repeat
  293.     else if action = "Exit" then
  294.         quit
  295.     else if action = "Back to Input" then
  296.         byteInput()
  297.     end if
  298. end repeat
  299.  
  300. -- Displays alert in case of location error and attempts to restore libraries (16)
  301. on locationError()
  302.     beep
  303.     set action to button returned of (display alert "An error occured." message "Some important files couldn't be accessed." & return & "(Error -002)" & return & return & "Restore the libraries to resolve this issue. Restoring will restart the app." as critical buttons {"Restore Libraries"} default button 1)
  304.     open application restorePath
  305. end locationError
  306. locationError()
Add Comment
Please, Sign In to add comment