Getting the type code of a file under OSX
Eric Chatonet
eric.chatonet at sosmartsoftware.com
Sat Jul 30 10:24:10 EDT 2005
Hi Alex,
I should not have thought that such a tiny problem could go so far :-)
Error management is the key of good software...
So, I propose another approach:
As usual, is this one the last one?
:-)
function CreatorAndType pFilePath
local tDefaultFolder,tFile,tCreatorType
-----
if the platform <> "MacOs" then return "Error: filetype not
supported"
put the defaultFolder into tDefaultFolder
set the itemdel to slash
set the defaultFolder to item 1 to -2 of pFilePath
put the detailed files into tFile
filter tFile with urlEncode(item -1 of pFilePath) & ",*"
set the defaultFolder to tDefaultFolder
set the itemDel to comma
if last char of tFile = comma then return "Error: could not
determine the filetype"
return char 1 to 4 of item -1 of tFile,char 5 to 8 of item -1 of
tFile
end CreatorAndType
Le 30 juil. 05 à 15:06, Alex Tweedly a écrit :
> Eric Chatonet wrote:
>
>
>> Hi Dave,
>>
>> You are perfectly right: I missed this one.
>> So a reliable function could be:
>>
>>
> getting close ... but not quite there yet, because of the issue of
> comma being an allowed character in file names.
> (I don't know if this applies to Mac - but I'm 99% sure it will,
> since this is a relatively common thing to do in Unix, so I'd
> expect the OSX file system to allow it).
>
> If the Mac doesn't allow this, then your function is OK - but
> beware of the problem for any other operation you do on "the
> detailed files".
> If Mac does allow commas in file names, then it causes two problems.
>
> The first is easy - but rather dangerous. You cannot use "item 11"
> for the file creator - you should alway use "item -1". The same
> applies to all the items returned - you should use "item -10" for
> file size, rather than "item 2", etc.
>
> However, this doesn't work as it should on Windows.
> The docs say
>
>
>> * The file's access permissions
>> * The file's creator and file type (Mac OS and OS X only)
>> Any attribute that is not supported on the current system is
>> reported as "0" (zero).
>>
>>
> But in fact on Windows, the creator and file type (not supported)
> are set to empty, not "0".
> This is a real pain, because it means that you always have an empty
> last item - so the number of items is 10.
> [Does the Mac ever return empty Creator and file type ?]
>
> Therefore you need to add a check for the last character being
> comma, and if it is append some safe string (e.g. a space), before
> using the negative index numbers.
>
> The second problem is rather more obscure - but if we want to claim
> to work in 100% of the cases, not 99.99..% we should cover it.
>
> I believe you cannot use a "filter" to match any arbitrary filename
> you might be given.
>
> filter ... pFileName & ",*"
> fails because "name" and "name,date" both match pFileName = "name"
> You might think you could depend on the fact that the files are
> alphabetic to always get the one you want first (I certainly did
> when I started writing this email), but you can't. Using the
> filename "a,11" will falsely match the file "a" if it happens to be
> 11 bytes long (or 110, or 11234, ...). Since "a" comes before "a,
> 11" it will be the first one found.
>
> You could try (I did !!) using the trick I saw from Richard a
> couple of weeks ago, using a complex filter expression to ensure
> the right number of items - e.g.
> filter ... pFileName & ",*,*,*,*,*,*,*,*,*,*" but that also
> fails the "a,11" case as above.
>
> So I think you need to loop through them, checking the file name.
> But you cannot do as Dave suggested, and compare item 1 of each
> line against the file name, because that would fail to match a
> filename containing a comma that should match.
> You instead need to compare "item 1 to -11" (once you've done the
> above check for empty last item).
>
> So we finish up with what I *think* is a truly reliable version
>
> function CreatorAndType pFilePath
> local tDefaultFolder,tFile,tCreatorType, theLine, L, tNewFiles, tName
> -----
> if the platform <> "MacOs" then return "Error: filetype not
> supported"
> put the defaultFolder into tDefaultFolder
> set the itemdel to slash
> set the defaultFolder to item 1 to -2 of pFilePath
> put urlDecode(the detailed files) into tFile
> put empty into tNewFiles
> put item -1 of pFilePath into tName
> set the itemDel to comma
> repeat for each line L in tFile
> put L into theLine
> if the last char of theLine = comma then put space after theLine
> if item 1 to -11 of theLine = tName then put L & cr after tNewFiles
> end repeat
> put tNewFiles into tFile
> -- put tFile & cr after msg
> if tFile = empty then return "Error: could not find file"
> set the defaultFolder to tDefaultFolder
> return tFile
> end CreatorAndType
>
>
>
>> Le 30 juil. 05 à 13:07, Dave Cragg a écrit :
>>
>>
>>> On 30 Jul 2005, at 10:40, Eric Chatonet wrote:
>>>
>>>
>>>
>>>> Hi Brian,
>>>>
>>>> As the files function returns a list sorted by alphabetical
>>>> order, I don't think it's necessary.
>>>>
>>>>
>>>
>>>
>>> But, for example, if you were looking for a file named
>>> "mend.png" and there was a file named "amend.png" in the folder,
>>> you'd find "amend.png".
>>>
>>> A bit cumbersome, but when I've done this, I've repeated through
>>> all lines in the detailed files and done a match on the first
>>> item in each.
>>>
>>> Cheers
>>> dave
>>>
>>
>>
>>
>> Best Regards from Paris,
>>
>> Eric Chatonet.
>> ----------------------------------------------------------------
>> So Smart Software
>>
>> For institutions, companies and associations
>> Built-to-order applications: management, multimedia, internet, etc.
>> Windows, Mac OS and Linux... With the French touch
>>
>> Free plugins and tutorials on my website
>> ----------------------------------------------------------------
>> Web site http://www.sosmartsoftware.com/
>> Email eric.chatonet at sosmartsoftware.com/
>> Phone 33 (0)1 43 31 77 62
>> Mobile 33 (0)6 20 74 50 86
>> ----------------------------------------------------------------
>>
>> _______________________________________________
>> use-revolution mailing list
>> use-revolution at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-revolution
>>
>>
>>
>
>
> --
> Alex Tweedly http://www.tweedly.net
>
>
>
> --
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.338 / Virus Database: 267.9.6/59 - Release Date:
> 27/07/2005
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>
>
More information about the use-livecode
mailing list