有没有更快的方法来更改与 OS X 上的文件类型相关联的默认应用程序?

有没有更快的方法来更改与 OS X 上的文件类型相关联的默认应用程序?

还有什么比使用更方便吗?RC默认应用或者魔法发射,还是只是反复按下 Finder 信息面板中的“更改全部”按钮?

我考虑编写一个 shell 脚本来修改 Info.plist 文件中的 CFBundleDocumentTypes 数组。但每个应用程序都有多个需要更改的键(有时是一个图标)。

lsregister不能用于对启动服务数据库进行特定修改。

$ `locate lsregister` -h
lsregister: [OPTIONS] [ <path>... ]
                      [ -apps <domain>[,domain]... ]
                      [ -libs <domain>[,domain]... ]
                      [ -all  <domain>[,domain]... ]

Paths are searched for applications to register with the Launch Service database.
Valid domains are "system", "local", "network" and "user". Domains can also
be specified using only the first letter.

  -kill     Reset the Launch Services database before doing anything else
  -seed     If database isn't seeded, scan default locations for applications and libraries to register
  -lint     Print information about plist errors while registering bundles
  -convert  Register apps found in older LS database files
  -lazy n   Sleep for n seconds before registering/scanning
  -r        Recursive directory scan, do not recurse into packages or invisible directories
  -R        Recursive directory scan, descending into packages and invisible directories
  -f        force-update registration even if mod date is unchanged
  -u        unregister instead of register
  -v        Display progress information
  -dump     Display full database contents after registration
  -h        Display this help

答案1

杜蒂

  1. 下载 pkg 安装程序或者从源代码编译
  2. 将这样的文件保存在某处:

    com.macromates.textmate public.shell-script all
    com.macromates.textmate public.unix-executable all
    com.macromates.textmate com.apple.property-list all
    org.videolan.vlc .avi all
    org.videolan.vlc .mkv all
    # ...
    
  3. duti $file.duti

查找捆绑包标识符或 UTI:

bundleid() {
  osascript -e "id of app \"$*\""
}

getuti() {
  local f="/tmp/me.lri.getuti.${1##*.}"
  touch "$f"
  mdimport "$f"
  mdls -name kMDItemContentTypeTree "$f"
  rm "$f"
}

lsapps

在发现它之前,我编写了一个与 duti 非常相似的 Ruby 脚本。不过,它需要重新启动操作系统才能应用更改。它还会覆盖对 的任何外部更改com.apple.LaunchServices.plist

#!/usr/bin/env ruby

datafile = "#{ENV['HOME']}/Notes/lsapps.txt"
cachedir = "#{ENV['HOME']}/Library/Caches/me.lri.scripts"
cachefile = cachedir + "/lsapps"
`mkdir -p #{cachedir}; touch #{cachefile}`
cachetext = IO.read(cachefile)

a = []
IO.readlines(datafile).each do |line|
  line.strip!
  next unless line =~ /^([^#].*): (.+)/
  z = $1, $2

  bid = cachetext.scan(/#{z[0]}  (.*)/)[0]
  unless bid
    bid = `osascript -e 'id of app "#{z[0]}"'`.chomp
    next unless bid
    File.open(cachefile, "a") { |f| f.puts(z[0] + "  " + bid) }
  end

  z[1].strip.split(" ").each do |e|
    if e =~ /(.+):\/\/$/
      a << "{LSHandlerURLScheme='#{$1}';LSHandlerRoleAll='#{bid}';}"
    elsif e.include?(".")
      a << "{LSHandlerContentType='#{e}';LSHandlerRoleAll='#{bid}';}"
    else
      a << "{LSHandlerContentTag='#{e}';LSHandlerContentTagClass=\
'public.filename-extension';LSHandlerRoleAll='#{bid}';}"
    end
  end
end

system("defaults", "write", "com.apple.LaunchServices", "LSHandlers",
"(" + a.join(",") + ")")

`defaults write com.apple.LaunchServices LSQuarantine -bool false`

lsapps.txt

NetNewsWire Lite: feed:// public.rss
iCal: icaltodo icalevent
Google Chrome: chrome://
Safari: public.html
TextEdit: txt text md markdown csv
TextMate: public.shell-script public.unix-executable public.script ssh:// xml plist dict css rst tex sh pl py rb gemspec php js java c h m conf bash opml as cfm cfml class coffee ctp erb rhtml haml less msass scss yaml rd
VLC: avi mpg flv mkv mp4 flac 3gp
The Unarchiver: rar
Skim: pdf
iChm: chm
AppleScript Editor: scpt
Sequential: png jpg

答案2

swda setHandler --app /Applications/VLC.app --UTI public.mp3

安装

wget 'https://github.com/Lord-Kamina/SwiftDefaultApps/releases/download/v2.0.1/SwiftDefaultApps-v2.0.1.zip'
unzip SwiftDefaultApps-v2.0.1.zip

测试于

MacOS Monterey 12.3.1

致谢

https://apple.stackexchange.com/a/380631/57681

答案3

您可以非常轻松地更改打开所有 AVI 的应用程序,无需编写脚本。找到一个 AVI 文件,然后对其执行“获取信息”。这将显示文件信息。选择要打开文件的程序,然后单击“更改全部...”按钮。这将提示您更改默认关联。 在此处输入图片描述

一个小问题,资源分支不会被淘汰,但创建者代码会被淘汰。因此,较新的 Mac OS 系统拥有更灵活的系统来确定如何打开文件。应用程序应该可以处理没有资源分支的文件,但有些应用程序仍在使用它们。

相关内容