Error when displaying value chosen from list

Hi. I’m writing a program that imports some sound files. I’m able to get all the files to display properly and store the name of the selected list item to a variable (variable “desiredSound” in the code below), but when I try to display the value of “desiredSound” I’m presented with an AppleScript error that says: “AppleEvent handler failed. (-10000)”.

I’m quite confused! Any help would be greatly appreciated.


if name of theObject = "Button-ChooseSound" then
	tell application "System Events"
		if exists process "System Preferences" then
			quit application "System Preferences"
		end if
	end tell
	
	set SystemSoundsPath to alias ((path to library folder from system domain as Unicode text) & "Sounds:")
	set UserSoundsPath to alias ((path to library folder as Unicode text) & "Sounds:")
	
	tell application "Finder"
		set SystemSounds to name of files of SystemSoundsPath
		set UserSounds to name of files of UserSoundsPath
	end tell
	
	set allSounds to (SystemSounds & UserSounds)
	
	set desiredSound to (choose from list allSounds with title "Sounds" with prompt "Select a sound to import" OK button name "Select" without empty selection allowed)
	
	if desiredSound is false then
		display dialog "nothing selected"
	else
		-- error is displayed at the next line
		display dialog desiredSound
	end if
end if

Model: iMac 24" (aluminum)
Browser: Safari 525.13
Operating System: Mac OS X (10.5)

Hi, ntnwwnet.

That’s working for me. Is your code in a tell block that’s not shown in your post?

Another thing to remember is that if the choose from list result isn’t false, it’ll be a list containing the chosen text, not the text itself ” eg. {“MySound.aiff”}, not “MySound.aiff”. A single-item list should be automatically coercible to the item itself, but perhaps that’s not happening on your machine for some reason. You could try getting the item from the list explicitly:

set desiredSound to (choose from list allSounds with title "Sounds" with prompt "Select a sound to import" OK button name "Select" without empty selection allowed)

if desiredSound is false then
	display dialog "nothing selected"
else
	display dialog (item 1 of desiredSound)
end if

Note: without empty selection allowed is the default setting, so you can omit it

@Nigel:

That works perfectly, thank you.

However, is there a way to save the “(item 1 of desiredSound)” portion to a string?

It seems that I’ve answered my own question.

All I did was:

set desiredSound to (item 1 of (choose from list allSounds with title "Sounds" with prompt "Select a sound to import" OK button name "Select"))

Now, in the program the user can select any sound file type. Is there a way to strip off the file extension (another part of the program takes care of opening the proper file)?

For example, if the user clicks on “StartupSound.mp3” in the list, is there a way to only store “StartupSound” to the variable?

Ideally, you should check that the choose from list result isn’t false before attempting to get its item 1.

There are a couple of ways of removing the name extension. I personally like to use text item delimiters:

set allSounds to {"StartupSound.mp3"}
set theChoice to (choose from list allSounds with title "Sounds" with prompt "Select a sound to import" OK button name "Select")
if (theChoice is false) then error number -128 -- Stop the script

set desiredSound to item 1 of theChoice
if (desiredSound contains ".") then
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set desiredSound to text 1 thru text item -2 of desiredSound
	set AppleScript's text item delimiters to astid
end if
-- display dialog desiredSound

@Nigel:

Thank you very much for all of your help. My program works perfectly!

Model: iMac 24" (aluminum)
Browser: Safari 525.13
Operating System: Mac OS X (10.5)

@Nigel:

You showed me a way to remove the file extensions from an item selected from a list, but is there a way to display the “choose from list” dialog with the file extensions removed?

Hello

You must strip the extensions by yourself.

set liste1 to list allSounds
set liste2 to {}
repeat with s in liste1
	copy my stripExt(s) to end of liste2
end repeat
set desiredSound to choose from list liste2 with title "Sounds" with prompt "Select a sound to import" OK button name "Select"
if desiredSound is false then return
set desiredSound to item 1 of desiredSound

on stripExt(n)
	if n contains "." then
		set AppleScript's text item delimiters to "."
		set n to text 1 thru text item -2 of n
		set AppleScript's text item delimiters to ""
	end if
	return n
end stripExt

Yvan KOENIG (from FRANCE vendredi 18 avril 2008 14:10:27)