AppleScript To Set Folder Views

I am using macOS 10.12.

I created an Automator that uses the Set Folder Views action, recursively on sub-folders. I use List View for nearly all of my folders, except for a select few.

I want to set the attributes Arrange By to None, and, Sort By to Name. The Automator Set Folder View action does not provide this capability.

I use AppleScript infrequently, given that my knowledge of it is limited.

Is anyone aware of an AppleScript that would perform this on both folders and their sub-folders, recursively? My searches have not found one.

Unless someone knows how to use Automator to set Arrange By and Sort By, I would like to use a more elegant solution, in which a single AppleScript sets all of my folder attributes (that I am using Automator to do), including Arrange By and Sort By, instead of running an Automator and an AppleScript to obtain the desired results.

Thank you.

Kurt

You may try to use :

# Requires BridgePlus available for free at : https://www.macosxautomation.com/applescript/apps/BridgePlus.html
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework

script o
	property showOptions : ""
end script
set theFolder to (choose folder)
set PosixPath to POSIX path of theFolder
# Grab the localized version of the menu item "Afficher les options de présentation"
tell application "Finder" to set o's showOptions to localized string "N35"
(* List the folders of the selected one *)
set theResult to current application's SMSForder's foldersIn:PosixPath recursive:true skipHidden:true skipInsidePackages:true asPaths:false
set theFolders to theResult as list # une liste d' URLs
# Add the root folder to the list
set end of theFolders to theFolder
--return (count theFolders as text)
tell application "Finder"
	repeat with aFolder in theFolders
		open aFolder
		try
			set current view of window 1 to icon view
		end try
		my ruleSettings()
		close window 1
	end repeat
end tell

on ruleSettings()
	tell application "System Events" to tell process "Finder"
		set frontmost to true
		if name of menu item 21 of menu 1 of menu bar item 5 of menu bar 1 = o's showOptions then
			keystroke "j" using {command down} -- open Presentation settings window
			
			repeat 10 times
				if subrole of window 1 is "AXSystemFloatingWindow" then exit repeat
				delay 0.1
			end repeat
		end if
		tell window 1
			(*
class of UI elements --> {checkbox, checkbox, static text, pop up button, pop up button, static text, group, button, button, static text}
role description of buttons --> {"bouton", "Bouton de fermeture"}
*)
			tell pop up button 1
				repeat 10 times
					try
						click it --> pop up button "Trier par :"
						exit repeat
					end try
					delay 0.2
				end repeat
				-- name of menu items of menu 1
				--> {"Aucun", missing value, "Aligner sur la grille", missing value, "Nom", "Type", "Date de dernière ouverture", "Date de l'ajout", "Date de modification", "Date de création", "Taille", "Tags"}
				repeat 10 times # ran it upon a folder with 1 753 853 folders and never loop more than 5 times
					try
						set byName to name of menu item 5 of menu 1
						exit repeat
					end try
					delay 0.2
				end repeat
				click menu item byName of menu 1 # "Nom"
			end tell # pop up button 1
			delay 0.2
			tell pop up button 2
				repeat 10 times
					try
						click it --> pop up button "Organiser par :".
						exit repeat
					end try
					delay 0.2
				end repeat
				-- name of menu items of menu 1
				--> {"Nom", "Type", "Application", "Date de dernière ouverture", "Date de l'ajout", "Date de modification", "Date de création", "Taille", "Tags", missing value, "Aucun"}
				repeat 10 times # ran it upon a folder with 1 753 853 folders and never loop more than 5 times
					try
						set noneItem to name of menu item -1 of menu 1
						exit repeat
					end try
					delay 0.2
				end repeat
				click menu item noneItem of menu 1 # "None"
			end tell # pop up button 1
			click button 2 # close the window
		end tell
	end tell
end ruleSettings

As the icon view options are defined as read only, I use GuiScripting.

Edited because when I ran it upon a folder with 1,753,853 subfolders the wanted menu item wasn’t available immediately. A loop with a try/ end try is now taking care of that. It wait from 0 to 2 seconds for the availability of the item. On the large folder used for tests, it loops 5 times only once.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 30 novembre 2016 18:16:03

Yvan, that’s some great AppleScripting!

I am needing to do something similar for a client, but I’m having trouble making the proper modifications.

The client wants all folders in List view, rather than Icon view–I was able to make that change to the code successfully.

