r/applescript • • Nov 13 '22

macOS Ventura Internet Sharing in System Settings

I wrote a script for macOS Monterey to activate internet sharing within system preferences. Things have changed in the new system settings and I can't figure out the new element path to click internet sharing on/off. Does anyone know what path would work.

EDIT: Here's the code that works for me

use framework "Foundation"
use scripting additions

property pane_ids : {|AppleID|:2, |Family|:3, |Wi-Fi|:5, |Bluetooth|:6, |Network|:7, |Notifications|:9, |Sound|:10, |Focus|:11, |Screen Time|:12, |General|:14, |Appearance|:15, |Accessibility|:16, |Control Center|:17, |Siri & Spotlight|:18, |Privacy & Security|:19, |Desktop & Dock|:21, |Displays|:22, |Wallpaper|:23, |Screen Saver|:24, |Battery|:25, |Lock Screen|:27, |Touch ID & Password|:28, |Users & Groups|:29, |Passwords|:31, |Internet Accounts|:32, |Game Center|:33, |Wallet & ApplePay|:34, |Keyboard|:36, |Trackpad|:37, |Printers & Scanners|:38, |Java|:40}

on open_settings_to(settings_pane)
	set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
	set pane_index to (pane's valueForKey:settings_pane) as anything
	tell application "System Settings"
		activate
		delay 1
	end tell
	tell application "System Events"
		tell application process "System Settings"
			tell splitter group 1 of group 1 of window 1
				tell outline 1 of scroll area 1 of group 1
					if name of static text of UI element 5 = "Wi‑Fi" then
						select row pane_index
					else
						set pane_index to pane_index - 0
						select row pane_index
						
					end if
				end tell
			end tell
		end tell
	end tell
end open_settings_to


on toggle_internet_sharing()
	tell application "System Events"
		tell application process "System Settings"
			tell window 1
				tell group 2 of splitter group 1 of group 1
					repeat until button 1 of group 3 of scroll area 1 of group 1 exists
						delay 0
					end repeat
					tell button 1 of group 3 of scroll area 1 of group 1
						click
					end tell
					repeat until checkbox 7 of group 1 of scroll area 1 of group 1 of group 1 exists
						delay 0
					end repeat
					tell checkbox 7 of group 1 of scroll area 1 of group 1 of group 1
						click
					end tell
				end tell
			end tell
		end tell
	end tell
end toggle_internet_sharing

on run {}
	open_settings_to("General")
	toggle_internet_sharing()
	quit application "System Settings"
end run
1 Upvotes

2 comments sorted by

1

u/Son_of_a_Shepherd Nov 13 '22

This should work. If you don't have Family sharing, you can try pane_index - 2.

use framework "Foundation"
use scripting additions

property pane_ids : {|AppleID|:2, |Family|:3, |Wi-Fi|:5, |Bluetooth|:6, |Network|:7, |Notifications|:9, |Sound|:10, |Focus|:11, |Screen Time|:12, |General|:14, |Appearance|:15, |Accessibility|:16, |Control Center|:17, |Siri & Spotlight|:18, |Privacy & Security|:19, |Desktop & Dock|:21, |Displays|:22, |Wallpaper|:23, |Screen Saver|:24, |Battery|:25, |Lock Screen|:27, |Touch ID & Password|:28, |Users & Groups|:29, |Passwords|:31, |Internet Accounts|:32, |Game Center|:33, |Wallet & ApplePay|:34, |Keyboard|:36, |Trackpad|:37, |Printers & Scanners|:38, |Java|:40}

on open_settings_to(settings_pane)
	set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
	set pane_index to (pane's valueForKey:settings_pane) as any
	tell application "System Settings"
		activate
		delay 1
	end tell
	tell application "System Events"
		tell application process "System Settings"
			tell splitter group 1 of group 1 of window 1
				tell outline 1 of scroll area 1 of group 1
					if name of static text of UI element 5 = "Wi‑Fi" then
						select row pane_index
					else
						set pane_index to pane_index + 2
						select row pane_index
					end if
				end tell
			end tell
		end tell
	end tell
end open_settings_to


on toggle_internet_sharing()
	tell application "System Events"
		tell application process "System Settings"
			tell window 1
				tell group 2 of splitter group 1 of group 1
					repeat until button 1 of group 3 of scroll area 1 of group 1 exists
						delay 0
					end repeat
					tell button 1 of group 3 of scroll area 1 of group 1
						click
					end tell
					repeat until checkbox 7 of group 1 of scroll area 1 of group 1 of group 1 exists
						delay 0
					end repeat
					tell checkbox 7 of group 1 of scroll area 1 of group 1 of group 1
						click
					end tell
				end tell
				set endDate to (current date) + 1
				set toggled_on to true
				repeat until sheet 1 exists
					delay 0.2
					if (current date) > endDate then
						set toggled_on to false
						exit repeat
					end if
				end repeat
				if toggled_on is true then
					click button 1 of sheet 1
				end if
			end tell
		end tell
	end tell
end toggle_internet_sharing

on run {}
	open_settings_to("General")
	toggle_internet_sharing()
	quit application "System Settings"
end run

1

u/imahermit Nov 13 '22

I'm not much of a scriptor so forgive me for my noob understanding of the language but for some reason this code below:

set pane_index to (pane's valueForKey:settings_pane) as any

was giving me this error "Expected class name but found identifier.". so i removed it.

Also

"select row pane_index" 

was giving the error "The variable pane_index is not defined." so instead I added keystrokes to type in "general" to reveal the general page which works great now. Thank you for your suggesting!! Here's the updated code if anyone wants it:

use framework "Foundation"
use scripting additions

use framework "Foundation"
use scripting additions

property pane_ids : {|AppleID|:2, |Family|:3, |Wi-Fi|:5, |Bluetooth|:6, |Network|:7, |Notifications|:9, |Sound|:10, |Focus|:11, |Screen Time|:12, |General|:14, |Appearance|:15, |Accessibility|:16, |Control Center|:17, |Siri & Spotlight|:18, |Privacy & Security|:19, |Desktop & Dock|:21, |Displays|:22, |Wallpaper|:23, |Screen Saver|:24, |Battery|:25, |Lock Screen|:27, |Touch ID & Password|:28, |Users & Groups|:29, |Passwords|:31, |Internet Accounts|:32, |Game Center|:33, |Wallet & ApplePay|:34, |Keyboard|:36, |Trackpad|:37, |Printers & Scanners|:38, |Java|:40}

on open_settings_to(settings_pane)
	set pane to current application's NSDictionary's dictionaryWithDictionary:pane_ids
	tell application "System Settings"
		activate
		delay 2
	end tell
	tell application "System Events"
		tell application process "System Settings"
			
			tell application "System Events" to keystroke "f" using command down
			tell application "System Events" to keystroke "general"
			delay 3
			tell application "System Events" to keystroke return
			delay 2
		end tell
	end tell
	
	
end open_settings_to


on toggle_internet_sharing()
	tell application "System Events"
		tell application process "System Settings"
			tell window 1
				tell group 2 of splitter group 1 of group 1
					repeat until button 1 of group 3 of scroll area 1 of group 1 exists
						delay 0
					end repeat
					tell button 1 of group 3 of scroll area 1 of group 1
						click
					end tell
					repeat until checkbox 7 of group 1 of scroll area 1 of group 1 of group 1 exists
						delay 0
					end repeat
					tell checkbox 7 of group 1 of scroll area 1 of group 1 of group 1
						click
					end tell
				end tell
				set endDate to (current date) + 1
				set toggled_on to true
				repeat until sheet 1 exists
					delay 0.2
					if (current date) > endDate then
						set toggled_on to false
						exit repeat
					end if
				end repeat
				if toggled_on is true then
					click button 1 of sheet 1
				end if
			end tell
		end tell
	end tell
end toggle_internet_sharing

on run {}
	open_settings_to("General")
	toggle_internet_sharing()
	quit application "System Settings"
end run