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)
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。
.md
文件:duti -s io.brackets.appshell .md all
Run Code Online (Sandbox Code Playgroud)
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)
归档时间: |
|
查看次数: |
28706 次 |
最近记录: |