What I can’t figure out how to do is change the code to manipulate the View Options window to:

1.) Check the check box at the top: “Always open in list view”
2.) Check the box under that: “Browse in list view”
3.) Arrange by “Name”
4.) Sort by “Kind”

I’ve tried making some changes that seemed logical to me, but it’s ending up with Arrange by “None” and sort by “Date Modified”

I’d also like to have AppleScript click the “Use as Defaults” button at the bottom of the View Options window, if possible.

I did get BridgePlus successfully installed in ~/Library/Scripting Libraries. I had to log out and back in to get Script Editor to notice it, but it’s there and working on both Sierra and High Sierra.

Thanks in advance, Yvan, or anyone else that can help!

Model: Mac Pro mid-2010
Browser: Safari 604.1.38
Operating System: Mac OS X (10.12.5)

It seems that insomnia is your friend.

Try to run :

(*
http://www.macscripter.net/viewtopic.php?id=45370
*)


# Requires BridgePlus available for free at : https://www.macosxautomation.com/applescript/apps/BridgePlus.html
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use script "BridgePlus"
load framework

script o
	property showOptions : ""
end script
set theFolder to (choose folder)
set PosixPath to POSIX path of theFolder
# Grab the localized version of the menu item "Afficher les options de présentation"
tell application "Finder" to set o's showOptions to localized string "N35"

(* List the folders of the selected one *)
set theResult to current application's SMSForder's foldersIn:PosixPath recursive:true skipHidden:true skipInsidePackages:true asPaths:false
set theFolders to theResult as list # une liste d' URLs
# Add the root folder to the list
set end of theFolders to theFolder
--return (count theFolders as text)
tell application "Finder"
	repeat with aFolder in theFolders
		open aFolder
		try
			set current view of window 1 to list view
		end try
		my ruleSettings()
		close window 1
	end repeat
end tell

on ruleSettings()
	tell application "System Events" to tell process "Finder"
		set frontmost to true
		if name of menu item 21 of menu 1 of menu bar item 5 of menu bar 1 = o's showOptions then
			keystroke "j" using {command down} -- open Presentation settings window
			
			repeat 10 times
				if subrole of window 1 is "AXSystemFloatingWindow" then exit repeat
				delay 0.1
			end repeat
		end if
		tell window 1
			(*
class of UI elements --> {checkbox, checkbox, static text, pop up button, pop up button, static text, group, button, button, static text}
role description of buttons --> {"bouton", "Bouton de fermeture"}
*)
			name of buttons --> {"Utiliser comme valeurs par défaut", missing value}
			name of pop up buttons --> {"Trier par :", "Organiser par :"}
			
			tell pop up button 1
				log (get its name)
				repeat 10 times
					try
						click it --> pop up button "Trier par :"
						exit repeat
					end try
					delay 0.2
				end repeat
				name of menu items of menu 1
				--> {"Nom", "Type", "Date de dernière ouverture", "Date de l’ajout", "Date de modification", "Date de création", "Taille", "Tags"}
				
				repeat 10 times # ran it upon a folder with 1 753 853 folders and never loop more than 5 times
					try
						set byKind to name of menu item 2 of menu 1
						exit repeat
					end try
					delay 0.2
				end repeat
				--log byKind
				click menu item byKind of menu 1 # "Kind"
			end tell # pop up button 1
			
			delay 0.2
			tell pop up button 2
				--log (get its name)
				repeat 10 times
					try
						click it --> pop up button "Organiser par :"…
						exit repeat
					end try
					delay 0.2
				end repeat
				--name of menu items of menu 1
				--> {{"Nom", "Type", "Application", "Date de dernière ouverture", "Date de l’ajout", "Date de modification", "Date de création", "Taille", "Tags", missing value, "Aucun"}}
				
				repeat 10 times # ran it upon a folder with 1 753 853 folders and never loop more than 5 times
					try
						set nameItem to name of menu item 1 of menu 1
						exit repeat
					end try
					delay 0.2
				end repeat
				--	log nameItem
				click menu item nameItem of menu 1 # "Name"
			end tell # pop up button 1
			
			click button 1 # "Utiliser comme valeurs par défaut"
			click button 2 # close the window
		end tell
	end tell
end ruleSettings

My understanding is that you missed that pop up button 1 is : Sort by :
and pop up button 2 is : Arrange by :

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) lundi 23 octobre 2017 23:55:38