文章目录
macOS 使用交流 QQ 群:658095824,V : ez-code
Music For This Passage
https://music.163.com/#/song?id=1815389717
关于 defaults
defaults 命令用于访问用户的偏好设置文件,应用会将一些数据配置在这个文件中;
通过操作这个文件,可以实现一些功能的开启关闭,数据配置等。具体根据各个应用的开发者有所不同。
常用命令
$ man defaults # 查看执行手册,内容较多见文末
$ defaults -h # 查看帮助
Command line interface to a user's defaults.
Syntax:
'defaults' [-currentHost | -host <hostname>] followed by one of the following:
# 读
read shows all defaults
read <domain> shows defaults for given domain
read <domain> <key> shows defaults for given domain, key
read-type <domain> <key> shows the type for the given domain, key
# 写
write <domain> <domain_rep> writes domain (overwrites existing)
write <domain> <key> <value> writes key for domain
# 重命名 key
rename <domain> <old_key> <new_key> renames old_key to new_key
# 删除
delete <domain> deletes domain
delete <domain> <key> deletes key in domain
import <domain> <path to plist> writes the plist at path to domain
import <domain> - writes a plist from stdin to domain
export <domain> <path to plist> saves domain as a binary plist to path
export <domain> - writes domain as an xml plist to stdout
domains lists all domains
find <word> lists all entries containing word
help print this help
<domain> is ( <domain_name> | -app <application_name> | -globalDomain )
or a path to a file omitting the '.plist' extension
<value> is one of:
<value_rep>
-string <string_value>
-data <hex_digits>
-int[eger] <integer_value>
-float <floating-point_value>
-bool[ean] (true | false | yes | no)
-date <date_rep>
-array <value1> <value2> ...
-array-add <value1> <value2> ...
-dict <key1> <value1> <key2> <value2> ...
-dict-add <key1> <value1> ...
注:执行完 defaults 命令,或者手动修改 plist 文件后,建议执行 killall -SIGTERM cfprefsd
命令,重启偏好设置服务;重启应用以保证效果。
基本使用
domain
可以查看可以修改的 app 偏好设置;比如你想修改 appstore,会发现存在 com.apple.AppStore
$ defaults domains
FN2V63AD2J.com.tencent.ScreenCapture2, MachOView, MobileMeAccounts, S8EX82NJP6.com.macpaw.CleanMyMac4, abnerworks.Typora, adobe.com.Adobe-Spaces-Helper, cn.better365.iRightMouse, cn.better365.iRightMouse.iRightMouseExtension, cn.better365.ishot, com.qtproject, com.qtproject.QtCreator, com.ridiculousfish.HexFiend, com.ripperhe.Bob, com.ripperhe.Bob-debug, ...., com.youdao.YoudaoDict, com.yourcompany.PSMTabBarControlDemo, com.yourcompany.installerbase, com.yourcompany.installerbase.qt-opensource-mac-x64-5.14.2, com.yww.FSEventStreamDemo, y.JNLunarCalendar, zoom.us.ZoomSDKSample
可以看出,从系统应用到第三方应用都有;
反应的是 ~/Library/Preferences
和部分 /Library/Preferences
的plist 文件。
读
读取全部
$ defaults read com.apple.systempreferences
{
AttentionPrefBundleIDs = {
"com.apple.preferences.softwareupdate" = 1;
};
DSKDesktopPrefPane = {
UserFolderPaths = (
"/Users/shushu/Pictures/\\U58c1\\U7eb8"
);
};
DefaultExposeTab = ExposeTab;
DidShowPrefBundleIDs = (
"com.apple.preferences.softwareupdate",
"com.apple.preferences.icloud"
);
DisableAutoLoginButtonIsHidden = 0;
NSNavLastRootDirectory = "~/Pictures/\\U58c1\\U7eb8";
NSNavPanelExpandedSizeForOpenMode = "{799, 448}";
"NSOutlineView Items DSKSourceOfPicturesOutlineView" = (
DSKAppleRootSource,
DSKUserFoldersRootSource,
DSKPhotosRootSource
);
NSQuitAlwaysKeepsWindows = 0;
"NSSplitView Subview Frames keyboard.shortcuts.splitview" = (
"0.000000, 0.000000, 171.000000, 239.000000, NO, NO",
"180.000000, 0.000000, 405.000000, 239.000000, NO, NO"
);
"NSWindow Frame Main Window Frame SystemPreferencesApp 8.0" = "1658 586 668 543 0 0 2560 1417 ";
"NSWindow Frame NSNavPanelAutosaveName" = "678 695 799 448 0 0 2560 1417 ";
SecurityPrefTab = Privacy;
ShowAllMode = 0;
SoundTab = input;
ThirdPartyCount = 2;
"com.apple.PreferenceSync.ExcludeSyncKeys" = (
"Main Window Frame SystemPreferencesApp 8.0",
"com.apple.preference.keyboard.lastselectedtab"
);
...
"com.apple.preferences.sharing.selectedservice" = 6;
"mouse.lastselectedtab" = 1;
}
读取部分键
$ defaults read com.apple.systempreferences DisableAutoLoginButtonIsHidden
0
$ defaults read com.apple.systempreferences DidShowPrefBundleIDs
(
"com.apple.preferences.softwareupdate",
"com.apple.preferences.icloud"
)
写
# 修改 NSQuitAlwaysKeepsWindows 键
$ defaults write com.apple.systempreferences NSQuitAlwaysKeepsWindows 1
# AttentionPrefBundleIDs 是一个字典,修改这个字典中的 com.apple.preferences.softwareupdate 键
$ defaults write com.apple.systempreferences AttentionPrefBundleIDs -dict com.apple.preferences.softwareupdate 1
删除
$ defaults delete com.netease.163music # 删除云音乐相关偏好设置
常见使用
Finder
显示隐藏文件
以.
开头的文件默认是被影藏的,使用这个命令可以展示他们
$ defaults write com.apple.Finder AppleShowAllFiles -bool true
在Finder标题栏显示完整路径
$ defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
显示文件后缀名
$ defaults write NSGlobalDomain AppleShowAllExtensions -bool true
safari
高亮非Retina的图片
$ defaults write -g CGContextHighlight2xScaledImages YES
禁止Safari生成预览文件
$ defaults write com.apple.Safari DebugSnapshotsUpdatePolicy -int 2
开启debug菜单
$ defaults write com.apple.Safari IncludeInternalDebugMenu 1
$ killall Safari
强制Safari在新标签中打开网页
$ defaults write com.apple.Safari TargetedClicksCreateTabs -bool true
loginwindow
- 在“快速查看”中开启文本选择功能
空格键一直是OSX最有用的功能之一。并且在快速预览中拥有文字选择功能似乎也是理所应当的。下面告诉你怎样打开它。
$ sudo defaults write /Library/Preferences/com.apple.loginwindow AdminHostInfo HostName`
名雪按:同理你可以在该命令后再添一些系统变量和自己的字符串啥的
- 禁止电源键一碰就睡眠
$ defaults write com.apple.loginwindow PowerButtonSleepsSystem -bool no
改变登陆背景
引号里边是图片路径
$ defaults write /Library/Preferences/com.apple.loginwindow DesktopPicture "/Library/Desktop/Nature/Aurora.jpg"`
禁用自动登录
$ defaults delete /Library/Preferences/com.apple.loginwindow autoLoginUser
开启/关闭 MacBook充电提示音
# 开启:
$ sudo defaults write com.apple.PowerChime ChimeOnAllHardware -bool true
$ open /System/Library/CoreServices/PowerChime.app
# 关闭:
$ sudo defaults write com.apple.PowerChime ChimeOnAllHardware -bool false; killall PowerChime
Airdrop
开启有线网络下的 Airdrop 支持
$ defaults write com.apple.NetworkBrowser BrowseAllInterfaces -bool true
CrashReporter 崩溃报告
关闭崩溃报告
$ defaults write com.apple.CrashReporter DialogType none
Dock
取消自动隐藏Dock的触发时间
对那些隐藏了Dock的用户来说,把鼠标移动到屏幕下缘呼出Dock有一个短延迟。
你可能对此不敏感,不过移除该延迟后会让此非常明显,有一种 Mac 更快了的错觉。
$ defaults write com.apple.Dock autohide-delay -float 0 && killall dock
你可以将-float 0修改为任意你想要的触发延迟,以秒为单位。
加快Mission Control的动画效果
这是另一个让你有 Mac 变快错觉的trick,仅仅是缩短了Mission Control动画的长度。同样的,可以在-float后自定义长度,以秒为单位。
$ defaults write com.apple.dock expose-animation-duration -float 0.12 && killall Dock`
让Dock中隐藏的程序图标半透明
“隐藏窗口”一直是一个 OS X 的实用功能,不过默认设置下不易区分哪些程序是被隐藏了的。这里使用一个简单的命令开启隐藏程序的图标半透明,这样就便于区别了。
$ defaults write com.apple.Dock showhidden -bool YES && killall Dock`
Screencapture 截屏
去掉窗口截屏的阴影
对窗口进行截屏的时候(Command-Shift-4, 空格),得到的图片周围会自动被加上一圈阴影。如果你不喜欢这个阴影的效果,可以把它关掉。
$ defaults write com.apple.screencapture disable-shadow -bool true
改变截图的默认保存地址
如果你经常⌘⇧3或⌘⇧4,你一定明白你的桌面会很快被一堆png淹没。推荐解决方案是在/Pictures或/Documents新建一个文件夹,然后将其设为默认的屏幕截图保存位置
$ defaults write com.apple.screencapture location ~/Pictures/Screenshots`
改变截图的默认格式
依然是屏幕截图。你可以改变默认文件格式为JPG或其它的格式。JPG可以提供最佳的图片质量和压缩比。
$ defaults write com.apple.screencapture type jpg && killall SystemUIServer
Xcode
显示Xcode 每一次build的所用时间
$ default write com.apple.dt.Xcode ShowBuildOperationDuration YES `
Helpviewer 帮助
更改mac的帮助显示方式
$ defaults write com.apple.helpviewer DevMode -bool true
想要还原
$ defaults delete com.apple.helpviewer DevMode`
改变系统字体大小(菜单等)
$ defaults write NSGlobalDomain AppleDisplayScaleFactor 1.25
这里边的数字,默认值为1,数值越大字体越大想要恢复,在终端中输入
$ defaults write NSGlobalDomain AppleDisplayScaleFactor 1
Launchpad
$ defaults write com.apple.dock springboard-columns -int 8 # 一行显示 8 列
$ defaults write com.apple.dock springboard-rows -int 7 # 显示 7 行
$ defaults write com.apple.dock ResetLaunchPad -bool TRUE;
$ killall Dock
Dashboard
关闭Dashboard
$ defaults write com.apple.dashboard mcx-disabled -boolean true
启用仪表盘
$ defaults write com.apple.dashboard mcx-disabled -boolean NO
$ killall Dock
开启/关闭 HIDPI
# 开启
$ sudo defaults write /Library/Preferences/com.Apple.windowserver DisplayResolutionEnabled -bool true
# 关闭:
$ sudo defaults delete /Library/Preferences/com.Apple.windowserver DisplayResolutionEnabled
此方法是开启原生的HIDPI,效果为当前分辨率/2
查看Defaults命令的执行历史
查看所有 defaults 相关
$ history |grep "defaults"
只查 defaults write 相关命令
$ history |grep "defaults write"
只查 defaults delete 相关命令
$ history |grep "defaults delete"
查看和某个应用有关的所有 defaults 命令
history |grep "defaults write com.apple.finder"
Manual
DEFAULTS(1) BSD General Commands Manual DEFAULTS(1)
NAME
defaults – access the Mac OS X user defaults system
SYNOPSIS
defaults [-currentHost | -host hostname] read [domain [key]]
defaults [-currentHost | -host hostname] read-type domain key
defaults [-currentHost | -host hostname] write domain { ‘plist’ | key ‘value’ }
defaults [-currentHost | -host hostname] rename domain old_key new_key
defaults [-currentHost | -host hostname] delete [domain [key]]
defaults [-currentHost | -host hostname] { domains | find word | help }
DESCRIPTION
Defaults allows users to read, write, and delete Mac OS X user defaults from a command-line shell. Mac OS X applications and other programs use the defaults system to record user preferences and other information that must be maintained when the applications aren’t running (such as default font for new documents, or the position of an Info panel). Much of this information is accessible through an application’s Preferences panel, but some of it isn’t, such as the position of the Info panel. You can access this information with defaults
Note: Since applications do access the defaults system while they’re running, you shouldn’t modify the defaults of a running application. If you change a default in a domain that belongs to a running application, the application won’t see the change and might even overwrite the default.
User defaults belong to domains, which typically correspond to individual applications. Each domain has a dictionary of keys and values representing its defaults; for example, “Default Font” = “Helvetica”. Keys are always strings, but values can be complex data structures comprising arrays, dictionaries, strings, and binary data. These data structures are stored as XML Property Lists.
Though all applications, system services, and other programs have their own domains, they also share a domain named NSGlobalDomain. If a default isn’t specified in the application’s domain, but is specified in NSGlobalDomain, then the application uses the value in that domain.
The commands are as follows:
read Prints all of the user’s defaults, for every domain, to standard output.
read domain Prints all of the user’s defaults for domain to standard output.
read domain key
Prints the value for the default of domain identified by key.
read-type domain key
Prints the plist type for the given domain identified by key.
write domain key ‘value’
Writes value as the value for key in domain. value must be a property list, and must be enclosed in single quotes. For example:
defaults write com.companyname.appname “Default Color” ‘(255, 0, 0)’
sets the value for Default Color to an array containing the strings 255, 0, 0 (the red, green, and blue components). Note that the key is enclosed in quotation marks because it contains a space.
write domain ‘plist’
Overwrites the defaults information in domain with that given as plist. plist must be a property list representation of a dictionary, and must be enclosed in single quotes. For example:
defaults write com.companyname.appname ‘{ “Default Color” = (255, 0, 0); “Default Font” = Helvetica; }’;
erases any previous defaults for com.companyname.appname and writes the values for the two names into the defaults system.
delete domain
Removes all default information for domain.
delete domain key
Removes the default named key from domain.
domains Prints the names of all domains in the user’s defaults system.
domains Prints the names of all domains in the user’s defaults system.
find word Searches for word in the domain names, keys, and values of the user’s defaults, and prints out a list of matches.
help Prints a list of possible command formats.
OPTIONS
Specifying domains:
domain If no flag is specified, domain is a domain name of the form com.companyname.appname. Example:
defaults read com.apple.TextEdit
-app application
The name of an application may be provided instead of a domain using the -app flag. Example:
defaults read -app TextEdit
filepath Domains may also be specified as a path to an arbitrary plist file, with or without the ‘.plist’ extension. For example:
defaults read ~/Library/Containers/com.apple.TextEdit/Data/Library/Preferences/com.apple.TextEdit.plist
normally gives the same result as the two previous examples. In the following example:
defaults write ~/Desktop/TestFile foo bar
will write the key ‘foo’ with the value ‘bar’ into the plist file ‘TestFile.plist’ that is on the user’s desktop. If the file does not exist, it will be created. If it does exist, the key-value pair will be added, overwriting the value of ‘foo’ if it already existed.
WARNING: The defaults command will be changed in an upcoming major release to only operate on preferences domains. General plist manipulation utilities will be folded into a different command-line program.
-g | -globalDomain | NSGlobalDomain
Specify the global domain. ‘-g’ and ‘-globalDomain’ may be used as synonyms for NSGlobalDomain.
Specifying value types for preference keys:
If no type flag is provided, defaults will assume the value is a string. For best results, use one of the type flags, listed below.
-string Allows the user to specify a string as the value for the given preference key.
-data Allows the user to specify a bunch of raw data bytes as the value for the given preference key. The data must be provided in hexidecimal.
-int[eger] Allows the user to specify an integer as the value for the given preference key.
-float Allows the user to specify a floating point number as the value for the given preference key.
-bool[ean] Allows the user to specify a boolean as the value for the given preference key. Value must be TRUE, FALSE, YES, or NO.
-date Allows the user to specify a date as the value for the given preference key.
-array Allows the user to specify an array as the value for the given preference key:
defaults write somedomain preferenceKey -array element1 element2 element3
The specified array overwrites the value of the key if the key was present at the time of the write. If the key was not present, it is created with the new value.
-array-add Allows the user to add new elements to the end of an array for a key which has an array as its value. Usage is the same as -array above. If the key was not present, it is created with the specified array as its value.
-dict Allows the user to add a dictionary to the defaults database for a domain. Keys and values are specified in order:
defaults write somedomain preferenceKey -dict key1 value1 key2 value2
The specified dictionary overwrites the value of the key if the key was present at the time of the write. If the key was not present, it is created with the new value.
-dict-add Allows the user to add new key/value pairs to a dictionary for a key which has a dictionary as its value. Usage is the same as -dict above. If the key was not present, it is created with the specified dictionary as its value.
Specifying a host for preferences:
Operations on the defaults database normally apply to any host the user may log in on, but may be restricted to apply only to a specific host.
If no host is provided, preferences operations will apply to any host the user may log in on.
-currentHost
Restricts preferences operations to the host the user is currently logged in on.
-host hostname
Restricts preferences operations to hostname.
BUGS
Defaults can be structured in very complex ways, making it difficult for the user to enter them with this command.
HISTORY
First appeared in NeXTStep.
Mac OS X Nov 3, 2003 Mac OS X
(END)