Consistent List View appearance across a folder and its subfolders

One more questionm, if you don’t mind :slight_smile:

The following block works fine:

tell options
  set properties of column name column to {index:1, sort direction:normal, width:100}
  set properties of column comment column to {index:2, sort direction:normal, width:1}
  set properties of column creation date column to {index:3, sort direction:normal, width:1}
  set properties of column kind column to {index:4, sort direction:normal, width:1}
  set properties of column label column to {index:5, sort direction:normal, width:1}
  set properties of column modification date column to {index:6, sort direction:normal, width:1}
  set properties of column size column to {index:7, sort direction:normal, width:1}
  set properties of column version column to {index:8, sort direction:normal, width:1}
end tell

But do you know which names to use to address the “Date Added” and “Date Last Opened” columns?

set properties of column date added column to ...

and

set properties of column date last opened column to ...

don’t work.

I couldn’t find anything that works. Also, in the following screenshot the sort column should be “Date Last Opened” and it is instead “name”. So, setting properties for those two columns may not be possible.

1 Like

Yes, sadly even

set properties of every column to ...

doesn’t work for them.

@peavine It turned out the current version doesn’t work for smart folders.

global mediumDelay, shortDelay, longDelay
set mediumDelay to 1 -- edit as necessary
set shortDelay to 0.5 -- edit as necessary
set longDelay to 2 -- edit as necessary

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

-- optionally
set theSubfoldersMax to 10 -- edit as desired
if (count theSubfolders) is greater than theSubfoldersMax then
  display dialog "This script will only reset" & space & theSubfoldersMax & space & "or fewer subfolders" buttons {"OK"} cancel button 1 default button 1
end if

repeat with aFolder in theFolders
  tell application "Finder" to set the target of Finder window 1 to aFolder
  resetFinderWindow()
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

--display dialog "Done." buttons {"OK"} default button "OK" -- optionally

on resetFinderWindow()
  tell application "Finder"
    tell front Finder window to set current view to list view
    activate

    tell application "System Events" to tell process "Finder"
      delay shortDelay -- edit as necessary
      tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
      delay mediumDelay -- edit as necessary
      repeat with boxName in {"Comments", "Date Added", "Date Created", "Date Last Opened", "Date Modified", "Kind", "Size", "Tags", "Version"}
        if boxName is in {"Comments", "Date Created", "Date Modified", "Kind", "Size", "Tags", "Date Added", "Date Last Opened"} then -- edit as desired
          tell checkbox boxName of group 1 of window 1 to if value is 0 then click
        else
          tell checkbox boxName of group 1 of window 1 to if value is 1 then click
        end if
      end repeat
      delay mediumDelay -- edit as necessary
      tell menu item "Hide View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
      delay mediumDelay -- edit as necessary
    end tell

    tell front Finder window to set options to its list view options

    tell options -- edit as desired
      set properties of column name column to {index:1, width:100}
      set properties of column comment column to {index:2, width:0}
      set properties of column creation date column to {index:3, width:0}
      set properties of column kind column to {index:4, width:0}
      set properties of column label column to {index:5, width:0}
      set properties of column modification date column to {index:6, width:0}
      set properties of column size column to {index:7, width:0}
      set properties of column version column to {index:8, width:0}
      -- "Date Added" and "Date Last Opened" aren't supported
    end tell
  end tell
end resetFinderWindow

I tried to adjust it to handle this case, but stumbled out. The idea is that in smart folders the script should turn columns on and off and control their width and order, but should not work recursively.

Here is the adjusted work-in-progress version. Could you take a look on it?

global mediumDelay, shortDelay, longDelay
set mediumDelay to 1 -- edit as necessary
set shortDelay to 0.5 -- edit as necessary
set longDelay to 2 -- edit as necessary

tell application "Finder"
  set targetFolder to target of front Finder window
  set isSmartFolder to false
  try
    -- attempt to get a POSIX path (fails for smart folders)
    set testPath to POSIX path of (targetFolder as alias)
  on error
    set isSmartFolder to true
  end try
end tell

if isSmartFolder then
  -- only apply settings to the smart folder view, no recursion
  resetFinderWindow()
else
  -- process regular folders recursively
  tell application "Finder"
    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

  set theSubfoldersMax to 10 -- edit as desired
  if (count theSubfolders) is greater than theSubfoldersMax then
    display dialog "This script will only reset" & space & theSubfoldersMax & space & "or fewer subfolders" buttons {"OK"} cancel button 1 default button 1
  end if

  repeat with aFolder in theFolders
    tell application "Finder" to set the target of Finder window 1 to aFolder
    resetFinderWindow()
  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
