Finder sorting by kind

Hello all!

I’m working on a Desktop setup script.
I have several issues:
Finder gets an error: Can’t set sort column to kind, even though I have visible set to true.
Also, does anyone know if you can sort by group -as in group by kind? Any help on this would be much appreciated.


set chooseWindowLeft to choose folder
set chooseWindowRight to choose folder

--in order to take advantage of whole screen, check to see if dock is hidden, if not hide dock
tell application "System Events"
	get autohide of the dock preferences
	if result is false then
		set autohide of dock preferences to true
	end if
end tell
--with dock hidden, and folders chosen, two Finder windows will open up and be positioned vertically on the left and right side of the desktop 
tell application "Finder"
	(*using container windows maybe a better way to execute this script:*)
	--chooseWindowLeft
	tell the front Finder window
		open chooseWindowLeft
		set the properties to {bounds:{0, 25, 640, 802}, current view:list view, toolbar visible:true, statusbar visible:true, pathbar visible:true, sidebar width:190}
	end tell
	tell list view options of the front Finder window
		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:modification date column}
		
		tell column name column
			set properties to ¬
				{index:1, sort direction:normal, width:120, visible:true}
		end tell
		tell column size column
			set properties to ¬
				{index:5, sort direction:normal, width:72, visible:true}
		end tell
		tell column modification date column
			set properties to ¬
				{index:2, sort direction:normal, width:120, visible:true}
		end tell
		tell column creation date column
			set properties to ¬
				{index:4, sort direction:normal, width:120, visible:true}
		end tell
		tell column kind column
			set properties to ¬
				{index:3, sort direction:normal, width:120, visible:true}
		end tell
		tell column label column
			set properties to ¬
				{index:6, sort direction:normal, width:72, visible:false}
		end tell
		tell column version column
			set properties to ¬
				{index:7, sort direction:normal, width:72, visible:false}
		end tell
		tell column comment column
			set properties to ¬
				{index:8, sort direction:normal, width:120, visible:false}
		end tell
	end tell
	set sort column to kind
	set current view of the front Finder window to list view
	set the current view of the window of every folder of chooseWindowLeft to list view
	--chooseWindowRight
	tell the front Finder window
		open chooseWindowRight
		set the properties to {bounds:{639, 23, 1279, 800}, current view:list view, toolbar visible:false, statusbar visible:true, pathbar visible:true, sidebar width:0}
	end tell
	tell list view options of the front Finder window
		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:modification date column}
		tell column name column
			set properties to ¬
				{index:1, sort direction:normal, width:120, visible:true}
		end tell
		tell column size column
			set properties to ¬
				{index:5, sort direction:normal, width:72, visible:true}
		end tell
		tell column modification date column
			set properties to ¬
				{index:2, sort direction:normal, width:120, visible:true}
		end tell
		tell column creation date column
			set properties to ¬
				{index:4, sort direction:normal, width:120, visible:true}
		end tell
		tell column kind column
			set properties to ¬
				{index:3, sort direction:normal, width:120, visible:true}
		end tell
		tell column label column
			set properties to ¬
				{index:6, sort direction:normal, width:72, visible:false}
		end tell
		tell column version column
			set properties to ¬
				{index:7, sort direction:normal, width:72, visible:false}
		end tell
		tell column comment column
			set properties to ¬
				{index:8, sort direction:normal, width:120, visible:false}
		end tell
		set sort column to kind
	end tell
	set current view of the front Finder window to list view
	set the current view of the window of every folder of chooseWindowRight to list view
end tell

I appreciate it in advance,

kind regards,
Kell

I didn’t review the entire script and instead looked at the sort-by-kind issue. It appears that the error results from the lines beginning with “set sort column.” Just to illustrate code that works:

tell application "Finder" to tell the front Finder window
	
	tell its list view options
		set sort column to column kind column
		-- set sort column to kind column -- this also appears to work
	end tell
	
	-- these three lines are to refresh Finder window
	set currentFolder to target
	set target to (path to pictures folder) -- this can be any folder other than current target
	set target to currentFolder
	
end tell

Hi peavine!
Thank you for your response. I still have the same issue. So basically, my script lets you choose two Finder folders and then places them vertically on the desktop. The code works for only certain folders. Let’s say I choose Desktop and iCloud folders it will work fine, but if I choose my home folder and another folder it throws up an error saying kind column must be visible. I guess, I could just NOT ever choose home folder but it would be nice if it worked in its entirety. I would really like to sort group by kind as well - with the Finder window not only organizes them by kind but actually segments/ divides them off by their kind. But I see nothing in Finder dictionary allowing me do this. Again, I could manually do this, clicking sort group by kind via toolbar, but would be nice to have this automatically set with the script. I appreciate your help!

