Sunday, January 23, 2022

#1 2022-01-09 03:45:26 pm

Beeman
Member
Registered: 2006-03-07
Posts: 93

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

Offline

 

#2 2022-01-09 04:53:51 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 176

Re: capturing user selection of a file

Two simple alternatives to specify the folder:

Applescript:

-- 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/arc … -CH216-SW4

Offline

 

#3 2022-01-09 06:40:21 pm

Beeman
Member
Registered: 2006-03-07
Posts: 93

Re: capturing user selection of a file

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

Offline

 

#4 2022-01-09 07:51:25 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 176

Re: capturing user selection of a file

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:

Applescript:

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

Or, you might be able to get away with:

Applescript:

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.

Last edited by Mockman (2022-01-09 07:57:42 pm)

Offline

 

#5 2022-01-11 05:33:05 pm

Beeman
Member
Registered: 2006-03-07
Posts: 93

Re: capturing user selection of a file

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?

Offline

 

#6 2022-01-11 08:49:27 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 176

Re: capturing user selection of a file

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)