end if

on resetFinderWindow()
  tell application "Finder"
    tell front Finder window to set current view to list view
    activate

    tell application "System Events" to tell process "Finder"
      delay shortDelay -- edit as necessary
      tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
      delay mediumDelay -- edit as necessary
      repeat with boxName in {"Comments", "Date Added", "Date Created", "Date Last Opened", "Date Modified", "Kind", "Size", "Tags", "Version"}
        if boxName is in {"Comments", "Date Created", "Date Modified", "Kind", "Size", "Tags", "Date Added", "Date Last Opened"} then -- edit as desired
          tell checkbox boxName of group 1 of window 1 to if value is 0 then click
        else
          tell checkbox boxName of group 1 of window 1 to if value is 1 then click
        end if
      end repeat
      delay mediumDelay -- edit as necessary
      tell menu item "Hide View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
      delay mediumDelay -- edit as necessary
    end tell

    tell front Finder window to set options to its list view options

    tell options -- edit as desired
      set properties of column name column to {index:1, width:100}
      set properties of column comment column to {index:2, width:0}
      set properties of column creation date column to {index:3, width:0}
      set properties of column kind column to {index:4, width:0}
      set properties of column label column to {index:5, width:0}
      set properties of column modification date column to {index:6, width:0}
      set properties of column size column to {index:7, width:0}
      set properties of column version column to {index:8, width:0}
    end tell
  end tell
end resetFinderWindow

john202307. As you may know, a Smart Folder is an alias file and will simply be skipped by your script if it’s in the target folder or in a subfolder of the target folder.

If the target of the front Finder window is a Smart Folder, then your script calls the resetFinderWindow handler and does in fact make changes to the Smart Folder. However, in my testing, every column in the Smart Folder is made visible (see screenshot below). My knowledge of GUI scripting is extremely limited, and I don’t know why this happens.

If you want to process Smart Folders that are in the target folder or in one of its subfolders, then you need to include the Smart Folders in the list of subfolders and the following code demonstrates how that might be done. However, Finder is very slow when doing this type of operation and might simply fail if the number of files in the target folder is large.

set targetFolder to "Macintosh HD:Users:robert:Working:" --set to desired value

tell application "Finder"
	set theSubfolders to every folder of the entire contents of folder targetFolder
	if class of theSubfolders is not list then set theSubfolders to theSubfolders as list
	set smartFolders to every file of the entire contents of folder targetFolder whose name extension is "savedSearch"
end tell

set regularAndSmartFolders to theSubfolders & smartFolders

It’s just my personal opinion but getting Smart Folders to work with your script is going to be difficult, and unless you have a lot of Smart Folders, I’d be inclined to skip them.

No, what I’m trying to achieve is to make the script work if a smart folder is the target of the front window. Or, in simpler words, if I have “opened” a smart folder. When I try to use the script in a smart folder, Finder get an error:

Can’t set alias file “” to item 1 of {alias file “” of «script»}.

That error occurs here:

tell application "Finder" to set the target of Finder window 1 to aFolder

John202307. I tested before posting and retested just now, and I do not get that error message. The line that you indicate is reporting an error is in the else section of the if statement, and that code should not be executed if the target folder is a Smart Folder. You may want to insert a few display dialog commands in the if and else sections of the if statement to see what code is in fact being executed.

The detection of whether the current folder is a smart folder works correctly.

tell application "Finder"
	set targetFolder to target of front Finder window
	set isSmartFolder to false
	try
		set testPath to POSIX path of (targetFolder as alias)
	on error
		set isSmartFolder to true
	end try
end tell

display dialog isSmartFolder

And yes, currently the script enables all of the possible columns. But I don’t understand how to make it sort columns and adjust their width, the same way as the original script.

In other words, I don’t understand why the tell options block doesn’t have any effect.

I hadn’t noticed this. :frowning:

I observed the same thing, and I don’t know the reason for that.

1 Like

Could you take a look on this new version?

I adjusted the script according your recommendation…

(If you want to process Smart Folders that are in the target folder or in one of its subfolders, then you need to include the Smart Folders in the list of subfolders and the following code demonstrates how that might be done. However, Finder is very slow when doing this type of operation and might simply fail if the number of files in the target folder is large.)

…so that now if the folder of the frontmost Finder window contains a smart folder, the script will open that smart folder and turn the columns on or off there according to the script’s settings, the same as in regular folder (but see Note 1 below).

However, despite you can see by your eyes that the script opens the smart folder and opens its View Options window:

  • it seems it doesn’t try to click the check boxes there
  • whether a specific check box is “on” or “off” doesn’t represent whether the corresponding column is visible or not
  • and as a consequence, when you re-visit that smart folder, it will look the same as before you run the script.

