Problem

I apologize but I tried your proposal with :

set theRoot to (path to library folder from system domain as text) & "PreferencePanes:"
set theBundle to theRoot & "Network.prefPane:"
set network_loc to localized string "CFBundleName" from table "InfoPlist" in bundle file theBundle --> "Réseau"
set sytemPrefs_loc to localized string "System Preferences" in bundle (path to application "System Preferences")

tell application "System Preferences"
	activate
	tell application "System Events"
		activate
		tell process "System Preferences"
			set frontmost to true
			keystroke "l" using {command down} --> shortcut to Show all Prefs
			repeat until exists window sytemPrefs_loc
				delay 0.1
			end repeat
			click button network_loc of scroll area 1 of window sytemPrefs_loc --- Select NETWORK Prefs
			repeat until exists window network_loc
				delay 0.1
			end repeat
			tell application "System Events"
				set MyServices to every service of current location of network preferences
				repeat with this_item in MyServices
					if name of (contents of this_item) contains "Wi-Fi" then
						connect (contents of this_item) -- or "connect" if WiFi is off
						exit repeat
					end if
				end repeat
			end tell
			delay 1
			(*click menu item "Show All Preferences" of menu 1 of menu bar item 4 of menu bar 1 --- Show all Prefs
			click menu item "Energy Saver" of menu 1 of menu bar item 4 of menu bar 1 --- Select ENERGY SAVER Prefs
			click button "Restore Defaults" of group 1 of window "Energy Saver" --- Select default settings
			---click checkbox "Put hard disks to sleep when possible" of group 1 of window "Energy Saver" --- Turn ON & OFF checkbox.
			*)
		end tell
	end tell
end tell

The events log was:

 # DON'T CLICK UPON Open this Scriplet in your Editor
tell current application
	path to library folder from system domain as text
	localized string "CFBundleName" from table "InfoPlist" in bundle file "SSD 1000:System:Library:PreferencePanes:Network.prefPane:"
end tell
tell application "System Preferences"
	path to current application
end tell
tell current application
	localized string "System Preferences" in bundle alias "SSD 1000:Applications:System Preferences.app:"
end tell
tell application "System Preferences"
	activate
end tell
tell application "System Events"
	activate
	set frontmost of process "System Preferences" to true
	keystroke "l" using {command down}
	exists window "Préférences Système" of process "System Preferences"
	exists window "Préférences Système" of process "System Preferences"
	exists window "Préférences Système" of process "System Preferences"
	exists window "Préférences Système" of process "System Preferences"
	click button "Réseau" of scroll area 1 of window "Préférences Système" of process "System Preferences"
	exists window "Réseau" of process "System Preferences"
	exists window "Réseau" of process "System Preferences"
	exists window "Réseau" of process "System Preferences"
	exists window "Réseau" of process "System Preferences"
	get every service of current location of network preferences
	get name of service id "2FDAA8EF-BE89-4E48-A990-DD72CE26B021" of network preferences
	get name of service id "3F0B72B6-255A-4576-ABB3-E1E95CD77FF8" of network preferences
	connect service id "3F0B72B6-255A-4576-ABB3-E1E95CD77FF8" of network preferences
end tell

and alas,Wi-Fi remains disconnected exactly as it was.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 décembre 2019 15:42:30

At last I found the resources containing the strings which weren’t localized in message #17.
I edited the message accordingly.

To find these resources I used a script finalized with the help of Shane Stanley.
Here it is :

-- Dialog Toolkit Plus.scptd should be in ~/Library/Script Libraries
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use script "Dialog Toolkit Plus" version "1.1.0"

property |NSURL| : a reference to current application's NSURL

script o
	property theLanguages : missing value
	property exactMatch : missing value
end script

