Làm cách nào để thay đổi ứng dụng mặc định cho tất cả các tệp của một loại tệp cụ thể thông qua Terminal trong OS X?
Làm cách nào để thay đổi ứng dụng mặc định cho tất cả các tệp của một loại tệp cụ thể thông qua Terminal trong OS X?
Câu trả lời:
Tôi có một cách đơn giản hơn. Bạn sẽ muốn Homebrew nếu bạn chưa có nó:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install duti
Bây giờ bạn cần tìm id của ứng dụng bạn muốn sử dụng và gán nó cho tiện ích mở rộng bạn muốn sử dụng. Trong ví dụ này, tôi đã sử dụng Chân đế cho *.shvà tôi cũng muốn sử dụng nó cho *.mdcác tệp thay vì xcode.
.shcác tệp:duti -x sh
output:
Brackets.app
/opt/homebrew-cask/Caskroom/brackets/1.6/Brackets.app
io.brackets.appshell
Dòng cuối cùng là id.
.mdcác tệp:duti -s io.brackets.appshell .md all
osascript -e 'id of app "$appName"'để lấy id của bất kỳ ứng dụng nào được cài đặt trên hệ thống của bạn
duti -s $(osascript -e 'id of app "Visual Studio Code"') .md all
Chỉnh sửa ~/Library/Preferences/com.apple.LaunchServices.plist.
Thêm một mục bên dưới LSHandlers, chứa UTI (khóa LSHandlerContentType, ví dụ public.plain-text) và mã định danh gói ứng dụng ( LSHandlerRoleAllví dụ com.macromates.textmate).
Nó trông như thế này trong Trình soạn thảo danh sách tài sản :

Để làm điều này từ dòng lệnh, sử dụng defaultshoặc /usr/libexec/PlistBuddy. Cả hai đều có nhiều trang.
Ví dụ: để mở tất cả .plistcác tệp bằng cách sử dụng Xcode:
defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = "com.apple.property-list"; LSHandlerRoleAll = "com.apple.dt.xcode"; }'
Tất nhiên, bạn cần chắc chắn rằng chưa có mục nào khác cho UTI com.apple.property-listđã có trong đó.
Đây là một tập lệnh hoàn chỉnh hơn sẽ loại bỏ các mục hiện có cho UTI và thêm một mục mới. Nó chỉ có thể xử lý LSHandlerContentTypevà sẽ luôn được đặt LSHandlerRoleAllvà có ID gói được mã hóa cứng thay vì tham số. Ngoài ra, nó nên hoạt động khá tốt.
#!/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
x=~/Library/Preferences/com.apple.LaunchServices.plist; plutil -convert xml1 $x; open -a TextEdit $xsao chép và dán các mục LSHandlers đó. Để có được định danh gói bạn có thể làm osascript -e 'bundle identifier of (info for (path to app "TextEdit"))'.
defaultsdường như không có khả năng thực hiện nó và nó đòi hỏi một vài PlistBuddycuộc gọi. Nhưng nó có thể làm điều đó trong một kịch bản shell có thể tái sử dụng.