Setting arrangement of new Finder Window via AppleScript

applescriptfinder

I want to create a new Finder Window, and change the default arrangement from "None" to "arrange by name". Here is my code that does everything except the arrange by name part:

tell application "Finder"
    set theWindow to make new Finder window
    tell theWindow
        set current view to icon view
    end tell
    tell its Finder window theWindow
        set arrangement of icon view options of theWindow to arranged by name
    end tell
end tell

I have tried the code without using the "tell its Finder Window" and using just the "tell theWindow" block, but that does nothing as well.

What is weird is, in the Finder AppleScript dictionary the "Icon View Options" property says its read only. But the "Icon View Options" class says that it is read/write, and it even has an arrangement property that also says that it is read/write.

So has anyone been able to successfully change the "arrangement" property and has the change been reflected in the Finder window?

Best Answer

  • For reasons of which I cannot explain, this following code was unreliable and only worked about 50% of the time.

    tell application "Finder"
            set finderWindowID to (make new Finder window)
            set current view of finderWindowID to icon view
            set arrangement of icon view options of finderWindowID to arranged by name
    end tell
    

    After trying several different things, I realized if I ran the code once, then closed that Finder window that was created, and ran the code again... I got the desired results every time.

    I know it's ugly and not very efficient but this produced the proper results.

    setIconView()
    tell application "Finder" to close finderWindowID
    setIconView()
    
    to setIconView()
        global finderWindowID
        tell application "Finder"
            set finderWindowID to (make new Finder window)
            set current view of finderWindowID to icon view
            set arrangement of icon view options of finderWindowID to arranged by name
        end tell
    end setIconView