choose file not working

choose file of type {"scpt"}

does not let you select any scpt files, this works for most other types. I did try using type identifiers but they seem even worse at getting it wrong. When I use

choose file of type {"com.apple.iwork.pages.pages"}

it only allows selection of pages files that are of the old package format, even though

type identifier of (info for (choose file))

returns the same thing for both; using the type id for .txt files seems to allow more that .txt files to be selected :mad:. I mean is this just a plain old bug in OS X or am I missing something?

The file type for a compiled script has always been “osas”. The type identifier "com.apple.applescript’ also works for me.

Otherwise you’re right. There does seem to be some inconsistency. Using the name extension sometimes works, sometimes not. Similarly with the type identifier.

Just looking at it quickly, the technique seems to be: if ‘info for’ returns a file type other than “”, use that; otherwise try the name extension; and if that doesn’t work, try the type identifier. Not guaranteed to be foolproof, though.

Hello Nigel

info for is deprecated for years. So it seems that it’s not a good idea to use it in new scripts. Better use System Events to get the needed info.

Yvan KOENIG (VALLAURIS, France) lundi 29 août 2011 09:29:22

OK. Thanks, Yvan ” although in this case, the script would be just a test to see what to put in the new script. :wink:

-- No longer: set {file type:fileType, name extension:nameExtn} to (info for (choose file))

-- But:
tell application "System Events" to set {file type:fileType, name extension:nameExtn} to my (choose file) -- 'my' to prevent System Events from coming to the front to display the dialog.

if (fileType is missing value) then
	set possibility to nameExtn
else
	set possibility to fileType
end if

choose file of type {possibility} with prompt "Trying: 'choose file of type {\"" & possibility & "\"}'." -- Test.

Would it be possible to get this info without an existing file? Just a string for a theoretical extension?

I have this script to create files of a given type, then try to work out the correct way to find them in a choose file:

tell current application
	open for access file valfile with write permission
	close access file valfile
end tell
tell application "Finder"
	set valident to file type of (info for file valfile)
	tell me to log valident
	if valident is "TEXT" then
		set valident to type identifier of (info for file valfile)
		tell me to log valident
		if valident is "com.apple.traditional-mac-plain-text" then
			set valident to text 2 thru -1 of valnewtypenam
			tell me to log valident
		end if
	end if
end tell

Exactly as I was advised it checks for a good type, identifier and then if all else fails the extension, with a .bin it returned com.apple.macbinary-archive, for an icon file it returned the extension. I have a folder full of files created this way, I perhaps wrongly use it to test the results on.

choose file of type {"com.apple.macbinary-archive"}

works

choose file of type {"icon"}

does not allow any selection

choose file of type {"icon","com.apple.macbinary-archive"}

allows everything to be selected, help!

Files created with ‘open for access’ are of type “TEXT” unless you explicitly change their ‘file type’ properties afterwards.

The system appears to work from a combination of ‘file type’ and ‘name extension’. If you create a combination the system recognises, the file will (according to my experiments so far) be given a usable ‘type identifier’.

To get icon files I use

choose file of type {"com.apple.icns"}

and multiple file types works too

choose file of type {"com.apple.icns", "com.apple.application-bundle"}

What exactly is your problem?

OK bad example,

choose file of type {"com.apple.macbinary-archive"}

works

choose file of type {"ichgjghjon"}

does not allow any selection

choose file of type {"ichgjghjon","com.apple.macbinary-archive"}

allows everything to be selected

the main problem is I entered 2 types and nothing was restricted. Yes I entered a garbage name extension but my script needs to be able to handle any new file.

You could use lsregister command to look if a file type is registered in the launch services (this is where the com.apple.icns notation is for). Then when it is not registered you also don’t have to put it in the list of types.

If a user of my app requests a file type to be added than it must even if it doesn’t exist on the machine yet, and I don’t understand the command, I’m not sure how it can help me exactly. It’ not too important, it is working acceptably well.

lsregister can help you too lookup how the file type is defined. I use it for CSV files. When excel is installed it has another file type than machines who hasn’t installed excel. This is for me quite helpful determining which file type I have to use in the choose file command.