I’m trying to create a script to maintain a consistent List View appearance across a folder and its subfolders, including:
which columns are shown and which are not
their order
their width
which column is used for sorting
sort direction
The usage I’m thinking about is to export it from Script Editor as an AppleScript application and add to the Finder toolbar:
But the script doesn’t really work yet. Could someone help to finish it?
set aFolder to choose folder
set myThen to current date -- time checker
recursiveHandler(aFolder as list)
on recursiveHandler(theFolders)
repeat with myFolder in theFolders
tell application "Finder"
tell front Finder window
set currentTarget to its target
set current view to list view
set options to its list view options
end tell
set current view of (container window of (alias (myFolder as text))) to list view -- Options are (icon view/list view/column view)
set folderList to folders of folder (myFolder as text)
--activate
delay 3
tell application "System Events" to tell process "Finder" -- show columns
tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
delay 3
repeat with boxName in {"Date Modified", "Date Created", "Date Last Opened", "Date Added", "Size", "Kind", "Version", "Comments", "Tags"} -- column names
if boxName is in {"Date Modified", "Date Created", "Size", "Kind", "Version", "Comments", "Tags"} then
tell checkbox boxName of group 1 of window 1 to if value is 0 then click -- check desired columns
else
tell checkbox boxName of group 1 of window 1 to if value is 1 then click -- uncheck others
end if
end repeat
delay 3
tell menu item "Hide View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
end tell
tell options
set width of column name column to 250
set width of column modification date column to 1
set width of column creation date column to 1
set width of column size column to 1
set width of column kind column to 1
set width of column version column to 1
set width of column comment column to 1
set width of column label column to 1
set the index of column name column to 1
set the index of column modification date column to 2
set the index of column creation date column to 3
set the index of column size column to 4
set the index of column kind column to 5
set the index of column version column to 6
set the index of column comment column to 7
set the index of column label column to 8
end tell
tell front Finder window -- workaround to refresh window to current column order
set its target to container of currentTarget -- previous...
set its target to currentTarget -- ...and back
end tell
delay 3
set folderList to folders of folder (myFolder as text)
my recursiveHandler(folderList) -- call this handler with the folders of this folder
end tell
end repeat
end recursiveHandler
There are two problems currently. The first one is that there is some error that simply prevents it from working properly.
The second is that I don’t completely understand how to make it work with the currently opened folder, instead of asking user to select it. This second problem is probably easier to solve.
john202307. There are many scripts out there that reset a Finder window, but your script is made much more complicated because it resets a folder and all of its subfolders and because your script uses GUI scripting which can raise timing issues.
Having said that and FWIW, I would take a different approach. I’ve removed a lot of stuff from your script just to get a basic approach that might work. The other stuff can then be added back.
The following script worked on my Sequoia computer. The script reports an error if there are more than 5 subfolders–this is just a precaution and can be changed to whatever you’re comfortable with.
--this script assumes that a Finder window is open to the target folder
tell application "Finder"
set targetFolder to target of front Finder window
try
set theSubfolders to every folder of the entire contents of targetFolder
if class of theSubfolders is not list then set theSubfolders to theSubfolders as list
on error
set theSubfolders to {}
end try
end tell
set theFolders to (targetFolder as list) & theSubfolders
if (count theSubfolders) is greater than 10 then display dialog "This script will only reset 10 or fewer subfolders" buttons {"OK"} cancel button 1 default button 1 --set to desired value
repeat with aFolder in theFolders
tell application "Finder" to set the target of Finder window 1 to aFolder
resetFinderWindow()
delay 1 --test smaller values
end repeat
tell application "Finder"
set target of front Finder window to container of targetFolder -- previous...
set target of front Finder window to targetFolder -- ...and back
end tell
on resetFinderWindow()
tell application "Finder"
tell front Finder window
set current view to list view
set options to its list view options
end tell
tell options --edit as desired
set width of column name column to 250
set width of column modification date column to 1
set width of column size column to 1
set width of column kind column to 1
set the index of column name column to 1
set the index of column kind column to 2
set the index of column size column to 3
set the index of column modification date column to 4
end tell
end tell
end resetFinderWindow
This looked like an interesting script. I was surprised by the “set width of column name column to 250” but left it alone for the first try. Copied a folder out of my Documents folder to the desktop and opened it in Finder.
After the first run, I then changed the first set of numbers as so:
on resetFinderWindow()
tell application "Finder"
tell front Finder window
set currentTarget to its target
set current view to list view
set options to its list view options
end tell
tell options --edit as desired
set width of column name column to 10
set width of column modification date column to 10
--set width of column creation date column to 10
set width of column size column to 10
set width of column kind column to 10
But nothing seemed to change at all. From the first try to the last the window stayed the same (see screenshot). Any ideas, I’m on Sonoma 14.7.4.
But nothing seemed to change at all. From the first try to the last the window stayed the same (see screenshot). Any ideas, I’m on Sonoma 14.7.4.
At the beginning it was not one script but two. Here they are, work fine for me on Sonoma and Sequoia. Now when you see how it looked before, it might be easier for you to figure out how to merge them in a single one.
The first script. It enables list view and adjusts columns, their order and width:
tell application "Finder"
tell front Finder window
set currentTarget to its target
set current view to list view
set options to its list view options
end tell
activate
delay 0.5
tell application "System Events" to tell process "Finder" -- show columns
tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
delay 0.5
repeat with boxName in {"Date Modified", "Date Created", "Date Last Opened", "Date Added", "Size", "Kind", "Version", "Comments", "Tags"} -- column names
if boxName is in {"Date Modified", "Date Created", "Size", "Kind", "Version", "Comments", "Tags"} then
tell checkbox boxName of group 1 of window 1 to if value is 0 then click -- check desired columns
else
tell checkbox boxName of group 1 of window 1 to if value is 1 then click -- uncheck others
end if
end repeat
delay 0.5
tell menu item "Hide View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
end tell
tell options
set width of column name column to 250
set width of column modification date column to 1
set width of column creation date column to 1
set width of column size column to 1
set width of column kind column to 1
set width of column version column to 1
set width of column comment column to 1
set width of column label column to 1
set the index of column name column to 1
set the index of column modification date column to 2
set the index of column creation date column to 3
set the index of column size column to 4
set the index of column kind column to 5
set the index of column version column to 6
set the index of column comment column to 7
set the index of column label column to 8
end tell
tell front Finder window -- workaround to refresh window to current column order
set its target to container of currentTarget -- previous...
set its target to currentTarget -- ...and back
end tell
end tell
The second script. It applies list view recursively:
set aFolder to choose folder
set myThen to current date -- time checker
recursiveHandler(aFolder as list)
display dialog "Views change done in " & ((current date) - myThen) & " seconds." -- time checker
on recursiveHandler(theFolders)
repeat with myFolder in theFolders
tell application "Finder"
set current view of (container window of (alias (myFolder as text))) to list view -- Options are (icon view/list view/column view)
set folderList to folders of folder (myFolder as text)
my recursiveHandler(folderList) -- call this handler with the folders of this folder
end tell
end repeat
end recursiveHandler
One thing which I didn’t figure out in your version yet — even after uncommenting commented-out lines and fixing a typo in column order numbering — is how to make it apply visible/hidden columns and their width recursively. Currently, it seems it doesn’t work recursively.
FWIW, I also posted the two scripts which are a base of what I posted in the very first post.
john202307. I tested both of your scripts and they work fine. And, it certainly might be possible to combine the two scripts using a recursive handler to do the work. However, in my mind, a simpler and perhaps more conventional approach is:
Get the target folder, which can be done either with a dialog or by getting the target of the front Finder window.
Get a list of the subfolders in the target folder.
For the target folder a) enable list view; b) enable/disable desired columns; c) adjust column order and width.
Loop through each subfolder and a) enable list view; b) enable/disable desired columns; c) adjust column order and width.
Refresh Finder window to see changes.
Is there some reason you want to use a recursive handler or do you see some error in my thinking as explained above? BTW, I edited my earlier script so that it works with no or multiple subfolders.
I guess I should have really asked, what part of the script adjusts the width of the various columns. I would have thought that changing the name columns width number from 250 down to 10, would have done something, but apparently it must be some other setting. Or is what I’m asking not possible?
I don’t know if this is helpful, or just clutter, but I rewrote my existing Finder-reset script to use the recursive approach employed in my script included earlier in this thread. I then tested the script on a target folder that contained 147 subfolders, and the script worked without issue (although this took some time).
--this script assumes that a Finder window is open to the target folder.
--this script also assumes that visible columns are name, date modified, size, and kind.
tell application "Finder"
activate
set targetFolder to target of front Finder window
try
set theSubfolders to every folder of the entire contents of targetFolder
if class of theSubfolders is not list then set theSubfolders to theSubfolders as list
on error
set theSubfolders to {}
end try
end tell
set theFolders to (targetFolder as list) & theSubfolders
if (count theSubfolders) is greater than 10 then display dialog "This script will only reset 10 or fewer subfolders" buttons {"OK"} cancel button 1 default button 1 --set to desired value
repeat with aFolder in theFolders
tell application "Finder" to set the target of Finder window 1 to aFolder
resetFinderWindow()
delay 0.5 --test different values
end repeat
tell application "Finder" to tell front Finder window --refresh Finder window
set target to (path to public folder)
set target to targetFolder
end tell
on resetFinderWindow()
tell application "Finder"
tell window 1
if not (exists) then error number -128
set {x, y} to position
set the bounds to {x, y, x + 1170, y + 700}
end tell
tell Finder window 1
set properties to {current view:list view, pathbar visible:true, sidebar width:200}
end tell
tell list view options of Finder window 1
set properties to {calculates folder sizes:false, shows icon preview:false, icon size:small, text size:14, sort column:name column, uses relative dates:true}
set properties of column name column to {index:1, sort direction:normal, width:0}
set properties of column modification date column to {index:2, sort direction:normal, width:175}
set properties of column size column to {index:3, sort direction:normal, width:100}
set properties of column kind column to {index:4, sort direction:normal, width:175}
end tell
end tell
end resetFinderWindow
Homer. Please see how column widths are set in my script immediately above. However, it’s important to note that column widths are constrained by some minimal values, and the width of the Name column is generally residual. In any event, and just as a matter of personal preference, I’ve found it best to set Finder window width, sidebar width, and column widths so that everything fits as you want. This takes a bit of testing, but you only have to do it once.