Choose File: Problems with Packages

(choose file) as string

When using the “choose file” command (shown above), if the file you choose is a package (numbers, pages, and probably all or most of the “i-apps”) the path returned will appear as a folder (ending with a colon “:”) rather than a file. This will cause an error if you later try to get a property of or manipulate the file.

To get around this problem, you can add a couple of lines of code to detect and remove the colon from the file path before proceeding further as follows:

set pathToFile to (choose file) as string
if pathToFile ends with ":" then set pathToFile to text 1 thru -2 of pathToFile as string

This problem exists in 10.5 and may also exist in previous versions of OS X.

Would you give an example?

Hi, Mark.

The trailing colon isn’t a bug, nor is it new in Leopard. Packages are containers, and coercions to text from container aliases have always ended with colons. choose file allows packages to be chosen because it’s a user-interaction command, and users are supposed to think of packages as files. It returns an alias and that’s the end of its involvement. The as string coercion that follows works from the alias, not knowing it came from choose file. It’s up to scripters to make the necessary allowances, but it’s not a bug.