Show/hide extension of a file through OS X command line

Solution 1:

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.


Through AppleScript

AppleScript offers this 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

With the SetFile command

Note: This is deprecated since Xcode 6.

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>

Solution 2:

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. =)

Solution 3:

In order to have only one argument on the command line ($ hideextension ~/music/somesong.mp3), you can make your applescript become a shell script. It is possible to use osascript in the shebang (#!/usr/bin/osascript) like in the following code. To proceed :

  1. Test your applescript code in a .scpt file => toggle_hidden_extension.scpt
  2. When OK, add the shebang (#!/usr/bin/osascript) at the beginning of the file
  3. Export it with file format "text" => toggle_hidden_extension.applescript
  4. Change extension to .sh => toggle_hidden_extension.sh
  5. In Terminal, make it executable :

    chmod u+x toggle_hidden_extension.sh
    
  6. Now you can run it :

    ./toggle_hidden_extension.sh /path/to/myfile.mp3
    

So, the code to illustrate:

#!/usr/bin/osascript

(*
usage: toggle_hidden_extension.sh file
*)

(*
Test 1 : ./toggle_hidden_extension.sh /Users/boissonnfive/Desktop/file.txt
Test 2 : ./toggle_hidden_extension.sh
Test 3 : ./toggle_hidden_extension.sh 0fdjksl/,3
*)

on run argv
    try
        processArgs(argv)
        toggleHiddenExtension(item 1 of argv)
    on error
        return usage()
    end try

    if result then
        return "Extension hidden for " & POSIX path of (item 1 of argv)
    else
        return "Extension revealed for " & (POSIX path of (item 1 of argv))
    end if

end run


on usage()

    return "usage: toggle_hidden_extension.sh file"

end usage

on processArgs(myArgs)

    set item 1 of myArgs to POSIX file (first item of myArgs) as alias

end processArgs

on toggleHiddenExtension(myFile)

    tell application "Finder" to set extension hidden of myFile to not (extension hidden of myFile)

end toggleHiddenExtension