通过 OS X 命令行显示/隐藏文件的扩展名

jos*_*ird 24 file-management command-line file-extension macos

我正在寻找一种通过终端更改特定文件扩展名是否显示在 Finder 中的方法,大致如下:

$ hideextension ~/music/somesong.mp3
Run Code Online (Sandbox Code Playgroud)

无需打开获取信息并更改复选框,因为它非常乏味。

我计划将它合并到我使用 FastScripts 通过快捷方式调用的脚本中。我想尝试远离 GUI 脚本,因为这感觉不干净,尽管欢迎任何关于如何实现这一点的想法。

slh*_*hck 27

通过 GUI 更改此设置的唯一真正方法是单击Finder信息窗口中的隐藏扩展程序。检查此更改扩展属性,您通常无法编辑 - 至少不容易。但是,我们可以使用工具为我们做这件事。com.apple.FinderInfo

为了使下面的工作正常运行,您显然需要在 Finder 的首选项中取消选中显示所有文件扩展名


通过 AppleScript

AppleScript 通过set extension hidden命令提供此功能 。您显然需要alias一个文件对象。例如,我们可以通过对话框获得。这只是一个最小的工作示例。

tell application "Finder"
    set some_file to (choose file)
    set extension hidden of some_file to true
end tell
Run Code Online (Sandbox Code Playgroud)

要反转,只需truefalse此处交换即可。完整的调用是,例如:

set extension hidden of alias "Macintosh HD:Users:werner:Desktop:file.png" to true
Run Code Online (Sandbox Code Playgroud)

您也可以直接从脚本文件运行它(感谢@DanielBeck的添加):

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

将其另存为filename.scpt并从命令行运行它:

osascript filename.scpt targetfile
Run Code Online (Sandbox Code Playgroud)

随着SetFile命令

注意:自 Xcode 6 起已弃用。

如果您安装了 Xcode,您将获得SetFile(1)二进制文件,它完全符合您的要求(并提供更多与文件属性相关的功能):

隐藏扩展名:

SetFile -a E <file>
Run Code Online (Sandbox Code Playgroud)

再次显示扩展名:

SetFile -a e <file>
Run Code Online (Sandbox Code Playgroud)


Flu*_*ner 6

感谢slhck 的回答,它帮助我完成了我想做的事情。

因此,由于我喜欢快捷方式,因此我通过 Automator 创建了一个“运行 Shell 脚本”服务。

for f in "$@"
do
    STATUS=`getFileInfo -ae "$f"`
    if [ $STATUS== 0 ];
    then
        SetFile -a E "$f"
    else
        SetFile -a e "$f"
    fi
done
Run Code Online (Sandbox Code Playgroud)

然后我去 Finder -> Services Preferences 并添加了一个 Service 的快捷方式。

 "Command + Shift + H" didn't work for me,
 "Command + H" hides the application
 so i chose "Command + Shift + E"
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。=)