Toggle column's visible of list view options

For ages now toggling the visibility of columns in the list view of Finder windows is not working in an AS script.
This for instance, keeps failing:

tell application "Finder"
	set _win to Finder window 1
	tell _win
		tell list view options of _win
			tell column size column of list view options of _win
				set properties to {index:4, name:size column, visible:false}
			end tell
		end tell
	end tell
end tell

I have not found any explanation as to why Apple doesn’t seem to have any intention to fix this. Any idea anybody?
Way back in 10.6 I have found this GUI script that worked trough 10.7 and maybe 10.8:

tell application "Finder"
		activate
		tell fwin
			select -- # added to bring fwin to front  
			set current view to list view
		end tell
	end tell
	tell application "System Events"
		tell process "Finder"
			set view_options to a reference to (window 1 whose subrole = "AXSystemFloatingWindow")
			set was_open to exists view_options
			if not was_open then
				tell menu bar 1's menu bar item 5 -- View  
					tell menu 1's menu item -1 -- Show View Options  
						click
					end tell
				end tell
			end if
			tell view_options
				tell group 1
					-- The checkbox was 4 for Label in 10.6.
					-- I'm trying 9 for Tags in 10.11 but the click fails.
					tell checkbox 9
						if value = 0 then click
						click
						(*
						-- error number -1719 from window 1 of process "Finder" ¬
						whose subrole = "AXSystemFloatingWindow" 
						-- Result: error ¬
						"System Events got an error: Can't get window 1 of process \"Finder\" ¬
						whose subrole = \"AXSystemFloatingWindow\". Invalid index." number -1719
						*)
					end tell
				end tell
				if not was_open then click (button 1 whose subrole = "AXCloseButton") -- Close  
			end tell
		end tell
	end tell
end show_label_in_list_view

I’m trying again now in 10.11 to manipulate column visibility in list view, but found nothing that works.
Please, can anybody help with a scripted solution for adding and/or removing columns in list view?
Maybe using ASObjC?

On Yosemite it is checkbox 4 and this code is working:

tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		set view_options to a reference to (window 1 whose subrole = "AXSystemFloatingWindow")
		set was_open to exists view_options
		if not was_open then
			tell menu bar 1's menu bar item 5 -- View 
				tell menu 1's menu item -3 -- Show View Options  
					click
				end tell
			end tell
		end if
		tell view_options
			tell group 1
				tell checkbox 4 -- tags
					click
				end tell
			end tell
			if not was_open then click (button 1 whose subrole = "AXCloseButton") -- Close  
		end tell
	end tell
end tell

Hi DJ,
I’m getting the same error in 10.11 as with the earlier script.
In El Capitan, checkbox 9 = tags. I also tried checkbox 5 = size. Both ended with:


click checkbox 5 of group 1 of window 1 of process "Finder" whose subrole = "AXSystemFloatingWindow"
--> error number -1719 from window 1 of process "Finder" whose subrole = "AXSystemFloatingWindow"
-- Result:
-- error "System Events got an error: Can't get window 1 of process \"Finder\" whose subrole = \"AXSystemFloatingWindow\". Invalid index." number -1719

What does the following command return when you have to view options window open? Is the subrole of the view options window “AXSystemFloatingWindow”?

tell application "Finder" to activate
tell application "System Events"
	tell process "Finder"
		return subrole of every window
	end tell
end tell
tell application "System Events"
	get subrole of every window of process "Finder"
		--> {"AXSystemFloatingWindow", "AXStandardWindow"}
end tell
--Result:
--{"AXSystemFloatingWindow", "AXStandardWindow"}

Hello flex20.

You missed one change carefully introduced by DJ Bazzie Wazzie.

In your code you have :
tell menu bar 1’s menu bar item 5 – View
tell menu 1’s menu item -1 – Show View Options

It was true in 10.6 but it’s wrong since the introduction of Full Screen mode.
Now you must use :
tell menu bar 1’s menu bar item 5 – View
–tell menu 1’s menu item -1 – Activate Full Screen Mode
tell menu 1’s menu item -3 – Show View Options