Kind regards.
Kell

You’re right, the standard AppleScript support doesn’t allow you to do several tasks.
My understanding is that you must use GUI scripting which would allow you to mimic what you achieve by hand.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 2 juillet 2020 20:00:16

Kell. As far as I know, setting the Finder to arrange items by group has to be done with GUI scripting, as in the following:

tell application "System Events"
	tell process "Finder"
		set frontmost to true
		set itemChecked to (value of attribute "AXMenuItemMarkChar" of menu item "Use Groups" of menu "View" of menu bar 1)
		if itemChecked = missing value then click menu item "Use Groups" of menu "View" of menu bar 1
		delay 1
		click menu item "Kind" of menu of menu item "Group By" of menu "View" of menu bar 1
	end tell
end tell

Just as a point of information, the visible property will not make a column visible or hidden in a Finder window.

Kell. I spent some time with your script and made several necessary changes. The GUI scripting has to be added if you want that, but this script is functionally equivalent to your script in post 1. I tested the script under Catalina without issue.

set chooseWindowLeft to choose folder
set chooseWindowRight to choose folder

tell application "System Events"
	if (autohide of the dock preferences) is false then
		set autohide of dock preferences to true
	end if
end tell

tell application "Finder"
	
	open chooseWindowLeft
	
	tell front Finder window
		set the properties to {bounds:{0, 25, 640, 802}, current view:list view, toolbar visible:true, statusbar visible:true, pathbar visible:true, sidebar width:190}
	end tell
	
	tell list view options of front Finder window
		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:kind column}
		tell column name column
			set properties to ¬
				{index:1, sort direction:normal, width:120, visible:true}
		end tell
		tell column size column
			set properties to ¬
				{index:5, sort direction:normal, width:72, visible:true}
		end tell
		tell column modification date column
			set properties to ¬
				{index:2, sort direction:normal, width:120, visible:true}
		end tell
		tell column creation date column
			set properties to ¬
				{index:4, sort direction:normal, width:120, visible:true}
		end tell
		tell column kind column
			set properties to ¬
				{index:3, sort direction:normal, width:120, visible:true}
		end tell
		tell column label column
			set properties to ¬
				{index:6, sort direction:normal, width:72, visible:false}
		end tell
		tell column version column
			set properties to ¬
				{index:7, sort direction:normal, width:72, visible:false}
		end tell
		tell column comment column
			set properties to ¬
				{index:8, sort direction:normal, width:120, visible:false}
		end tell
	end tell
	
	tell front Finder window -- refresh Finder window
		set currentFolder to target
		set target to (path to pictures folder) -- this can be any folder other than current target
		set target to currentFolder
	end tell
	
	-- useGroups() of me -- GUI script to enable group view
	
	delay 1 -- test to see if delay needed
	
	open chooseWindowRight
	
	tell front Finder window
		set properties to {bounds:{639, 23, 1279, 800}, current view:list view, toolbar visible:false, statusbar visible:true, pathbar visible:true, sidebar width:0}
	end tell
	
	tell list view options of front Finder window
		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:kind column}
		tell column name column
			set properties to ¬
				{index:1, sort direction:normal, width:120, visible:true}
		end tell
		tell column size column
			set properties to ¬
				{index:5, sort direction:normal, width:72, visible:true}
		end tell
		tell column modification date column
			set properties to ¬
				{index:2, sort direction:normal, width:120, visible:true}
		end tell
		tell column creation date column
			set properties to ¬
				{index:4, sort direction:normal, width:120, visible:true}
		end tell
		tell column kind column
			set properties to ¬
				{index:3, sort direction:normal, width:120, visible:true}
		end tell
		tell column label column
			set properties to ¬
				{index:6, sort direction:normal, width:72, visible:false}
		end tell
		tell column version column
			set properties to ¬
				{index:7, sort direction:normal, width:72, visible:false}
		end tell
		tell column comment column
			set properties to ¬
				{index:8, sort direction:normal, width:120, visible:false}
		end tell
	end tell
	
	tell front Finder window -- reset list view and refresh Finder window
		try
			set the current view of the window of every folder to list view
		end try
		set currentFolder to target
		set target to (path to pictures folder) -- this can be any folder other than current target
		set target to currentFolder
	end tell
	
	-- useGroups() of me -- GUI script to enable group view
	
end tell