on searchIn:targetDir forValue:aValue
	set reportName to "matches_sehctam.txt"
	set targetFile to (path to desktop as text) & reportName
	
	my writeto(targetFile, "theValue" & tab & "theKey" & tab & "HfsPath" & linefeed, «class utf8», false) # create a report
	
	set theFolderURL to current application's NSURL's fileURLWithPath:(POSIX path of targetDir)
	set NSFileManager to a reference to current application's NSFileManager's defaultManager()
	set theOptions to (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
	set theURLs to (NSFileManager's enumeratorAtURL:theFolderURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value))'s allObjects()
	if theURLs is equal to missing value then error (theError's localizedDescription() as text)
	
	repeat with aURL in theURLs
		set aName to (aURL's lastPathComponent()) as text
		if aName is in o's theLanguages then
			# Here we have the searched language lproj name
			set someURLs to (NSFileManager's enumeratorAtURL:aURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value))'s allObjects()
			if someURLs is not equal to missing value then
				set englishResAvailable to false
				--set englishURL to missing value
				repeat with anURL in someURLs
					if anURL's pathExtension() as text is "strings" then
						set theDict to (current application's NSDictionary's dictionaryWithContentsOfURL:anURL)
						if theDict is not missing value then
							set theKeys to theDict's allKeys()
							repeat with akey in theKeys
								try
									set theValue to (theDict's valueForKey:akey) # NSString
									if o's exactMatch then
										set matching to (theValue as text = aValue)
									else
										set matching to (theValue as text) contains aValue
									end if
									if matching then
										if not englishResAvailable then
											# Build the path of the English proj
											set frenchPath to anURL as text
											set englishPath to (my replace:frenchPath origString:aName newString:"en.lproj")
											set englishURL to (|NSURL|'s fileURLWithPath:(POSIX path of englishPath))
											if (englishURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then
												set englishResAvailable to true
											else
												set englishPath to (my replace:frenchPath origString:aName newString:"English.lproj")
												set englishURL to (|NSURL|'s fileURLWithPath:(POSIX path of englishPath))
												if (englishURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then
													set englishResAvailable to true
												end if
											end if
										end if
										if englishResAvailable then
											set englishDict to (current application's NSDictionary's dictionaryWithContentsOfURL:englishURL)
											set englishValue to (englishDict's valueForKey:(akey as text))
											if englishValue is missing value then
												set englishValue to "ENGLISH KEY UNAVAILABLE"
											else
												set englishValue to englishValue as text
											end if
										else
											set englishValue to "ENGLISH lproj UNAVAILABLE"
										end if
										if aName is in {"en.lproj", "English.lproj"} then
											set theData to (theValue as text) & tab & akey & tab & anURL & linefeed
										else
											set theData to (theValue as text) & tab & akey & tab & englishValue & tab & anURL & linefeed
										end if
										my writeto(targetFile, theData, «class utf8», true) # create a report
									end if
									(*
								on error
									# we met a key like "@1 isn't scriptable."
									set theData to ">>>>>>>>>>>>>>" & tab & akey & tab & anURL & linefeed
									my writeto(targetFile, theData, «class utf8», true) # create a report
									*)
								end try
							end repeat
						end if
					end if
				end repeat
			end if
		end if
	end repeat
	tell application "TextEdit"
		activate
		if exists window reportName then close window reportName
		open file targetFile
	end tell
end searchIn:forValue:

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeto

#=====

on replace:sourceString origString:d1 newString:d2
	set sourceString to current application's NSString's stringWithString:sourceString
	set anArray to (sourceString's componentsSeparatedByString:d1)
	set newString to (anArray's componentsJoinedByString:d2) as text
	return newString
end replace:origString:newString:

#=====

my germaine()
on germaine()
	set accViewWidth to 400
	-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	set btnOther to "French" # Edit to fit your needs
	set otherLanguages to {"fr.lproj", "French.lproj"} # Edit to fit your needs
	-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	set btnEnglish to "English"
	set btnCancel to "Cancel"
	set {theButtons, minWidth} to create buttons {btnCancel, btnEnglish, btnOther} default button btnOther cancel button btnCancel with equal widths
	
	if minWidth > accViewWidth then set accViewWidth to minWidth -- make sure buttons fit
	-- to make it look better, we can get the length of the longest label we will use, and use that to align the controls
	set theLabelStrings to {"Search in:", "Text to search:"}
	set maxLabelWidth to max width for labels theLabelStrings
	set controlLeft to maxLabelWidth + 8
	set theTop to -8
	-- start from the bottom with a checkbox
	# box checked --> return strings equal to the searched string -- default setting
	# box unchecked --> return strings containing the searched string
	set {theCheckbox, theTop, newWidth} to create checkbox "Exact match" left inset controlLeft bottom (theTop + 8) max width accViewWidth with initial state
	
	-- then a labeled popup to define the location where we will search
	set inSystemFolder to "Library from System folder"
	set inApplications to "Applications folder"
	set inPrefPanes to "PreferencePanes folder"
	set inResources to "Folder Resources on the Desktop"
	
	set {locationPopup, popupLabel, theTop} to create labeled popup {inSystemFolder, inApplications, inPrefPanes, inResources} left inset 0 bottom (theTop + 8) popup width 250 max width accViewWidth label text (theLabelStrings's item 1) popup left controlLeft initial choice inResources
	# Define the string to search
	set {searchField, searchLabel, theTop, fieldLeft} to create side labeled field "Réseau" placeholder text "Searched text" left inset 0 bottom (theTop + 12) total width accViewWidth label text (theLabelStrings's item 2) field left controlLeft
	
	-- make list of controls and pass to display command
	set allControls to {theCheckbox, locationPopup, popupLabel, searchField, searchLabel}
	-- controlResults will be in the same order as allControls
	set {buttonName, controlsResults} to display enhanced window "Search in resources" acc view width accViewWidth acc view height theTop acc view controls allControls buttons theButtons initial position {100, 30} with align cancel button
	
	set {matchBoolean, searchWhere, unused, aValue, unused} to controlsResults
	
	if buttonName is btnEnglish then # search in English resources
		set o's theLanguages to {"en.lproj", "English.lproj"}
	else # search in other language resources
		set o's theLanguages to otherLanguages
	end if
	
	set o's exactMatch to matchBoolean
	
	if searchWhere is inSystemFolder then
		set targetDir to path to library folder from system domain
	else if searchWhere is inApplications then
		set targetDir to path to applications folder from local domain
	else if searchWhere is inPrefPanes then
		set targetDir to (path to library folder from system domain as text) & "PreferencePanes:"
	else
		set targetDir to (path to desktop as text) & "Resources:"
	end if
	
	my searchIn:targetDir forValue:aValue
end germaine

As you know I am unable to test it under 10.14 or 10.15.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 décembre 2019 18:23:50

Edited to give it a neat interface

New enhancements. When the searched language is not English, the script return infos about the availability of the English version of the searched string.

Wow, Yvan, your script in post #17 works perfect on my Mojave. With turning WiFi both On and Off.

I will delete my wrong posts where I say “can’t select WiFi row from table”. I updated my script at post #16, and it works now fine, like your localized script in post #17.

NOTE: You can make file coercion of bundle only once, and then use in bundle theBundle:

set theBundle to (POSIX path of theRoot & "EnergySaver.prefPane") as POSIX file

In reply to KniazidisR

I’m not sure I understand: are you saying it’s not possible to automate GUI of toggling the Wi-Fi switcher? Or is it not possible when it’s on only? Or are you saying locating and summoning the Network window fails in vanilla AppleScript?

Does the following code work for you?

tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			tell the first item of (every menu bar item whose description contains "Wi-Fi")
				perform action "AXPress"
				repeat until menu 1 exists
					delay 0.1
				end repeat
				tell menu 1
					try
						set TargetMenuItem to the first item of (every menu item whose title contains "Wi-Fi")
						if title of TargetMenuItem contains "Off" then
							tell TargetMenuItem to perform action "AXPress"
						else
							tell TargetMenuItem to perform action "AXPress"
						end if
					on error errMessage number errNumber
						display alert "The was an error " & errNumber message errMessage as warning giving up after 3
						return
					end try
				end tell
			end tell
		end tell
	end tell
end tell

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 9537.86.7
Operating System: macOS 10.9

I don’t understand your point.

I wrote:

set theRoot to (path to library folder from system domain as text) & “PreferencePanes:”
set theBundle to theRoot & “Network.prefPane:”

After that, theBundle contains a string value so I must code
set network_loc to localized string “CFBundleName” from table “InfoPlist” in bundle file theBundle → “Réseau”

Below, (path to application “System Preferences”) returns an alias so there is no need for the file statement.
set sytemPrefs_loc to localized string “System Preferences” in bundle (path to application “System Preferences”)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 9 décembre 2019 20:45:38

You can toggle WiFi also with networksetup in the shell however it needs admin rights.


property pass : "•••••••••"

on toggleWiFi()
	try
		tell application "System Events" to set device to id of first interface of network preferences whose kind is "IEEE80211"
		set isOn to (last word of (do shell script "/usr/sbin/networksetup -getairportpower " & device) is "on") as integer
		do shell script "networksetup -setairportpower " & device & space & item (isOn + 1) of {"on", "off"} password pass with administrator privileges
	end try
end toggleWiFi

As the questions is 4, my answers is 4 too: yes, yes, yes, no.

So, you wrote my simplest vanilla AppleScript for toggling the WiFi (which I deleted) in other form. And now it doesn’t work on my Mojave system. So, I will repeat the working variant:

tell application "System Events" to tell process "SystemUIServer"
	tell menu bar 1 to tell (first menu bar item whose description contains "Wi-Fi")
		click
		repeat until menu 1 exists
			delay 0.1
		end repeat
		try
			tell menu 1 to click second menu item
		on error errMessage number errNumber
			display alert "Error " & errNumber message errMessage as warning giving up after 3
			return
		end try
	end tell
end tell

This is the simplest vanilla Apple-script to toggle WiFi (that is, without entering the password). But I still don’t see one solution better than Shane’s AsObjC solution.

I’m confused. Your re-written code is basically the same as mine. I haven’t tested it on High Sierra however it shouldn’t work differently and it works 100% in Mavericks.
As for advantages, there’s one – it’s much simpler and easy both to write and read.

Update

Now that I’m in High Sierra I see that it clicks on a wrong UI element since System Events lookup mechanism considers the status indicator description also containing “Wi-Fi”. However, even in this case, the fix is rather simple and possible by prefacing “Wi-Fi” with “Turn” as in this corrected line:

This can be an issue with a different localization but I think it’s a minor ineluctable distraction.

It’s really easy to get rid of localized settings

# Grab localized strings
set theRoot to (path to library folder from system domain as text) & "PreferencePanes:"
set theBundle to theRoot & "Network.prefPane:"
--set network_loc to localized string "CFBundleName" from table "InfoPlist" in bundle file theBundle --> "Réseau"
set TURN_AIRPORT_ON_loc to localized string "TURN_AIRPORT_ON" in bundle file theBundle --> "Activer le Wi-Fi"
set TURN_AIRPORT_OFF_loc to localized string "TURN_AIRPORT_OFF" in bundle file theBundle --> "Activer le Wi-Fi"

tell application "System Events"
	tell process "SystemUIServer"
		tell menu bar 1
			set WiFimenu to first item of (every menu bar item whose description contains "Wi-Fi")
			tell WiFimenu
				perform action "AXPress"
				repeat until menu 1 exists
					delay 0.1
				end repeat
				tell menu 1
					try
						if exists menu item TURN_AIRPORT_ON_loc then
							tell menu item TURN_AIRPORT_ON_loc to perform action "AXPress"
						else
							tell menu item TURN_AIRPORT_OFF_loc to perform action "AXPress"
						end if
					on error errMessage number errNumber
						display alert "There was an error " & errNumber message errMessage as warning giving up after 3
						return
					end try
					
				end tell
			end tell
		end tell
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 10 décembre 2019 14:03:10

I edited the script posted in message #22 to give it a neat interface.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 10 décembre 2019 14:33:50