Can a finder window be set to "arranged by" with Applescript?

Hello and thanks for reading this.
Very newbie here and trying slowly and painfully to learn by myself Applescript (with “Apple Training Series AppleScript 1-2-3”). :slight_smile:
Tried to search the net and forums (including this one) but to no avail.
I need to open a finder window (or set an open one) to be arranged by (kind, name, etc.).
I hope it’s doable and if it is, I guess the answer will be much shorter than my question. :slight_smile:
Thanks in advance for help.
BD

The Finder’s dictionary states :

icon view options‚n : the icon view options
properties
arrangement (not arranged/Œsnap to grid/Œarranged by name/Œarranged by modification date/Œarranged by creation date/Œarranged by size/Œarranged by kind/Œarranged by label) : the property by which to keep icons arranged
icon size (integer) : the size of icons displayed in the icon view
shows item info (boolean) : additional info about an item displayed in icon view
shows icon preview (boolean) : displays a preview of the item in icon view
text size (integer) : the size of the text displayed in the icon view
label position (right/Œbottom) : the location of the label in reference to the icon
background picture (file) : the background picture of the icon view
background color (RGB color) : the background color of the icon view

So, you may try :

set theFolder to choose folder
tell application "Finder"
	open theFolder
	tell the front Finder window
		set current view to icon view  # It's the unique mode understanding "set arrangement."
		set iconViewOptions to its icon view options
		set arrangement of iconViewOptions to arranged by name --modification date
	end tell
	close folder theFolder
	open folder theFolder
end tell

If my memory is not too odd, there is a neater way to force the change to apply but it’s sufficiently odd to refuse to tell me what it is.

Yvan KOENIG (VALLAURIS, France) samedi 27 juin 2015 16:08:30

Thanks Ivan for your reply.
When I run your script the only thing it does is let my choose the folder and then it opens it in icon view, but ignores the part of “arrange by name” (it’s arranged by kind).
I changed the letter icon to list (I wanted mine to be opened in list view).
set theFolder to choose folder

tell application “Finder”
open theFolder
tell the front Finder window
set current view to list view
set listViewOptions to its list view options
set arrangement of listViewOptions to arranged by name --modification date
end tell
end tell

It indeed opens the finder in list view but then it still ignores the the part of “arrange by name” (it’s also arranged by kind) and gives me the error:

“error “Finder got an error: Can’t set arrangement of list view options of Finder window id 1095 to arranged by name.” number -10006 from arrangement of list view options of Finder window id 1095”

Model: 15" MBPr late 2013
AppleScript: 2.7
Browser: Chrome/Safari
Operating System: Mac OS X (10.10)

An application’s dictionary will tell you what it can do under AppleScript.
To read them: while in Script Editor, open the dictionary palette - Window> Library, or cmd-shift-L.
If your app is not in the list you can add it by clicking the + button.
If you cannot add it, it is not scriptable.

It would appear that, with list view options, arranged by is not an option.

(1) Here, the script which I posted does exactly what it is supposed to achieve and it’s supposed to do the same if you run it with no change.

(2) The dictionary is clear : there is no “arrange” option available when the view mode is not “icon view”.

(3) As far as I know, if you wish to sort by name when you apply list view, you must use gui scripting.
Here is a script doing the job :

set theFolder to choose folder
--set thefolder to (path to desktop as text) & "atelier scripts:" as alias

tell application "Finder"
	open theFolder
	tell the front Finder window
		set current view to list view
	end tell
	set Name_loc to localized string "N220" # Fine, don't change with 10.11
end tell

tell application "System Events" to tell process "Finder"
	set frontmost to true
	set wName to name of window 1
	if not (exists (first window whose (name is wName and subrole is "AXSystemFloatingWindow"))) then
		keystroke "j" using {command down}
		repeat
			if exists (first window whose (name is wName and subrole is "AXSystemFloatingWindow")) then exit repeat
			delay 0.1
		end repeat
	end if
	tell (first window whose (name is wName and subrole is "AXSystemFloatingWindow"))
		set thePopUp to pop up button 2
		click thePopUp
		click menu item Name_loc of menu 1 of thePopUp
	end tell
end tell

Yvan KOENIG (VALLAURIS, France) dimanche 28 juin 2015 13:01:56

Thanks Ivan.
I tried the 2nd script you wrote.
Since this is a script that I am trying to integrate in a KM (Keyboard Maestro) macro and the 2nd script needs more actions, I would prefer to keep it simple and stick to the 1st one you wrote.

The 1st script works except the line “set arrangement of iconViewOptions to arranged by name”. This line doesn’t change the “Arrange by”.
Maybe there is something with the syntax that needs to be changed?
(Sorry, I tried to find a way but. too limited knowledge) :).
If we succeed to make this line work and we get the arranged by name, then the line after that should by something like “set current view to list view” and then the arranged by should still stay as by name. (I think) :slight_smile:

Thanks alastor for the tip!

I repeat that the first version is unable to set the arrangement in list mode because this feature isn’t available.
This is why I wrote the GUI Scripting code which, as far as I know ,is the only way to rule the arrangement when we use list mode.

