Can anyone explain why the following script works as expected, but if you comment out the one line “choose from list allFiles” it does nothing.
tell application "Finder"
activate
set allFiles to the name of every file in the front window
choose from list allFiles
set the current view of the front Finder window to icon view
tell application "System Events"
tell process "Finder"
tell menu item 1 of menu 1 of menu item 7 of menu 1 of menu bar item 5 of menu bar 1 to click
end tell
end tell
end tell
The ‘choose from list’ line doesn’t make any difference for me either. But the script itself is slightly fragile.
The ‘set allFiles.’ line will error if the front window isn’t a ‘Finder window’.
The GUI Scripting won’t work if the “Keep arranged by” box in the window’s “View Options” is ticked, because that disables the menu item you’re trying to click. It would be more sure-footed to script the Finder directly for that:
tell application "Finder"
activate
tell front Finder window
if (it exists) then
-- set allFiles to the name of every file of its target
-- choose from list allFiles
set its current view to icon view
set arrangement of its icon view options to arranged by name
end if
end tell
end tell