Hide extension without Finder possible ?

Hi everyone,

Is it somehow possible to hide a file its extension without the Finder ? maybe System Events or shells scripts?

Thanks

This may not be what you want since it uses the Finder, but heres how I do it…


tell application "Finder" to repeat with eachSelectedFile in selection as list
	set extension hidden of eachSelectedFile to not (extension hidden of eachSelectedFile)
	delay 0.2
	update eachSelectedFile
end repeat

No, this doesn’t do the trick :frowning: I knew how to do it with the Finder but it conflicts with some stuff in my script…

I tried

tell application "System Events"
	set extension hidden of theFile to true
end tell

and the error is something like “Can’t set extension hidden of to true”

I also tried

do shell script "mv " & posix file theFile & space & posix file (text 1 thru -5 of theFile)

Which moves the file to the same location but hides the extension. If I use that UNIX way, the Finder doesn’t know what kind of file it is anymore…

That last one does not hide the extension, it deletes the last four characters from the filename (which would be the extension only if the extension was exactly three characters). You really do not want to do that. It might seem to work for files that have a creator code set, but for others it will fail miserably (like you noticed).

At least on 10.4, extension hidden is not a part of System Events. In the context of System Events the term actually comes from StandardAdditions. It is only used as the label for a read-only property of the result of the info for command.

If you have the developer tools installed on the target machine, you might be able to use this:

set a to choose file
hideExtension(a)
--showExtension(a)

to hideExtension(fileref)
	setAttr(fileref, "E")
end hideExtension
to showExtension(fileref)
	setAttr(fileref, "e")
end showExtension
to setAttr(fileref, attrString)
	do shell script "/Developer/Tools/SetFile -a " & quoted form of attrString & " " & quoted form of POSIX path of fileref
end setAttr

If you are handy with Carbon (C) or Cocoa (Objective-C) (or know someone who is) you could write your own redistributable command-line tool that could do the same thing and call it in a similar way from AppleScript.

Thanks, it works :slight_smile:
unfortunately I do not know someone who knows Objective-c, and neither can I…

Well, then I can probably only fix it by searching for a solution of the conflict with the Finder… It’s a drop box what causes the long delay, there are some threads on this forum about it but most of them require alternatives for the actions done with Finder …

I wrote the following script for those who are interested in an AsObjC solution to this problem.

NOTE: if the SHOW ALL FILENAME EXTENSIONS option is set in the Finder preferences, hiding the file name extension is not possible at all. So, ensure the corresponding checkbox is off.
 

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

hideExtension(choose file) -- will be ignored if in the Finder preferences SHOW ALL FILENAME EXTENSIONS is on

on hideExtension(theAlias)
	set DM to current application's NSFileManager's defaultManager()
	set attrDict to current application's NSDictionary's dictionaryWithDictionary:{NSFileExtensionHidden:true}
	set {theResult, theError} to DM's setAttributes:attrDict ofItemAtPath:(POSIX path of theAlias) |error|:(reference)
end hideExtension

 

tell application "Finder" to set all name extensions showing of Finder preferences to false