applehelpwriter

Get list of Accessibility apps

Jul 5th, 2018
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###########################################################
  2. # ABOUT
  3. ###########################################################
  4. (*
  5.  
  6.  Phil Stokes -- 2018
  7.  applehelpwriter.com
  8.  sqwarq.com
  9.  
  10.  Full article: http://applehelpwriter.com/2018/07/09/accessing-tcc-db-without-privileges/
  11.  
  12. *)
  13. ###########################################################
  14. # DESCRIPTION
  15. ###########################################################
  16. (*
  17.  
  18. get list of apps that are checked in Accessibility without requiring admin privileges
  19. works on 10.11 -> 10.14 beta
  20.  
  21. *)
  22. ###########################################################
  23. # USAGE
  24. ###########################################################
  25. (*
  26.  
  27. The script runner must itself be included in the list of Accessibility apps in order to work.
  28. Run the script and view the result in your Script Editor
  29.  
  30. *)
  31. ###########################################################
  32. # IMPORT STATEMENTS
  33. ###########################################################
  34.  
  35. use AppleScript version "2.4" -- Yosemite (10.10) or later
  36. use scripting additions
  37.  
  38. ###########################################################
  39. # VARIABLES
  40. ###########################################################
  41.  
  42. set os to do shell script "sw_vers -productVersion"
  43.  
  44.  
  45. ###########################################################
  46. # HANDLERS
  47. ###########################################################
  48.  
  49. on getAccessibilityApps(vers) -- main workhorse to manipulate Sys Prefs app and extract info
  50.    
  51.     local appsList
  52.     local canQuit
  53.     set appsList to {}
  54.     set canQuit to false
  55.    
  56.     (* more than one instance of Sys Prefs may target the wrong window *)
  57.     try
  58.         tell application "System Preferences" to quit
  59.     end try
  60.  
  61.     (* launch a new instance of Sys Prefs as quietly as we can *)
  62.     do shell script "open -Fg /Applications/System\\ Preferences.app"
  63.  
  64.     delay 1 -- wait for the ui to load
  65.    
  66.     tell application "System Preferences"
  67.         reveal pane id "com.apple.preference.security"
  68.         delay 1
  69.     end tell
  70.     try
  71.         tell application "System Events"
  72.             tell its application process "System Preferences"
  73.                 tell its window "Security & Privacy"
  74.                     tell its tab group 1
  75.                         if vers > 11 then -- Sierra and later use a different interface
  76.                             tell its UI element "Privacy" to click
  77.                             tell its UI element 5
  78.                                 tell its table 1
  79.                                     set uiNum to 7
  80.                                     if vers > 13 then
  81.                                         set uiNum to 8 -- Mojave moves Accessibility down a row
  82.                                     end if
  83.                                     tell its UI element uiNum
  84.                                         tell its attribute "AXSelected"
  85.                                             set its value to true -- click the Accessibility row
  86.                                         end tell
  87.                                     end tell
  88.                                 end tell
  89.                             end tell
  90.                         end if
  91.                         tell its group 1
  92.                             tell its scroll area 1
  93.                                 tell its table 1
  94.                                     tell its attribute "AXRows"
  95.                                         set theRows to value
  96.                                     end tell
  97.                                     set numRows to count of theRows
  98.                                     repeat with i from 1 to numRows
  99.                                         tell its row i
  100.                                             tell its UI element
  101.                                                 (* is the item's checkbox checked or not? *)
  102.                                                 set val to value of its checkbox 1
  103.                                                 if val's first item is 1 then
  104.                                                     set end of appsList to name
  105.                                                 end if
  106.                                             end tell
  107.                                         end tell
  108.                                     end repeat
  109.                                     set canQuit to true
  110.                                 end tell
  111.                             end tell
  112.                         end tell
  113.                     end tell
  114.                 end tell
  115.             end tell
  116.         end tell
  117.         (* lets clean up after ourselves *)
  118.         if canQuit is true then
  119.         try
  120.             tell application "System Preferences" to quit
  121.         end try
  122.         end if
  123.     on error errMsg
  124.         set end of appsList to (errMsg as text)
  125.     end try
  126.     return appsList
  127. end getAccessibilityApps
  128.  
  129.  
  130. on getVersFor(op) -- get the minor version number as a whole integer
  131.     set o to offset of "." in op
  132.     set postStr to text (o + 1) thru -1 of op
  133.     try
  134.         set o to offset of "." in postStr
  135.         set numb to text 1 thru o in postStr as integer
  136.     on error
  137.         set numb to text 1 thru -1 in postStr as integer
  138.     end try
  139.     return numb
  140. end getVersFor
  141.  
  142. on checkForError(aList) -- make sure the script runner has access or return error as result
  143.     set returnList to aList
  144.     if aList's item 1 contains "not allowed assistive access" then set returnList to aList's item 1 as text
  145.     return returnList
  146. end checkForError
  147.  
  148.  
  149. on disembedList(aList) -- disembed nested lists one level deep
  150.     set returnList to {}
  151.     repeat with listItem in aList
  152.         if class of listItem is list then
  153.             repeat with i from 1 to count of listItem
  154.                 set sublist to item i of listItem
  155.                 set end of returnList to sublist
  156.             end repeat
  157.         else
  158.             set end of returnList to listItem
  159.         end if
  160.     end repeat
  161.     return returnList
  162. end disembedList
  163.  
  164.  
  165. ###########################################################
  166. # COMMANDS
  167. ########################################################
  168.  
  169. set theList to getAccessibilityApps(getVersFor(os))
  170. set theList to disembedList(theList)
  171. checkForError(theList)
  172.  
  173.  
  174. ###########################################################
  175. #EOF
Add Comment
Please, Sign In to add comment