How to force it remember the changes?

And a few side notes about this version:

  1. I gave up trying to set index and width of columns in smart folders. This seems to be a limitation of Finder, and it doesn’t have easy workaround.
  2. I adjusted the script so that now it processes 5 (or any other number of) subfolders, and then asks you whether to process the next 5.
global mediumDelay, shortDelay, longDelay
set mediumDelay to 1
set shortDelay to 0.5
set longDelay to 2

tell application "Finder"
  set targetFolder to target of front Finder window
  set isSmartFolder to false
  try
    set testPath to POSIX path of (targetFolder as alias)
  on error
    set isSmartFolder to true
  end try
end tell

if isSmartFolder then
  resetFinderWindow(isSmartFolder)
else
  tell application "Finder"
    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
    set smartSubfolders to every file of the entire contents of targetFolder whose name extension is "savedSearch"
  end tell

  set regularAndSmartSubfolders to theSubfolders & smartSubfolders
  set theFolders to (targetFolder as list) & regularAndSmartSubfolders

  set theSubfoldersMax to 5 -- edit as desired
  set folderCount to count theSubfolders
  set processedCount to 0

  repeat with aFolder in theFolders
    tell application "Finder" to set the target of Finder window 1 to aFolder
    resetFinderWindow(false)
    set processedCount to processedCount + 1

    if processedCount mod theSubfoldersMax is 0 and processedCount is less than folderCount then
      display dialog "Processed " & processedCount & " subfolders. Continue?" buttons {"No", "Yes"} default button "Yes"
      if button returned of result is "No" then exit repeat
    end if
  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
end if

on resetFinderWindow(isSmartFolder)
  tell application "Finder"
    tell front Finder window to set current view to list view
    activate

    tell application "System Events" to tell process "Finder"
      delay shortDelay
      tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
      delay mediumDelay
      repeat with boxName in {"Comments", "Date Added", "Date Created", "Date Last Opened", "Date Modified", "Kind", "Size", "Tags", "Version"}
        if boxName is in {"Comments", "Date Created", "Date Modified", "Kind", "Size", "Tags", "Date Added", "Date Last Opened"} then -- edit as desired
          tell checkbox boxName of group 1 of window 1 to if value is 0 then click
        else
          tell checkbox boxName of group 1 of window 1 to if value is 1 then click
        end if
      end repeat
      delay mediumDelay
      tell menu item "Hide View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
      delay mediumDelay
    end tell

    if not isSmartFolder then
      tell front Finder window to set options to its list view options
      tell options -- edit as desired
        set properties of column name column to {index:1, width:100}
        set properties of column comment column to {index:2, width:0}
        set properties of column creation date column to {index:3, width:0}
        set properties of column kind column to {index:4, width:0}
        set properties of column label column to {index:5, width:0}
        set properties of column modification date column to {index:6, width:0}
        set properties of column size column to {index:7, width:0}
        set properties of column version column to {index:8, width:0}
      end tell
    end if
  end tell
end resetFinderWindow

john202307. I tested your script where the target of the front Finder window was a regular folder. This folder contained a regular folder and a smart folder. I then ran your script, and all of the columns in all of the folders (including the smart folder) were reset as expected. However, closing and opening a Finder window caused the smart folder but not the other folder to reset to a different set of columns. I reran this test where the target of the front Finder window was the smart folder and saw the same result.

This is pretty much what you are seeing but with one difference. The smart folder does not reset to the columns that were visible before your script was run. Instead, it resets to the columns that almost all of my other folders are set to (an apparent default). This is not in itself surprising, as a smart folder is an alias file not a folder.

What is a bit surprising occurs if I manually change the visible columns in a smart folder by way of the Show View Options menu. In this instance, after restarting the Finder window, the columns reset the apparent default noted above. So, this does not appear to have anything to do with your script.

Anyways, 1) I don’t know how to fix your script to set visible columns in smart folders; and 2) an interesting question is how the apparent default columns are set.

BTW, I didn’t look at the portion of the script that works on multiple smart folders, because (at least for now) resetting visible columns in a smart folder does not appear to work (at least on my Sequoia computer). If there’s something in particular you want me to look at, please let me know. I am not seeing the first two bulleted points in the portion of your post quoted above, but the important point is that in my testing, the columns in a smart folder can be changed manually or by way of your script but these changes are not seen when the Finder window is restarted.

So, when all is said and done, the central question remains:

1 Like

