Select file using type for file extesion

I do have the following problem:
I want to restrict the choose file available type to CSV only, so you can only choose a CSV file. I’ve already tried this:

set aFile to choose file with prompt “Kies om te zetten bestand:” default location alias aFolderDownload of type {“public.csv”} without invisibles

Ho can help me?

Hi,

CSV files are plain text files. There is no unique file type or type identifier for CSV files

Hello

It seems that no program installs the official UTI dedicated to CSV file in the system’s database so, when we ask the properties of such a file, we don’t get 'public.csv" but something like:

type identifier:“dyn.age80g650”

A workaround was given in december 2008 by Daniel Varlet.

Save your script as an application bundle.

ctrl-click to ‘show package’s contents’

open the info.plist file and edit it to match this one:

<?xml version="1.0" encoding="UTF-8"?> CFBundleAllowMixedLocalizations CFBundleDevelopmentRegion English CFBundleExecutable applet CFBundleIconFile applet CFBundleInfoDictionaryVersion 6.0 CFBundleName install_csv_UTI CFBundlePackageType APPL CFBundleSignature aplt LSRequiresCarbon UTImportedTypeDeclarations UTTypeConformsTo public.composite-content public.data UTTypeDescription CSV family UTTypeIconName Document.icns UTTypeIdentifier public.csv UTTypeTagSpecification com.apple.macos.ostype CSV public.filename-extension csv WindowState name ScriptWindowState positionOfDivider 274 savedFrame 48 586 439 460 0 0 1920 1178 selectedTabView result

Save the info.plist
Close the script’s window.
It may be useful to move it from a folder to an other one to force the lazy Finder to take care of the info.plist.

Run the script.

The filter will be active and will continue to be active if you save the script under an other name and execute it.
This is why I named the beast install_csv_UTI .

Yvan KOENIG (from FRANCE vendredi 20 février 2009 18:56:55)

Hi Ivan,

About 1 year ago you helped me with the attached code for selecting only *.csv files in my script. After upgrading to Snow Leopard it stopped working. Do you have a tip for me to solve this problem?

Regards,
Paul

Not so elegant, but you could use a repeat loop :confused: :

set aFile to missing value
set myPrompt to "Kies het *.CSV bestand"
repeat while aFile is missing value
	set aFile to (choose file with prompt myPrompt)
	tell application "Finder" to if name extension of aFile is not "csv" then
		set aFile to missing value
		set myPrompt to "Alleen bestanden met de CSV extensie zijn toegelaten"
	end if
end repeat
get aFile

Or:

set myPrompt to "Kies het *.CSV bestand"
repeat
	set aFile to (choose file with prompt myPrompt)
	tell application "Finder" to if name extension of aFile is not "csv" then
		set myPrompt to "Alleen bestanden met de CSV extensie zijn toegelaten"
	else
		exit repeat
	end if
end repeat
get aFile

You can even put it in a handler:

set csvFile to chooseFileOfType("csv")

on chooseFileOfType(aFileType)
	set myPrompt to "Kies het *." & aFileType & " bestand"
	repeat
		set aFile to (choose file with prompt myPrompt)
		tell application "Finder" to if name extension of aFile is not aFileType then
			set myPrompt to "Alleen bestanden met de " & aFileType & " extensie zijn toegelaten"
		else
			exit repeat
		end if
	end repeat
	
	return aFile
end chooseFileOfType

Or using recursion:

set csvFile to chooseFileOfType("csv", false)

on chooseFileOfType(aFileType, allreadyCalledBOOL)
	if allreadyCalledBOOL is false then set aFile to choose file with prompt "Kies een *." & aFileType & " bestand"
	if allreadyCalledBOOL is true then set aFile to choose file with prompt "Alleen bestanden met de " & aFileType & " extensie zijn toegelaten"
	
	tell application "Finder" to if name extension of aFile is not aFileType then tell me to chooseFileOfType(aFileType, true)
	
	return aFile
end chooseFileOfType

Hope it helps,
ief2