on useGroups()
	
	-- GUI script to enable group view	
	
end useGroups

peavine!

Thanks so much for your help on this. Your help has been great.
I am fairly new to AppleScript and still working through some books and support such as MacScripter has been super helpful and helpful people such as yourself. Again, I appreciate it.
I have very limited knowledge of GUI scripting, but when I run it i get an error such as this one:
System Events got an error: Can’t get menu item “Use Groups” of menu “View” of menu bar 1 of process “Finder”.

My new code is below. I isolated the GUI script part to do some troubleshooting on it.

Thanks again!


set chooseWindowLeft to choose folder
set chooseWindowRight to choose folder
--in order to take advantage of whole screen, check to see if dock is hidden, if not hide dock
tell application "System Events"
 	get autohide of the dock preferences
 	if result is false then
 		set autohide of dock preferences to true
 	end if
end tell
--with dock hidden, and folders chosen, two Finder windows will open up and be positioned vertically on the left and right side of the desktop 
tell application "Finder"
 	(*using container windows maybe a better way to execute this script:*)
 	--chooseWindowLeft
 	tell the front Finder window
 		open chooseWindowLeft
 		set the properties to {bounds:{0, 25, 640, 802}, current view:list view, toolbar visible:true, statusbar visible:true, pathbar visible:true, sidebar width:190}
 	end tell
 	tell list view options of the front Finder window
 		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:kind column}
 		
 		tell column name column
 			set properties to ¬
 				{index:1, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column size column
 			set properties to ¬
 				{index:5, sort direction:normal, width:72, visible:true}
 		end tell
 		tell column modification date column
 			set properties to ¬
 				{index:2, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column creation date column
 			set properties to ¬
 				{index:4, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column kind column
 			set properties to ¬
 				{index:3, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column label column
 			set properties to ¬
 				{index:6, sort direction:normal, width:72, visible:false}
 		end tell
 		tell column version column
 			set properties to ¬
 				{index:7, sort direction:normal, width:72, visible:false}
 		end tell
 		tell column comment column
 			set properties to ¬
 				{index:8, sort direction:normal, width:120, visible:false}
 		end tell
 	end tell
 	tell front Finder window
 		set currentFolder to chooseWindowLeft
 		set currentFolder to target
 		set target to currentFolder
 	end tell
 	delay 1
 	
 	--chooseWindowRight
 	tell the front Finder window
 		open chooseWindowRight
 		set the properties to {bounds:{639, 23, 1279, 800}, current view:list view, toolbar visible:false, statusbar visible:true, pathbar visible:true, sidebar width:0}
 	end tell
 	tell list view options of the front Finder window
 		set sort column to column kind column
 		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:kind column}
 		tell column name column
 			set properties to ¬
 				{index:1, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column size column
 			set properties to ¬
 				{index:5, sort direction:normal, width:72, visible:true}
 		end tell
 		tell column modification date column
 			set properties to ¬
 				{index:2, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column creation date column
 			set properties to ¬
 				{index:4, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column kind column
 			set properties to ¬
 				{index:3, sort direction:normal, width:120, visible:true}
 		end tell
 		tell column label column
 			set properties to ¬
 				{index:6, sort direction:normal, width:72, visible:false}
 		end tell
 		tell column version column
 			set properties to ¬
 				{index:7, sort direction:normal, width:72, visible:false}
 		end tell
 		tell column comment column
 			set properties to ¬
 				{index:8, sort direction:normal, width:120, visible:false}
 		end tell
 		
 	end tell
         	set current view of the front Finder window to list view
 		set currentFolder to chooseWindowRight
 		set currentFolder to target
 		set target to currentFolder
         tell front Finder window
 	         try
 			set the current view of the window of every folder to list view
 		end try
 	end tell
end tell


Kell. I did the following:

  • Opened a Finder window set to list view;
  • clicked on “Open this Scriplet in your Editor” in post 5 above; and
  • ran the script, which worked as expected.

Some possible reasons for our differing results:

  • We are on different versions of macOS with different Finder menu items.
  • The delay value of 1 second is not enough on your computer and needs to be increased (although this would not explain the error message you reported).

@iKel

Would be useful to tell us:
which is the system which you use. “Use Groups” was introduced with Mojave, older systems doesnt know it.

Most helpers behave as if every users are running macOS in English. Happily, they are wrong.
So, it ould be useful to tell us which is the language used on your system. Most of the strings triggered in GUI Scripting are localization dependent.

For instance, in French “View” is spelled “Presentation”.
When it’s available, in French “Use Groups” is spelled “Utiliser les groupes”.
Below is a script trying to get rid of that which grab the localized strings from dedicated resources.
Alas, I don’t know which is really the string used for View"

tell application "Finder"
	set useGroups_loc to localized string "N148.2" --> "Utiliser les groupes" in French
	set groupBy_loc to localized string "N148" --> "Grouper par" in French
	set kind_loc to localized string "N224" --> "Type" in French
	set view206_loc to localized string "206.title" from table "MenuBar" --> "Afficher" in French
	set view207_loc to localized string "207.title" from table "MenuBar" --> "Présentation" in French
	-- As I can't run Mojave or higher I don't kow if it's view206_loc or view207_loc which is really used
end tell
tell application "System Events"
	tell process "Finder"
		set frontmost to true
		set itemChecked to (value of attribute "AXMenuItemMarkChar" of menu item useGroups_loc of menu view207_loc of menu bar 1)
		if itemChecked = missing value then click menu item useGroups_loc of menu view207_loc of menu bar 1 -- maybe it's view206_loc
		delay 1
		click menu item kind_loc of menu 1 of menu item groupBy_loc of menu view207_loc of menu bar 1 -- maybe it's view206_loc
	end tell
end tell

For safe, I repeat : As I can’t run Mojave or higher I don’t kow if it’s view207_loc or view207_loc which is really used.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 4 juillet 2020 21:31:22

I rewrote the code so it no longer use a string whose I’m not sure of whichb it is.

tell application "Finder"
	set useGroups_loc to localized string "N148.2" --> "Utiliser les groupes" in French
	set groupBy_loc to localized string "N148" --> "Grouper par" in French
	set kind_loc to localized string "N224" --> "Type" in French
	set view_loc to 5 -- I‘m sure that the wanted menu bar item (View in English) is at index 5
end tell
tell application "System Events" to tell process "Finder"
	set frontmost to true
	tell menu bar 1
		-- name of menu bar items--> {"Apple", "Finder", "Fichier", "Édition", "Présentation", "Aller", "Fenêtre", "Aide"}
		tell menu bar item view_loc to tell menu 1
			name of menu items --> {"Par icônes", "Par liste", "Par colonnes", "Sous forme de Cover Flow", …}
			set itemChecked to (value of attribute "AXMenuItemMarkChar" of menu item useGroups_loc)
			if itemChecked = missing value then click menu item useGroups_loc
			tell menu item useGroups_loc
				repeat until exists menu 1
					delay 0.1
				end repeat
				tell menu 1 to click menu item kind_loc
			end tell
		end tell
	end tell
end tell

Hi all :slight_smile:
Peavine and Yvan, thanks so much for your help on this.

Peavine, are you on Catalina, as well? I thought we were running the same OS, so I thought it would work nonetheless.

@Yvan
You are correct. I need to provide more information. Sorry, I thought I did at least provide my macOS version. My system language is in English. I am running 10.15.5 (Catalina) and View is the 5 menu for me.

I will take both of your suggestions and see if I can get something working here.
Again, I appreciate the help guys!

Kell. I’m running Catalina.

I edited my script in post 6 to include your settings from post 1 and included a slightly modified version of the GUI script. Delays at a few points in the script may be needed depending on your computer’s speed. I tested this on my computer and it worked without issue.

set chooseWindowLeft to choose folder
set chooseWindowRight to choose folder

tell application "System Events"
	if (autohide of the dock preferences) is false then set autohide of dock preferences to true
end tell

tell application "Finder"
	
	open chooseWindowLeft
	set frontmost to true
	
	tell front Finder window
		set the properties to {bounds:{0, 25, 640, 802}, current view:list view, toolbar visible:true, statusbar visible:true, pathbar visible:true, sidebar width:190}
	end tell
	
	tell list view options of front Finder window
		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:kind column}
		tell column name column
			set properties to ¬
				{index:1, sort direction:normal, width:120, visible:true}
		end tell
		tell column size column
			set properties to ¬
				{index:5, sort direction:normal, width:72, visible:true}
		end tell
		tell column modification date column
			set properties to ¬
				{index:2, sort direction:normal, width:120, visible:true}
		end tell
		tell column creation date column
			set properties to ¬
				{index:4, sort direction:normal, width:120, visible:true}
		end tell
		tell column kind column
			set properties to ¬
				{index:3, sort direction:normal, width:120, visible:true}
		end tell
		tell column label column
			set properties to ¬
				{index:6, sort direction:normal, width:72, visible:false}
		end tell
		tell column version column
			set properties to ¬
				{index:7, sort direction:normal, width:72, visible:false}
		end tell
		tell column comment column
			set properties to ¬
				{index:8, sort direction:normal, width:120, visible:false}
		end tell
	end tell
	
	tell front Finder window -- refresh Finder window
		set currentFolder to target
		set target to (path to pictures folder) -- this can be any folder other than current target
		set target to currentFolder
	end tell
	
	useGroups() of me
	
	delay 1 -- test with various values
	
	open chooseWindowRight
	set frontmost to true
	
	tell front Finder window
		set properties to {bounds:{639, 23, 1279, 800}, current view:list view, toolbar visible:false, statusbar visible:true, pathbar visible:true, sidebar width:0}
	end tell
	
	tell list view options of front Finder window
		set the properties to {calculates folder sizes:false, shows icon preview:true, icon size:small, text size:12, uses relative dates:true, sort column:kind column}
		tell column name column
			set properties to ¬
				{index:1, sort direction:normal, width:120, visible:true}
		end tell
		tell column size column
			set properties to ¬
				{index:5, sort direction:normal, width:72, visible:true}
		end tell
		tell column modification date column
			set properties to ¬
				{index:2, sort direction:normal, width:120, visible:true}
		end tell
		tell column creation date column
			set properties to ¬
				{index:4, sort direction:normal, width:120, visible:true}
		end tell
		tell column kind column
			set properties to ¬
				{index:3, sort direction:normal, width:120, visible:true}
		end tell
		tell column label column
			set properties to ¬
				{index:6, sort direction:normal, width:72, visible:false}
		end tell
		tell column version column
			set properties to ¬
				{index:7, sort direction:normal, width:72, visible:false}
		end tell
		tell column comment column
			set properties to ¬
				{index:8, sort direction:normal, width:120, visible:false}
		end tell
	end tell
	
	tell front Finder window -- set folders to list view and refresh Finder
		try
			set the current view of the window of every folder to list view
		end try
		set currentFolder to target
		set target to (path to pictures folder) -- this can be any folder other than current target
		set target to currentFolder
	end tell
	
end tell

useGroups() of me

on useGroups()
	
	tell application "System Events"
		tell process "Finder"
			set itemChecked to (value of attribute "AXMenuItemMarkChar" of menu item "Use Groups" of menu "View" of menu bar 1)
			if itemChecked = missing value then
				click menu item "Use Groups" of menu "View" of menu bar 1
			else
				click menu item "Kind" of menu of menu item "Group By" of menu "View" of menu bar 1
			end if
		end tell
	end tell
	
end useGroups

Peavine,

It works! This GUI scripting is pretty cool. I need to learn how to do this. How did you learn this stuff. You have any good sources?
The other thing I wish I could figure out is why when I attempt to choose Home folder I get an error “Finder got an error: A column must be visible to make it the sort column.” From what I can see Kind column is visible in my home folder. Maybe that’s what that’s not implying.

Thanks for all your help. You’ve been great.

Kell. I’m glad to hear the script works. :slight_smile:

I haven’t found any good resources for GUI scripting. Whenever the need arises, I just do a Google search. Also, a few members of this forum are real pro’s with GUI scripting and will help should the need arise.

I haven’t encountered an issue with the script and my home folder and I can’t think of any reason it won’t work in yours. Just to make sure the column is visible, you may want to open a Finder window set to your home folder and run the following script:

tell application "Finder"
	set frontmost to true
	tell list view options of front Finder window
		set visibleCheck to visible of column kind column
	end tell
end tell

The visible property doesn’t work well at all and Nigel (the forum moderator) aptly described the situation as follows:

https://macscripter.net/viewtopic.php?pid=197715

Hey Peavine,
yeah that’s weird.

Yeah, as Nigel says even if you still try to set it to true - it remains false. Strange - and that it’s only for certain folders too.


tell application "Finder"
	set frontmost to true
	tell list view options of front Finder window
		set visibleCheck to visible of column kind column
		if false then
			set visible of column kind column to true
		end if
	end tell
end tell
--still get false if home folder is frontmost

Model: macBook Pro running macOS - Catalina (10.15.5)
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: Other

Kell. I had one final thought on this. As you probably know, most folders contain a hidden file named DS_Store, which contains configuartion information for that folder. These are easily deleted in one or several folders and are automatically rebuilt by the OS. Probably a long shot, but you might want to delete the DS_Store file in the home folder to see if that helps.

Cool,
I’ll give that a try.
Thanks for all your help on this peavine! Truly appreciate it.

Best regards,
Kell