Select Finder menu bar item and ensure it's toggled on

I have a script to open a folder and set the bounds, view etc. but I’m struggling with one part of it.

I want it to always make sure the ‘Use Groups’ toggle is enabled/selected (Finder > View > Use Groups), but my script doesn’t know whether it’s already set to on or off so just ‘blindly toggles it’.

tell application "Finder"
	if computer name of (system info) is "iMac 5K" then
		set theBounds to ({670, 100, 1890, 1268} as rectangle)
	end if
	if computer name of (system info) is "MacBook Pro" then
		set theBounds to ({280, 23, 1400, 995} as rectangle)
	end if
	
	set NewWindow to make new Finder window
	set target of NewWindow to (path to desktop folder as text)
	
	tell Finder window 1
		set the bounds to theBounds
		set its sidebar width to 200
		set its toolbar visible to true
		set current view to list view
		
		tell its list view options
			set the sort column to modification date column
			set sort direction of column id modification date column to normal
			set icon size to small icon
		end tell
	end tell
	activate
end tell


(* Help! *)
tell application "System Events"
	click menu item ¬
		"Use Groups" in menu "View" of menu bar item ¬
		"View" in menu bar 1 of process "Finder"
end tell

Is there a way to make sure it’s always enabled, or look at whether it’s enabled or not and act accordingly?

You have to check if the menu item exists and if the attribute AXMenuItemMarkChar is the checkmark character


tell application "System Events"
	tell menu "View" of menu bar item "View" in menu bar 1 of process "Finder"
		if exists menu item "Use Groups" then
			tell menu item "Use Groups"
				if value of attribute "AXMenuItemMarkChar" is not "✓" then
					click it
				end if
			end tell
		end if
	end tell
end tell

On my Finder in French I see no item resembling to “Use Groups”.
Is it an item introduced by Mojave ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 28 février 2019 15:29:15

Perfect, thanks Stefan!

No, it was there before Mojave (although might have been renamed in Mojave).

@Yvan, yes, Use Groups appears only in Mojave

Thank you Stefan.
It seems that the screenshot posted by jono was done under Mojave.
Here, under High Sierra the menu items are:

{“Par icônes”, “Par liste”, “Par colonnes”, “Sous forme de Cover Flow”, missing value, “Aligner”, “Aligner la sélection”, “Aligner par”, “Organiser par”, “Trier par”, missing value, “Masquer la barre d’onglets”, “Masquer la barre du chemin d’accès”, “Masquer la barre d’état”, “Masquer la barre latérale”, “Masquer l’aperçu”, missing value, “Barre d’outils”, “Personnaliser la barre d’outils…”, missing value, “Afficher les options de présentation”, missing value, “Activer le mode plein écran”}

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 28 février 2019 16:20:41