capturing user selection of a file

Hello,

I can’t figure out how to present the user with an existing folder in finder. Then let the user select one of the files and get the selection.

any thoughts

Kevin

Two simple alternatives to specify the folder:

-- if you know in advance
set exFolder to ((path to desktop as text) & "someFolder") as alias

-- if user needs to choose
set exFolder to choose folder default location (path to desktop)

choose file default location exFolder

Both choose file and choose folder have options (in addition to default location) so it might be worth looking them up in the Language Guide. If you don’t set the default location, then it becomes the Documents folder.

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-SW4

thanks for the tips:

here is what I have so far

set database_folder to “Macintosh HD:Users:kevin:Documents:allyn database stuff:”
–I am manually entering the database_folder for testing purposes

set SoundFolder to database_folder & “savedaudio:” as alias

try
set thesoundFile to choose file with prompt “please select the audio file you wish to use” of type “m4a” default location SoundFolder
set nothingwentwrong to true
on error number -128
set thesoundFile to “user canceled”
–capture if user canceled (error number -128)
end try

the script returns this error

error “Can’t make alias "Macintosh HD:Users:kevin:Documents:allyn database stuff:savedaudio:Frankalide1747.m4a" into type string.” number -1700 from alias “Macintosh HD:Users:kevin:Documents:allyn database stuff:savedaudio:Frankalide1747.m4a” to string

what I need is just the “Frankalide1747.m4a” part of the thesoundFile

Not sure about the error as I don’t get it. I’m running Sierra… don’t know if that makes a difference. But that error suggests a parameter problem. And I don’t see any code that is trying to coerce the resulting alias into text (although perhaps it is the ‘set soundFolder to database…’ line). When you’re trying to figure out errors, remove the ‘try’ statements.

This will get the name of the chosen file:

tell application "Finder" to set soundFileName to name of thesoundFile

Or, you might be able to get away with:

set soundFileName to get name of (info for of thesoundFile)

On a separate note, while it does work here, the ‘type’ parameter generally implies a Uniform Type Identifier (UTI), in this case: com.apple.m4a-audio. You can retrieve this with ‘info for’ the file in question, or by running ‘mdls’ on the file with the Terminal. Per documentation, the four-character file type code ("M4A ") works but this form has been deprecated.

thank you for the great information.

would you recommend that I use the -of type " com.apple.m4a-audio in the script
to help future proof it?

In the great scheme of things, I suspect that it’s a non-issue but in general, it’s better to use the correct form than the deprecated form. Maybe nothing ever happens but if something does, it’s more likely to fail that way. Also, I believe that going through the process helps understand how things work and where to look for stuff when you want to do other things.