Here is a video: https://easyupload.io/b7pxbp (40 MB) Does it work somewhat differently for you?

1 Like

Is it correct that for you the script works somewhat differently?

I gave up attempts to figure out how to enable/disable columns in smart folders, and reverted my own home version one step back. Here it is, with comments and minor adjustments:

(* https://discussions.apple.com/thread/3533331
 * https://macscripter.net/t/35411
 * https://macscripter.net/t/76565
 * https://macscripter.net/t/76906
 *
 * current limitations:
 * - if the target of the frontmost Finder window is a smart folder, the script will process it non-recursively and also its columns indices and widths won't be changed
 * - if the target of the frontmost Finder window is a regular folder containing smart folders, those smart folders won't be processed
 * - also, Finder doesn't allow to change index or width of "Date Added" and "Date Last Opened" columns, even in regular folders
 *)

global mediumDelay, shortDelay, longDelay
set mediumDelay to 1 -- edit as necessary
set shortDelay to 0.5 -- edit as necessary
set longDelay to 2 -- edit as necessary

tell application "Finder"
	set targetFolder to target of front Finder window
	set isSmartFolder to false
	try
		set testPath to POSIX path of (targetFolder as alias)
	on error
		set isSmartFolder to true
	end try
end tell

if isSmartFolder then
	resetFinderWindow(isSmartFolder)
else
	tell application "Finder"
		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
	
	set theSubfoldersMax to 5 -- edit as desired
	set folderCount to count theSubfolders
	set processedCount to 0
	
	repeat with aFolder in theFolders
		tell application "Finder" to set the target of Finder window 1 to aFolder
		resetFinderWindow(false)
		set processedCount to processedCount + 1
		
		if processedCount mod theSubfoldersMax is 0 and processedCount is less than folderCount then
			display dialog "Processed" & space & processedCount & space & "subfolders. Continue?" buttons {"No", "Yes"} default button "Yes"
			if button returned of result is "No" then exit repeat
		end if
	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
end if

on resetFinderWindow(isSmartFolder)
	tell application "Finder"
		tell front Finder window to set current view to list view
		activate
		
		tell application "System Events" to tell process "Finder"
			delay shortDelay -- edit as necessary
			tell menu item "Show View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
			delay mediumDelay -- edit as necessary
			
			set enabledColumns to {"Comments", "Date Added", "Date Created", "Date Last Opened", "Date Modified", "Kind", "Size", "Tags"} -- edit as desired
			repeat with boxName in {"Comments", "Date Added", "Date Created", "Date Last Opened", "Date Modified", "Kind", "Size", "Tags", "Version"}
				try
					set theCheckbox to checkbox boxName of group 1 of window 1
					if (boxName is in enabledColumns and value of theCheckbox is 0) or (boxName is not in enabledColumns and value of theCheckbox is 1) then click theCheckbox
				end try
			end repeat
			
			delay mediumDelay -- edit as necessary
			tell menu item "Hide View Options" of menu of menu bar item "View" of menu bar 1 to if exists then click
			delay mediumDelay -- edit as necessary
		end tell
		
		if not isSmartFolder then
			tell front Finder window to set options to its list view options
			tell options -- edit as desired
				set properties of column name column to {index:1, width:0}
				set properties of column comment column to {index:2, width:0}
				set properties of column creation date column to {index:3, width:0} -- Date Created
				set properties of column modification date column to {index:4, width:0} -- Date Modified
				set properties of column kind column to {index:5, width:0}
				set properties of column size column to {index:6, width:0}
				set properties of column label column to {index:7, width:0} -- Tags
				set properties of column version column to {index:8, width:0}
			end tell
		end if
	end tell
end resetFinderWindow

Also, I tried to improve this part:

tell options -- edit as desired
  set properties of column name column to {index:1, width:0}
  set properties of column comment column to {index:2, width:0}
  set properties of column creation date column to {index:3, width:0} -- Date Created
  set properties of column modification date column to {index:4, width:0} -- Date Modified
  set properties of column kind column to {index:5, width:0}
  set properties of column size column to {index:6, width:0}
  set properties of column label column to {index:7, width:0} -- Tags
  set properties of column version column to {index:8, width:0}
end tell

so that to rearrange columns it will be possible to use a list and rearrange lines there, but with no luck.

So, I’m going to stick with that version.

1 Like

John202307. I happened to notice that–unlike regular folders–smart folders do not have a Use as Defaults button in their View Options dialog. This would seem to confirm what you’ve already concluded–that it’s not possible to permanently enable/disable columns in smart folders. Your script works great with regular folders and that’s a significant accomplishment. :+1:

1 Like