Here is a script in which I added some instructions useful to understand what we are doing.


tell application "Finder" to activate

tell application "System Events"
	tell process "Finder"
		set frontmost to true # useful with Yosemite, don't hurt in El Capitan
		set view_options to a reference to (window 1 whose subrole = "AXSystemFloatingWindow")
		set was_open to exists view_options
		if not was_open then
			tell menu bar 1 to name of menu bar items
			log result
			tell menu bar 1's menu bar item 5 -- View  
				tell menu 1 to name of menu items
				log result
				--tell menu 1's menu item -1 -- Activate Full Screen Mode
				tell menu 1's menu item -3 -- Show View Options  
					click
				end tell
			end tell
		end if
		tell view_options
			tell group 1
				name of checkboxes
				log result
				-- The checkbox was 4 for Label in 10.6.
				-- It's 4 under 10.11 too
				tell checkbox 4
					click
					(*
					# It was issued because you weren't in Show View Options window
					# But in the Finder's window in Full View mode
						-- error number -1719 from window 1 of process "Finder" ¬
						whose subrole = "AXSystemFloatingWindow" 
						-- Result: error ¬
						"System Events got an error: Can't get window 1 of process \"Finder\" ¬
						whose subrole = \"AXSystemFloatingWindow\". Invalid index." number -1719
						*)
				end tell
			end tell
			if not was_open then click (button 1 whose subrole = "AXCloseButton") -- Close  
		end tell
	end tell
end tell

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) samedi 16 janvier 2016 16:07:18

Hi Yvan,

Thank you for helping.

I haven’t really noticed those changes DJ made, but I run his exact code without any changes other than trying different checkbox numbers. All tries ended with the same error.

I run now your exact code, including the checkbox 4. Again I got the same error.

I have no idea what, but something is wrong, since I’m running the same scripts which work on your macs but not on mine. I have the same os x 10.11.2 as you have (English not French).

By the way, you seem to say that checkbox 4 for Label is the same in 10.11. In my “Show View Options” window there is no Label any longer. There is Tags and that is checkbox 9. That is off cause, if the 9 checkboxes are numbered in the sequence they are drawn. Are they?

Cheers, Chris

Yvan, sorry, forgotten to add the log from your script:

tell application "System Events"
	set frontmost of process "Finder" to true
	exists window 1 of process "Finder" whose subrole = "AXSystemFloatingWindow"
		--> false
	get name of every menu bar item of menu bar 1 of process "Finder"
		--> {"Apple", "Finder", "File", "Edit", "View", "Go", "Window", "Help"}
	(*Apple, Finder, File, Edit, View, Go, Window, Help*)
	get name of every menu item of menu 1 of menu bar item 5 of menu bar 1 of process "Finder"
		--> {"as Icons", "as List", "as Columns", "as Cover Flow", missing value, "Clean Up", "Clean Up Selection", "Clean Up By", "Arrange By", "Sort By", missing value, "Show Tab Bar", "Hide Path Bar", "Hide Status Bar", "Show Sidebar", "Show Preview", missing value, "Hide Toolbar", "Customize Toolbar.", missing value, "Show View Options", missing value, "Enter Full Screen", missing value, "Show Hidden Items", "Refresh", "Collapse All"}
	(*as Icons, as List, as Columns, as Cover Flow, missing value, Clean Up, Clean Up Selection, Clean Up By, Arrange By, Sort By, missing value, Show Tab Bar, Hide Path Bar, Hide Status Bar, Show Sidebar, Show Preview, missing value, Hide Toolbar, Customize Toolbar., missing value, Show View Options, missing value, Enter Full Screen, missing value, Show Hidden Items, Refresh, Collapse All*)
	click menu item -3 of menu 1 of menu bar item 5 of menu bar 1 of process "Finder"
		--> menu item "Show Hidden Items" of menu "View" of menu bar item "View" of menu bar 1 of application process "Finder"
	get name of every checkbox of group 1 of window 1 of process "Finder" whose subrole = "AXSystemFloatingWindow"
		--> error number -1719 from window 1 of process "Finder" whose subrole = "AXSystemFloatingWindow"
