Tuesday, December 10, 2019

#1 2019-11-14 08:14:49 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 235

Setting a variable to a property

The following works as expected:

Applescript:

tell application "Finder"
   set sortedFiles to sort selection by name
end tell

Why doesn't the following work?

Applescript:

tell application "Finder"
   set sortOrder to name
   set sortedFiles to sort selection by sortOrder
end tell

Also, I'd like to specify the sort order at the beginnng of the script outside the Finder tell block, as in the following. Is this possible?

Applescript:

set sortOrder to name

tell application "Finder"
   set sortedFiles to sort selection by sortOrder
end tell

I spent significant time googling this and researching the ALG but could find nothing. Thanks for the help.

Last edited by peavine (2019-11-14 08:17:04 am)


2018 Mac mini - macOS Catalina

Offline

 

#2 2019-11-14 08:29:08 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5118

Re: Setting a variable to a property

Hi peavine.

'name' is a term common to lots of things. When you use it in a script, it's evaluated in context. So in your second script, sortOrder gets set to "Finder", in the third it's set to the name of the script. The only way I can see to do what you want is to set sortOrder to a reference to 'name' in a Finder context. This sets the variable to the Finder's enum for 'name' instead of an actual name:

Applescript:

tell application "Finder" to set sortOrder to a reference to name

tell application "Finder"
   set sortedFiles to sort selection by sortOrder
end tell


NG

Offline

 

#3 2019-11-14 11:38:46 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 768

Re: Setting a variable to a property

Hi, Peavine.

Your question is answered, but as I see, you want to provide to user choose sort order method from list. To do that you should map sort properties to its names (string representation, as choose from list works with simple AppleScript values):

Applescript:


set aChoice to choose from list {"Name", "Kind", "Creation Date", "Modification Date", "Locked", "Name Extension", "Owner", "Group", "Size"} with prompt "Choose Method to Sort By"

tell application "Finder"
   if aChoice is "Name" then
       sort selection by name
   else if aChoice is "Kind" then
       sort selection by kind
   else if aChoice is "Creation Date" then
       sort selection by creation date
   else if aChoice is "Modification Date" then
       sort selection by modification date
   else if aChoice is "Locked" then
       sort selection by locked
   else if aChoice is "Name Extension" then
       sort selection by name extension
   else if aChoice is "Owner" then
       sort selection by owner
   else if aChoice is "Group" then
       sort selection by group
   else
       sort selection by size
   end if
end tell

NOTE: exists many other properties to sort items of selection. See ITEM properties in the Finder's dictionary.

Last edited by KniazidisR (2019-11-14 12:09:32 pm)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#4 2019-11-14 03:15:17 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 235

Re: Setting a variable to a property

Thanks Nigel and KniazidisR for the responses. The operator, "a reference to", is just what I was looking for.


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)