如何通过 OS X 中的终端更改特定文件类型的所有文件的默认应用程序?

yas*_*han 45 file-association macos

如何通过 OS X 中的终端更改特定文件类型的所有文件的默认应用程序?

mat*_*rns 58

我有一个更简单的方法。如果你还没有Homebrew,你会想要它:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Run Code Online (Sandbox Code Playgroud)

安装 duti:

brew install duti
Run Code Online (Sandbox Code Playgroud)

现在您需要找到您要使用的应用程序的 id,并将其分配给您要使用它的扩展程序。在这个例子中,我已经使用 Brackets for*.sh并且我还想将它用于*.md文件而不是 xcode。

获取.sh文件的默认应用程序 ID :

duti -x sh

output:
  Brackets.app
  /opt/homebrew-cask/Caskroom/brackets/1.6/Brackets.app
  io.brackets.appshell
Run Code Online (Sandbox Code Playgroud)

最后一行是id。

将此应用程序 ID 用于所有.md文件:

duti -s io.brackets.appshell .md all
Run Code Online (Sandbox Code Playgroud)

  • 如果您还没有通过这种方式将应用程序与某个应用程序相关联,但知道应用程序的名称,则可以执行 `osascript -e 'id of app "$appName"'` 来获取系统上安装的任何应用程序的 ID (5认同)
  • 如果可以的话,我会为此投票 50 次。很棒的信息。在 El Capitan 工作对我来说就像一种魅力。 (3认同)
  • 也在塞拉工作。额外提示:UTI 很麻烦,但您可以使用“mdls -name kMDItemContentType <file>”来查找给定文件(以及扩展名)的 UTI。 (2认同)
  • 仅供参考:将@GrayedFox 的提示与 duti 结合使用:`duti -s $(osascript -e 'id of app "Visual Studio Code"') .md all` (2认同)

Dan*_*eck 19

编辑~/Library/Preferences/com.apple.LaunchServices.plist

在 下添加一个条目LSHandlers,其中包含 UTI(密钥LSHandlerContentType,例如public.plain-text)和应用程序包标识符(LSHandlerRoleAll例如,com.macromates.textmate)。

属性列表编辑器中看起来像这样:

替代文字 替代文字

要从命令行执行此操作,请使用defaults/usr/libexec/PlistBuddy。两者都有大量的联机帮助页。

例如.plist使用Xcode以下命令打开所有文件:

defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = "com.apple.property-list"; LSHandlerRoleAll = "com.apple.dt.xcode"; }'

当然,您需要确保那里没有 UTI 的另一个条目com.apple.property-list

这是一个更完整的脚本,它将删除 UTI 的现有条目并添加一个新条目。它只能处理LSHandlerContentType,并且将始终设置LSHandlerRoleAll,并且具有硬编码的包 ID 而不是参数。除此之外,它应该工作得很好。

#!/usr/bin/env bash

PLIST="$HOME/Library/Preferences/com.apple.LaunchServices.plist"
BUDDY=/usr/libexec/PlistBuddy

# the key to match with the desired value
KEY=LSHandlerContentType

# the value for which we'll replace the handler
VALUE=public.plain-text

# the new handler for all roles
HANDLER=com.macromates.TextMate

$BUDDY -c 'Print "LSHandlers"' $PLIST >/dev/null 2>&1
ret=$?
if [[ $ret -ne 0 ]] ; then
        echo "There is no LSHandlers entry in $PLIST" >&2
        exit 1
fi

function create_entry {
        $BUDDY -c "Add LSHandlers:$I dict" $PLIST
        $BUDDY -c "Add LSHandlers:$I:$KEY string $VALUE" $PLIST
        $BUDDY -c "Add LSHandlers:$I:LSHandlerRoleAll string $HANDLER" $PLIST
}

declare -i I=0
while [ true ] ; do
        $BUDDY -c "Print LSHandlers:$I" $PLIST >/dev/null 2>&1
        [[ $? -eq 0 ]] || { echo "Finished, no $VALUE found, setting it to $HANDLER" ; create_entry ; exit ; }

        OUT="$( $BUDDY -c "Print 'LSHandlers:$I:$KEY'" $PLIST 2>/dev/null )"
        if [[ $? -ne 0 ]] ; then 
                I=$I+1
                continue
        fi

        CONTENT=$( echo "$OUT" )
        if [[ $CONTENT = $VALUE ]] ; then
                echo "Replacing $CONTENT handler with $HANDLER"
                $BUDDY -c "Delete 'LSHandlers:$I'" $PLIST
                create_entry
                exit
        else
                I=$I+1 
        fi
done
Run Code Online (Sandbox Code Playgroud)

  • 最简单的方法可能是`x=~/Library/Preferences/com.apple.LaunchServices.plist; plutil -convert xml1 $x; 打开 -a TextEdit $x` 并复制并粘贴这些 LSHandlers 条目。要获取包标识符,您可以执行 `osascript -e 'bundle identifier of (info for (path to app "TextEdit"))'`。 (3认同)