sorting volumes "Can’t make «handler sort_list» into type reference.

I’m trying to get a list of mounted volumes and then search for the position of one in the list
I’m using

set discdrivename to "Nautilus HD"
set disk_names to (list disks)
set the_list to disk_names
my sort_list(the_list)
set item_num to my list_position(discdrivename, sort_list)
return item_num
on list_position(discdrivename, sort_list)
	repeat with i from 1 to the count of sort_list
		if item i of sort_list is discdrivename then return i
	end repeat
	return 0
end list_position
on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {"" & the_list, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort")
end sort_list

which gives me an error of

but

set discdrivename to "Nautilus HD"
set disk_names to (list disks)
set the_list to disk_names
my sort_list(the_list)
set sort_list to {"Nautilus HD", "vesiPod", "puddledrive"}
set item_num to my list_position(discdrivename, sort_list)
return item_num
on list_position(discdrivename, sort_list)
	repeat with i from 1 to the count of sort_list
		if item i of sort_list is discdrivename then return i
	end repeat
	return 0
end list_position
on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {"" & the_list, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort")
end sort_list

does not when I manually type in the result into sort_list from what I get in the result to my sort_list in this

set disk_names to (list disks)
set the_list to disk_names
my sort_list(the_list)
on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {"" & the_list, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort")
end sort_list

How do I get the first script to work and return the proper number?

Hi,

the problem is, you don’t save the result of the sorting.
A variable and a handler with the same name are not the same object


.
set sort_list to sort_list(the_list)
.

the my keyword is only necessary if the handler call is within an application tell block.

I don’t get the purpose of the script.
To determine a particular disk you can use


tell application "System Events"
	set myDisk to disk "Nautilus HD"
end tell

Yes they are! It’s the handler call that’s not the same thing. :slight_smile:

on sort_list()
	return {1, 2, 3}
end sort_list

{sort_list, sort_list()}
--> {«handler sort_list», {1, 2, 3}}

Using a variable with the same name as a handler will make the script lose touch with the handler:

on sort_list()
	return {1, 2, 3}
end sort_list

set sort_list to sort_list()
sort_list()
--> Error: «script» doesn't understand the sort_list message.

I was trying to find the number of which the disk drive is in the list of mounted volumes

set disk_names to (list disks)
set the_list to disk_names
my sort_list(the_list)
on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {"" & the_list, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort")
end sort_list

returns a list of the volumes and I wanted to compare that to
the disk drive volume

tell application "System Events"
	1st disk whose ejectable is true and free space is 0
	end tell

to find what number it is in the list since they are alphabetized according to their names and the disk drive changes according to the first letter in the media name.

sorry, I don’t get the purpose of the script
What is the next step after the comparison using the index number?

You can get all properties from the disk element e.g. the name


tell application "System Events"
	try
		set diskName to name of (1st disk whose ejectable is true and free space is 0)
	on error
		set diskName to ""
	end try
end tell

This does indeed return an alphabetically sorted copy of the disk-name list. However, the first script in your first post ignores the returned result and instead passes the ‘sort_list’ handler itself to the ‘list_position’ handler. That’s why you get the error: “Can’t make «handler sort_list» into type reference.”

The second script in your first post reassigns the ‘sort_list’ variable to your hard-coded list instead of to the handler, so the ‘list_postion’ handler then works. However, as I pointed out in my own post above, you can’t then use the sort handler again because its name now means something else and the script won’t be able to find it. Basically, you shouldn’t use variables with the same names as handlers.

In this version of your first script, I’ve used a variable called ‘sorted_names’ to hold the sorted list:

set discdrivename to "Nautilus HD"
set disk_names to (list disks)
set sorted_names to sort_list(disk_names) -- Store the sort result in a variable with a unique name.

set item_num to my list_position(discdrivename, sorted_names) -- Use the unique name in the 'list_position' call.

on list_position(discdrivename, the_list) -- Don't use the 'sort_list' handler name here either.
	repeat with i from 1 to the count of the_list
		if item i of sort_list is discdrivename then return i
	end repeat
	return 0
end list_position

on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {"" & the_list, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort")
end sort_list

I was looking for the position of the volume to select it with keystrokes within and application since the application did not have applescript support

tell application "System Events"
	try
		set DiskName to name of (1st disk whose ejectable is true and free space is 0)
	on error
		set DiskName to ""
	end try
end tell

set disk_names to (list disks)
set sorted_names to sort_list(disk_names) -- Store the sort result in a variable with a unique name.

set item_num to my list_position(DiskName, sorted_names) -- Use the unique name in the 'list_position' call.
return sorted_names
set item_num1 to item_num - 1

tell application "anyprog"
	activate
	delay 2
	
	tell application "System Events" to keystroke (ASCII character 29) --> rigth arrow
	
	
	repeat with i from 1 to item_num1
		tell application "System Events" to keystroke (ASCII character 31) --> down arrow
		delay 2
	end repeat
	tell application "System Events" to keystroke return
end tell

on list_position(diskdrive, the_list) -- Don't use the 'sort_list' handler name here either.
	repeat with i from 1 to the count of the_list
		if item i of the_list is diskdrive then return i
	end repeat
	return 0
end list_position

on sort_list(the_list)
	set nl to (ASCII character 10)
	tell (a reference to my text item delimiters)
		set {old_delim, contents} to {contents, nl}
		set {the_list, contents} to {"" & the_list, old_delim}
	end tell
	return paragraphs of (do shell script "echo " & (quoted form of the_list) & " | sort")
end sort_list

was the code I was looking for, however it was not very consistent since there were more folders and volumes in the program’s browser than the computers.
I settled on this (callling the volume by the name discovered in the first part.

tell application "System Events"
	try
		set DiskName to name of (1st disk whose ejectable is true and free space is 0)
	on error
		set DiskName to ""
	end try
end tell
tell application "anyprog"
	activate
	delay 2
	tell application "System Events" to keystroke DiskName
	tell application "System Events" to keystroke return
end tell

As discovered in an other thread, this code is not reliable. Try my CLI discName.
It displayes the name of the inserted media in the optical drive (or the names linefeed separated if the disc has more than one partition).

Sorry, without knowing the application it is almost impossible to help with GUI scripting.