Yvan KOENIG (VALLAURIS, France) mardi 30 juin 2015 09:41:49

Hi Ivan.
Sorry, maybe I did not explain myself properly.

“I repeat that the first version is unable to set the arrangement in list mode because this feature isn’t available.”
That is clear to me now, I understood.

But what I am saying is, that if we succeeded to get your script working and set the finder window in icon view arranged by name, then all we need to do is to add a line that will show now the same window in list view and the arranged by name will be still showing, so we got eventually the finder window in list view, arranged by name.
Hope this time is more clear and hope there is a way to make your script work and make it show the window in icon view arranged by name.
Thanks for your time and understanding (and patience).
BD

I apologize but the application is not supposed to take care of your dreams.

You wrote : “set the finder window in icon view arranged by name, then all we need to do is to add a line that will show now the same window in list view and the arranged by name will be still showing,”
but your dreams don’t come true.

The folder targeted by the script below was set to list view arranged by Size.
After running the script it was always having the original settings.

set theFolder to "Macintosh HD:Users:Important:pour ebay:" as alias
tell application "Finder"
	open theFolder
	tell the front Finder window
		set current view to icon view # It's the unique mode understanding "set arrangement."
		set iconViewOptions to its icon view options
		set arrangement of iconViewOptions to arranged by name --modification date
	end tell
	close folder theFolder
	open theFolder
	tell the front Finder window
		set current view to list view
	end tell
	close folder theFolder
	open theFolder
end tell

I also tried with :

set theFolder to "Macintosh HD:Users:Important:pour ebay:" as alias
tell application "Finder"
	open theFolder
	tell the front Finder window
		set current view to icon view # It's the unique mode understanding "set arrangement."
		set iconViewOptions to its icon view options
		set arrangement of iconViewOptions to arranged by name --modification date
	end tell
	close folder theFolder
	tell me to delay 0.1
	open theFolder
	tell the front Finder window
		set current view to list view
	end tell
	close folder theFolder
	open theFolder
end tell

with the same behavior.

This is why I wrote : i As far as I know, if you wish to sort by name when you apply list view, you must use gui scripting.[/i]

Yvan KOENIG (VALLAURIS, France) mercredi 1 juillet 2015 11:45:17

PS. It would be fair to stop editing my first name according to your taste. Since 1943/12/31 it’s Yvan so I am accustomed to it.

Hello Yvan (and apologies for my brain interpretation of your name, won’t happen again) :slight_smile:

Thanks again for trying to help me here. I appreciate it very much!

Let’s step back a bit. There is a miscommunication between us: I am trying to tell you that the “arrange by” in the script doesn’t work.

I tried what you wrote (copied and pasted):

set theFolder to “Users:MyName:My Folder:Misc:Ksafim:Taxes:” as alias
tell application “Finder”
open theFolder
tell the front Finder window
set current view to icon view # It’s the unique mode understanding “set arrangement.”
set iconViewOptions to its icon view options
set arrangement of iconViewOptions to arranged by name --modification date
end tell
end tel

And it doesn’t work.
It opens theFolder in icon view but the “arranged by” doesn’t work. It doesn’t matter what I write “name”, kind, size, whatever the arrangement stays “none”

I have no idea why. I guess the script works fine in your computer. If so I cannot of a reason why the arrange by doesn’t work in mine.
If we succeed to find the reason, then we can make some progress.

The script works flawlessly when you don’t remove instructions.
Back to the first version. I commented the required instructions which you dropped.

set theFolder to choose folder
tell application "Finder"
	open theFolder
	tell the front Finder window
		set current view to icon view # It's the unique mode understanding "set arrangement."
		set iconViewOptions to its icon view options
		set arrangement of iconViewOptions to arranged by name --modification date
	end tell
	close folder theFolder # THESE TWO INSTRUCTIONS ARE REQUIRED
	open folder theFolder # TO FORCE THE FINDER TO APPLY THE CHANGE
end tell

I carefully wrote : If my memory is not too odd, there is a neater way to force the change to apply but it’s sufficiently odd to refuse to tell me what it is.

Yvan KOENIG (VALLAURIS, France) vendredi 3 juillet 2015 09:13:11

Hey Yvan,

I think this is what you were after:


set otherFolder to path to favorites folder

tell application "Finder"
	set currentFolder to insertion location as alias
	
	# Make your changes to the window.
	
	set target of front window to otherFolder
	set target of front window to currentFolder
	
end tell

Thanks.

It’s exactly that. Maybe I read it once in one of your messages.

I will store the code in the rtf file in which I store numerous pieces of code.
Yes, in a rtf file because once the AppleScript file in which I stored them become unreadable. Happily, I had a backup but I decided to work with belt and straps.

Yvan KOENIG (VALLAURIS, France) vendredi 3 juillet 2015 15:52:39

Hello Yvan.
Apparently I had some kind of problem that it wasn’t working for me. Somehow I tried some different settings and now it is.
Your code does exactly what I needed.
So thanks a lot for your help and you patience.
Have a great day.
BD