Skip to content

Instantly share code, notes, and snippets.

@0xdevalias
Created December 19, 2023 05:00
Show Gist options
  • Save 0xdevalias/db8b0c8b626ccb9a775b806be673eb55 to your computer and use it in GitHub Desktop.
Save 0xdevalias/db8b0c8b626ccb9a775b806be673eb55 to your computer and use it in GitHub Desktop.
Some AppleScript automation snippets

AppleScript Automation Snippets

Check if script has 'Assistive Access' permission enabled

Check if app running the script has 'Assistive Access', and if not, prompts the user to enable it:

-- Function to check if assistive access is enabled
on hasAssistiveAccess()
	tell application "System Events"
		return UI elements enabled
	end tell
end hasAssistiveAccess

-- Get the name of the current application
tell application "System Events"
	set currentAppName to name of the first process whose frontmost is true
end tell

-- Main Script
if not hasAssistiveAccess() then
	-- display dialog "This script requires assistive access to interact with System Settings. Please grant access in System Preferences under Security & Privacy > Privacy > Accessibility." buttons {"OK"} default button 1
	display dialog "This script requires assistive access to interact with System Settings. Please grant access to '" & currentAppName & "' in System Preferences under Security & Privacy > Privacy > Accessibility." buttons {"OK"} default button 1
	return
end if

Enumerate 'System Settings' panes

Enumerate the name and ID of all of the panes within 'System Settings':

tell application "System Settings"
	set paneList to every pane
	set paneMap to {}
	
	repeat with aPane in paneList
		set paneID to the id of aPane
		set paneName to the name of aPane
		set end of paneMap to {id:paneID, name:paneName}
	end repeat
end tell

return paneMap

Which returns (manually formatted for easier reading):

{
  {id:"com.apple.Appearance-Settings.extension", name:"Appearance"},
  {id:"com.apple.Battery-Settings.extension*BatteryPreferences", name:"Battery"},
  {id:"com.apple.settings.PrivacySecurity.extension", name:"Privacy & Security"},
  {id:"com.apple.ExtensionsPreferences", name:"Extensions"},
  {id:"com.apple.Profiles-Settings.extension", name:"Profiles"},
  {id:"com.apple.BluetoothSettings", name:"Bluetooth"},
  {id:"com.apple.ScreenSaver-Settings.extension", name:"Screen Saver"},
  {id:"com.apple.Trackpad-Settings.extension", name:"Trackpad"},
  {id:"com.apple.Keyboard-Settings.extension", name:"Keyboard"},
  {id:"com.apple.systempreferences.AppleIDSettings*AppleIDSettings", name:"Glenn Grant"},
  {id:"com.apple.Lock-Screen-Settings.extension", name:"Lock Screen"},
  {id:"com.apple.Focus-Settings.extension", name:"Focus"},
  {id:"com.apple.Screen-Time-Settings.extension", name:"Screen Time"},
  {id:"com.apple.Sound-Settings.extension", name:"Sound"},
  {id:"com.apple.Passwords-Settings.extension", name:"Passwords"},
  {id:"com.apple.Touch-ID-Settings.extension*TouchIDPasswordPrefs", name:"Touch ID & Password"},
  {id:"com.apple.Siri-Settings.extension", name:"Siri & Spotlight"},
  {id:"com.apple.Network-Settings.extension", name:"Network"},
  {id:"com.apple.systempreferences.GeneralSettings", name:"General"},
  {id:"com.apple.SystemProfiler.AboutExtension", name:"About"},
  {id:"com.apple.Software-Update-Settings.extension", name:"Software Update"},
  {id:"com.apple.settings.Storage", name:"Storage"},
  {id:"com.apple.AirDrop-Handoff-Settings.extension", name:"AirDrop & Handoff"},
  {id:"com.apple.LoginItems-Settings.extension", name:"Login Items"},
  {id:"com.apple.Localization-Settings.extension", name:"Language & Region"},
  {id:"com.apple.Date-Time-Settings.extension", name:"Date & Time"},
  {id:"com.apple.Sharing-Settings.extension", name:"Sharing"},
  {id:"com.apple.Time-Machine-Settings.extension", name:"Time Machine"},
  {id:"com.apple.Transfer-Reset-Settings.extension", name:"Transfer or Reset"},
  {id:"com.apple.Startup-Disk-Settings.extension", name:"Startup Disk"},
  {id:"com.apple.Displays-Settings.extension", name:"Displays"},
  {id:"com.apple.WalletSettingsExtension", name:"Wallet & Apple Pay"},
  {id:"com.apple.ControlCenter-Settings.extension", name:"Control Centre"},
  {id:"com.apple.Internet-Accounts-Settings.extension", name:"Internet Accounts"},
  {id:"com.apple.Mouse-Settings.extension", name:"Mouse"},
  {id:"com.apple.Users-Groups-Settings.extension", name:"Users & Groups"},
  {id:"com.apple.Print-Scan-Settings.extension", name:"Printers & Scanners"},
  {id:"com.apple.Desktop-Settings.extension", name:"Desktop & Dock"},
  {id:"com.apple.Accessibility-Settings.extension", name:"Accessibility"},
  {id:"com.apple.Game-Center-Settings.extension", name:"Game Center"},
  {id:"com.apple.wifi-settings-extension", name:"Wi‑Fi"},
  {id:"com.apple.Notifications-Settings.extension", name:"Notifications"},
  {id:"com.apple.Wallpaper-Settings.extension", name:"Wallpaper"},
  {id:"cl.fail.lordkamina.SwiftDefaultApps", name:"SwiftDefaultApps"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment