Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a way, via terminal, to change whether or not a specific file's extension is shown in the Finder, something along the lines of:

$ hideextension ~/music/somesong.mp3

Without having to open Get Info and change the checkbox, as it's massively tedious.

I plan on incorporating it into a script I'm calling via a shortcut using FastScripts. I'd like to try and stay away from GUI scripting as that feels unclean, although any ideas on how to accomplish this are welcome.

share|improve this question
    
If it makes any difference, I'm trying to accomplish this on Lion. –  joshua.thomas.bird Mar 12 '12 at 20:18

2 Answers 2

up vote 12 down vote accepted

The only real way to change this via GUI is to click Hide extension in the Finder Info window. Checking this changes the com.apple.FinderInfo extended attribute, which you normally can't edit – at least not easily. We can however use a tool to do it for us.

For the below to work, you obviously need to have Show all file extensions unchecked in Finder's preferences.


The SetFile command

If you have Xcode installed, you will get the SetFile(1) binary, which does exactly what you want (and offers a few more functions related to file attributes):

Hide extension:

SetFile -a E <file>

Show extension again:

SetFile -a e <file>

Through AppleScript

AppleScript offers the same functionality with the set extension hidden command. You obviously need an alias to a file object. We can get that, for example, though a dialog. Here's just a minimal working example.

tell application "Finder"
    set some_file to (choose file)
    set extension hidden of some_file to true
end tell

To reverse, just exchange true with false here. The full call is then, for example:

set extension hidden of alias "Macintosh HD:Users:werner:Desktop:file.png" to true

You can run this straight from a script file too (thanks @DanielBeck for the addition):

on run argv
tell application "Finder" to set extension hidden of (POSIX file (first item of argv) as alias) to true
end run

Save this as filename.scpt and run it from the command line with:

osascript filename.scpt targetfile
share|improve this answer
    
Strictly speaking, it's an alias, not a file. Here's how to use the AppleScript from the command line: on run argv [newline] tell application "Finder" to set extension hidden of (POSIX file (first item of argv) as alias) to true [newline] end run, use as osascript filename.scpt targetfile. –  Daniel Beck Mar 12 '12 at 21:45
1  
You're right, of course. I added the full AppleScript event. In the future, just go ahead and add anything important to the answer – you're always welcome to. –  slhck Mar 12 '12 at 21:50
1  
just what I was looking for.. thankfully I have Xcode installed and SetFile did the trick :-) –  thandasoru Oct 2 '13 at 13:42

Thanks slhck for your Answer, it helped me a bunch to get what I wanted done.

So since I like shortcuts, I created a "Run Shell Script" Service though Automator.

for f in "$@"
do
    STATUS=`getFileInfo -ae "$f"
    if [ $STATUS== 0 ];
    then
        SetFile -a E "$f"
    else
        SetFile -a e "$f"
    fi
done

Then I went to Finder -> Services Preferences and added a shortcut to the Service.

 "Command + Shift + H" didn't work for me,
 "Command + H" hides the application
 so i chose "Command + Shift + E"

Hope it helps. =)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.