File extensions & Displayed names
File extensions & Displayed names
- Subject: File extensions & Displayed names
- From: Mr Tea <email@hidden>
- Date: Tue, 5 Feb 2002 13:34:21 +0000
OS X was supposed to ease the heart-ache of dealing with dos-style file
extensions on the Mac, or so I thought, but applescript reveals only a
murky and indistinct mire of complexity.
A recent question about 'renaming files in Finder' threw me neck-deep
into this mire when I tried to tackle it.
First up, appending something to the name of a file with a line like this:
set name of theFile to "Prefix " & (name of theFile)
..made any hidden file extensions visible. So, "URL testfile" would
become "Prefix URL testfile.url", and "Clipping testfile" would expand to
"Prefix Clipping testfile.textClipping" (must have been late on Friday
afternoon when the extension allocation team came up with that one).
The immediate solution was to change the script to:
set name of theFile to "Prefix " & (displayed name of theFile)
..but that wipes out the hidden extension if there was one, which is not
ideal.
All of which got me wondering what would happen if I wanted to add a
suffix. Here's what happens:
set name of theFile to (name of theFile) & " suffix"
..puts the suffix after the extension, thereby disabling it. Same
happens using 'displayed name of the file' if the extension is not
hidden, and if it is hidden, it is lost, as in the prefix example.
This makes renaming files with applescript an onerously complex business
where any kind of name extension is involved. Preserving name extensions
and retaining their hidden or unhidden state would seem to require
something like this:
on open {x}
tell application "Finder"
activate
set theExtn to name extension of x as string
if extension hidden of x is true then
set hideExt to true
else
set extension hidden of x to true
set hideExt to false
end if
if theExtn is not "" then
set name of x to (displayed name of x & " suffix" & "." & theExtn)
else
set name of x to (name of x & " suffix")
end if
if hideExt then set extension hidden of x to true
end tell
end open
Preposterous is the word that springs to mind. And sluggish too. Can
anyone suggest a faster and significantly more concise way to achieve the
same thing?
Mr Tea
--
"Always remember to hide the pot."