How do I restrict the type of file I can select using:
choose file of type
I want to just be able to select text files .txt
How do I restrict the type of file I can select using:
choose file of type
I want to just be able to select text files .txt
choose file of type {"Txt","Text"}
I tried choose file of type {“mp3”, “mov”} and had no luck What am I doing wrong?
Lambo:
I am also having difficulties. According to the dictionary entry (Standard Additions):
choose file‚v : Choose a file on a disk or server
choose file
[with prompt string] : the prompt to be displayed in the dialog box
[of type list of string] : a list of file types or type identifiers. Only files of the specified types will be selectable.
And from the Finder dictionary:
file‚n [inh. item] : every file
elements
contained by application, containers, disks, folders, desktop-objects, trash-objects.
properties
file type (type class) : the OSType identifying the type of data contained in the item
So, I whipped this baby together:
set a to choose file
tell application "Finder" to get a's file type
Every file I choose results in missing value, so I am thinking the OSType thing is non-functional.
yep, missing value for me too. Good thinking on your part though.
In OSX, many applications don’t give text files file types. A workaround is to use the name extension to get files and use the ‘choose from list’ listing files with a certain name extension.
set f to choose folder
tell application “Finder”
set file_list to name of every file of f whose name extension is “txt”
end tell
set user_list to choose from list file_list with multiple selections allowed
Note that some files may not have extensions either, so you may want to look at both file type and name extension.
ignoring case
set file_list to every file of f whose name extension is “txt” or file type is “text”
end ignoring
I forget if ignoring case is default in this situation, so it may not be necessary.
gl,
Some where along the way Apple made a change to the Finder’s Applescript functions that invoive file types. At one time, using “txt” or “TEXT” was sufficient. Just today I had to rewrite a script that worked a year ago, so it has been sometime in the last 12 months, probably the 10.4.9 update.
Now, in order to find text files you need to use “public.plain-text” to make the files selectable in choose file.
This is also true of other files, not just text files. Here is the list of types I had to use to write an image converter using Image Events:
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", ¬
"PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}
Apparently Apple is ditching the old Mac file type codes and will be using Java style codes (they are like reversed URLs, kind of). They usually start with either “com” for commercial software, “org” or “public” for public domain stuff. Then the company or organization that created the type is next, and then the specific type. The text type “public.plain-text” is an exception, since no organization “create” text file types.
That’s the 4-byte “type code” which was the way of determining file types in Mac OS 9 and earlier. In Mac OS X, it’s use has become more uncommon.
I hadn’t realized that! That’s good to know.
That’s a Uniform Type Identifier.
I don’t mind the new type identifiers so much, but I wish someone would tell us when the old ones quit working.
Hello
The ‘old file type’ is always usable . when the application which created the doc gave it a file type.
Many ‘modern applications’ don’t do that so the file type of their files is:
ASCII character (0) + ASCII character (0) + ASCII character (0) + ASCII character (0)
Yvan KOENIG (from FRANCE mercredi 25 avril 2007 07:18:28)
For those that are interested, here’s the main Cocoa document that explains Uniform Type Identifiers.
Hopefully in Leopard they will set it up so that it converts from one type to another (UTI to old XXXX to .xxx).
Jumping forward to 2018, here is a pretty good tool for deciphering UTI’s on the fly. You could easily adapt it to only return the UTI’s themselves but this script asks the user to choose samples of files, then returns a list of all files which share the sample’s UTI.
Be careful not to select two files with the same extension or the result will contain redundancies. Selecting a sample with the extension “.mp4” returned not only every mp4 on my drives, but also every file with the “.m4v” or “.MP4” extensions!
Here is an example of the output after selecting both a movie and a sound file:
/Volumes/External1TBACKUP/Movies/thenobelprize/video.mp4
/Volumes/External1TBACKUP/Movs/thenobelprize/video.1.mp4
set samplFylsLST to (choose file with prompt "Command-click to select multiple sample files to get a list of UTI's." with multiple selections allowed)
tell application "System Events"
set fylsWithSameUTI to ""
repeat with samplfyl in samplFylsLST
set UTI to type identifier of samplfyl
set fylsWithSameUTI to fylsWithSameUTI & return & ¬
"--------------------------------------------------------" & return & ¬
"Here is a list of files matching UTI '" & UTI & "':" & return & ¬
"--------------------------------------------------------" & return
set fylsWithSameUTI to fylsWithSameUTI & (do shell script "mdfind kMDItemContentTypeTree == '" & UTI & "'") & return
end repeat
return fylsWithSameUTI
end tell
Model: Mac Pro, Yosemite
AppleScript: 2.7
Browser: Safari 601.2.7
Operating System: macOS 10.14