Result:
error "System Events got an error: Can't get window 1 of process \"Finder\" whose subrole = \"AXSystemFloatingWindow\". Invalid index." number -1719

OK.

May you run

tell application "Finder"
	activate
	set FinderVersion to version
end tell
log FinderVersion

and post what you get ?

I’m wondering if you are really running El Capitan and if you do, if it’s version 10.11.2

Compare our results :

	tell menu bar 1's menu bar item 5 -- View  
				tell menu 1 to name of menu items
				log result
				(*
Under 10.11.2 I get :
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, Afficher la barre des onglets, Afficher la barre du chemin d'accès, Masquer la barre d'état, Masquer la barre latérale, Afficher l'aperçu, missing value, Masquer la barre d'outils, Personnaliser la barre d'outils., missing value, 
-3 - Afficher les options de présentation, Show View Options in English
-2 - missing value, 
-1 - Activer le mode plein écran*)
				(*
On your system which is ? you got
as Icons, as List, as Columns, as Cover Flow, missing value, Clean Up, Clean Up Selection, Clean Up By, Arrange By, Sort By, missing value, Show Tab Bar, Hide Path Bar, Hide Status Bar, Show Sidebar, Show Preview, missing value, Hide Toolbar, Customize Toolbar., missing value, 
-7 - Show View Options, 
-6 - missing value, 
-5 - Enter Full Screen, 
-4 - missing value, 
-3 - Show Hidden Items, 
-2 - Refresh, 
-1 - Collapse All*)

I don’t remember seeing your four last items : -4 - missing value,
-3 - Show Hidden Items,
-2 - Refresh,
-1 - Collapse All*)
Are you using a third party tool adding these menu items to the Finder ?

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) samedi 16 janvier 2016 18:31:12

here it is:

tell application "Finder"
	activate
end tell
(*10.11.2*)

Regarding those 4 strange additions, yes I was testing an app, which I removed now and rebooted.
Unfortunately, the GUI scripts still fail with the same error.

I’m quite sure that you use a third party tool adding menu items.

Look at the French list which I get completed by the English list which you get.

01- Par icônes, as Icons,
02 - Par liste, as List,
03 - Par colonnes, as Columns,
04 - Sous forme de Cover Flow, as Cover Flow,
05 - missing value, missing value,
06 - Aligner, Clean Up,
07 - Aligner la sélection, Clean Up Selection,
08 - Aligner par, Clean Up By,
09 - Organiser par, Arrange By,
10 - Trier par, Sort By,
11 - missing value, missing value,
12 - Afficher la barre des onglets, Show Tab Bar,
13 - Afficher la barre du chemin d’accès, Hide Path Bar,
14 - Masquer la barre d’état, Hide Status Bar,
15 - Masquer la barre latérale, Show Sidebar,
16 - Afficher l’aperçu, Show Preview,
17 - missing value, missing value,
18 - Masquer la barre d’outils, Hide Toolbar,
19 - Personnaliser la barre d’outils., Customize Toolbar.,
20 - missing value, missing value,
21 - Afficher les options de présentation, Show View Options,
22 - missing value, missing value,
23 - Activer le mode plein écran, Enter Full Screen,
24 - ???, missing value,
25 - ???, Show Hidden Items,
26 - ???, Refresh,
27 - ???, Collapse All

The items 1 to 23 are the same here and on your machine.
Items 24 thru 27 exist only for you.

Triggering the menu item backwards was a bad idea.

replace

tell menu 1’s menu item -3
by
tell menu 1’s menu item 21

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) samedi 16 janvier 2016 18:49:36

That’s correct. As I said above, I was testing two app for a special Finder feature I’m after. One of those apps, bad timing and Murphy’s Law combined to mess up the Finder, right at the time of my posting. Looking at the “glass half full” it did reveal an unacceptable behaviour of that app. Everything is restored now.

