One-time resize of Finder window's column when window opens - only if it is not at specific size

I have a macro that resizes the Name column of Finder windows (see attached). This works great via a keystroke, but what I would like to do is make it happen automatically when I open Finder windows and ONLY if the Name column is NOT my desired width. If I set the trigger to "The Focused Window Changes", what happens is the macro runs every time I switch back and forth between windows, even if the Name column is the correct width - this gets fairly annoying. Is there any way to have it run only when needed?

Resize Finder Window - Column 500.kmmacros (3.5 KB)

You can add a condition (see comment in the script):

use AppleScript version "2.4"
use scripting additions

set otherFolder to path to favorites folder

tell application "Finder"
  set currentFolder to insertion location as alias
  
  tell front window
    tell its list view options
      tell column name column
        # Abort if column width is 500
        if width = 500 then error number -128
        set properties to {index:1, width:500}
      end tell
      set properties to {calculates folder sizes:true, shows icon preview:false, icon size:small icon, text size:14, uses relative dates:true, sort column:name column}
      
    end tell
    
    set its target to otherFolder
    set its target to currentFolder
  end tell
  
end tell

Alternatively, you can also prepend the condition to the whole script:

Resize Finder Window - Column 500 (conditional) <8B61 200315T174023>-pty-fs8

Resize Finder Window - Column 500 (conditional) <8B61 200315T174023>.kmmacros (4.8 KB)

This script seems to do the trick...

Glad to hear :slight_smile:

One problem with this approach is that it fails on search windows (smart folders), which don't have an insertion location. And I don't know about the OP, but those are just where I seem to have a problem with cramped name columns. Is there any other way to force a refresh besides switching to a different folder and back, or alternatively to capture and restore the current search if the window is open to a smart folder?

You could use the Finder’s Back keystroke in these cases (default: [ ):

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set otherFolder to path to favorites folder
set currentFolder to missing value

tell application "Finder"
  try
    set currentFolder to insertion location as alias
  end try
  tell front window
    tell its list view options
      tell column name column
        if width = 500 then error number -128
        set width to 500
      end tell
    end tell
    set target to otherFolder
    if currentFolder ≠ missing value then
      set target to currentFolder
    else
      tell application "Finder" to activate # not necessary if frontmost
      tell application "System Events" to keystroke "[" using command down
    end if
  end tell
end tell

Resize Finder Window - Column 500 (extended version) <F359 200317T131010>.kmmacros (3.1 KB)



Or, a more KM-“native” variant:

Resize Finder Window - Column 500 (extended version II) <19C3 200317T135637>-pty-fs8

Resize Finder Window - Column 500 (extended version II) <19C3 200317T135637>.kmmacros (5.3 KB)

1 Like