I’m new to the world of Mac and scripting, but loving it so far!!
I build a script where I want to select a file from the folder ‘Downloads’ where only the files with the extension .csv are selectable and the rest is grayed out. I think it has to work via chosing the right type in te commandline from the ‘choose file’ command. I tried to find which constant I should use but I’m lost. There are many predefined systemconstants starting with public.??? but I can’t find .csv. Trying to find out I got lost in the help via te script language guide in defining Uniform Type Identifiers. Is there no easier way to do this? If not is there a simpler way to prefilter selectable files?
You can do this by File Type, like "PDF " (and yes, there’s a space at the end of that) or by the UTI. The Kind does not appear to work, at least on Tiger.
Here’s a little quickie if you don’t know the File Type. Open Applications>Utilities>Terminal and type: mdls then a space and drop your .CSV file into it and press Return. Copy the kMDITemContentType from the window and paste it into your script. (I would put it in a list even if it’s one item so I could add more later without forgetting to make it a list. A list of one item is still a list.)Note: File Type and Creator Type are sometimes listed in the results but since Apple seems less inclined to require this, it’s less prevalent than it was (and less than it should be - they’re still incredibly useful ).
here’s a simple example:
choose file of type {"com.adobe.illustrator.ai-image"} default location (path to desktop) without invisibles
if the folder location is constant, I suggest this
-- assuming theFolder is an alias
tell application "Finder" to set csvFiles to name of files of theFolder whose name extension is "csv"
set theFile to choose from list csvFiles
if theFile is false then return
set theFile to alias ((theFolder as text) & item 1 of theFile)
--> result is the alias of the chosen file
Since this is a more unusual case, where the UTI is apparently always dynamically mapped by default, I agree with you. But as a general rule, I try and avoid using name extension as my first criteria since this can easily be spoofed. (This is one of those Windows-like behaviors I wish Apple had never implemented.).
I have a JPEG file which has no File Type or Creator Type (not unusual these days). It reports properly and LaunchServices behaves as expected. Now if I change the extension to .RTFD the icon changes, mdls reports it as an RTFD file as does the Finder i[/i], and LaunchServices decides that TextEdit should open it for me!
Just a thought. ahh, for the simpler days of File Types and Creator Codes and the extensionless magic of the Mac!