Many thanks Yvan.
Great remote debugging. That did it. The GUI scripts work perfectly now.
For anybody interested, this is the list of the 12 checkboxes (listed from 1 to 12) in group 1 of the “Show View Options”:

{"Calculate all sizes", "Use relative dates", "Version", "Tags", "Date Modified", "Size", "Kind", "Comments", "Date Created", "Show icon preview", "Date Last Opened", "Date Added"}

As an added benefit, I learned from a master GUI scripter, how to start digging in GUI objects to get their contents.
Thanks again,
Chris

Thanks for the feedback.

In French the names of the checkboxes are :
{“Calculer toutes les tailles”, “Utiliser les dates relatives”, “Version”, “Tags”, “Date de modification”, “Taille”, “Type”, “Commentaires”, “Date de création”, “Utiliser un aperçu comme icône”, “Date de dernière ouverture”, “Date de l’ajout”}

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) samedi 16 janvier 2016 21:27:36

For anybody interested, this is my take for a scripted handling of column visibility in list view:

-- Toggle Column Visibility in List View with choice of column and target.
-- Batch processing can be added easily.
-- Based on scripts and comments by DJ Bazzie Wazzie at #2 and Yvan Koenig at #6, #9, #11 above.
-- http://macscripter.net/viewtopic.php?pid=184423#p184423

tell application "Finder"
	activate
	display dialog "Toggle any column in the list view of" buttons ¬
		{"Cancel", "Open Window", "Choose Folder"}
	set ddb to button returned of result
	if ddb is "Open Window" then
		set myWindow to window 1
	else -- if ddb is "Choose Folder" then
		set myFolder to choose folder
		open myFolder
		set myFolder to window 1
	end if
	my ToggleColumnVisibilityInListView()
end tell

on ToggleColumnVisibilityInListView()
	tell application "Finder" to activate
	tell application "System Events"
		tell process "Finder"
			set view_options to a reference to ¬
				(window 1 whose subrole = "AXSystemFloatingWindow")
			set was_open to exists view_options
			if not was_open then
				tell menu bar 1's menu bar item 5 -- View
					tell menu 1 to set menu_items_List to name of menu items
					-- Get the rank of the "Show View Options" without hard-coding it.
					set Show_View_Options_Num to 0
					repeat with i in menu_items_List
						set Show_View_Options_Num to Show_View_Options_Num + 1
						if i as text is "Show View Options" then exit repeat
					end repeat
					tell menu 1's menu item Show_View_Options_Num to click
				end tell
			end if
			tell view_options
				tell group 1
					-- Get the list of checkboxes in group 1.
					set checkbox_List to {"Calculate all sizes", "Use relative dates", "Version", "Tags", "Date Modified", "Size", "Kind", "Comments", "Date Created", "Show icon preview", "Date Last Opened", "Date Added"}
					-- Set the column menu as a subset list of the checkboxes list.
					set column_List to {"Version", "Tags", "Date Modified", "Size", "Kind", "Comments", "Date Created", "Date Last Opened", "Date Added"}
					tell application "Finder" to choose from list column_List
					set thechoice to result
					if thechoice is false then
						-- Close "Show View Options".
						tell view_options to click (button 1 whose subrole = "AXCloseButton")
						return
					end if
					set column_Name to thechoice as text
					-- Get the selected column's rank in the checkbox list without hard-coding.
					set checkbox_column_Num to 0
					repeat with i in checkbox_List
						set checkbox_column_Num to checkbox_column_Num + 1
						if i as text is column_Name then exit repeat
					end repeat
					tell checkbox checkbox_column_Num to click
				end tell
				click (button 1 whose subrole = "AXCloseButton") -- Close "Show View Options".
			end tell
		end tell
	end tell
end ToggleColumnVisibilityInListView

Cheers

Added closing the “Show View Option” floating window when choosing a column is cancelled.