Edited at

Cocoaの機能を使ってdefaultsコマンドを実行するAppleScriptハンドラ

More than 3 years have passed since last update.



  • defaultsコマンドを使ってアプリケーションやシステムの設定を読み書きできる



  • CocoaではNSUserDefaultsクラスを使う

  • 下のbashとAppleScriptは同じ操作


  • defaultsWritedefaultsDeleteは一度に複数の項目を対象にできる

defaults read com.apple.dock

defaults write com.apple.dock pinning -string start
defaults delete com.apple.dock pinning


defaultsRead_Write_Delete.scpt

use framework "Foundation"

my defaultsRead("com.apple.dock")
my defaultsWrite("com.apple.dock", {pinning:"start"})
my defaultsDelete("com.apple.dock", {"pinning"})

on defaultsRead(domain as text)
--require framework: Foundation
set defaults to current application's NSUserDefaults's standardUserDefaults()
return (defaults's persistentDomainForName:domain) as record
end defaultsRead

on defaultsWrite(domain as text, writeDictionary as record)
--require framework: Foundation
set defaults to current application's NSUserDefaults's standardUserDefaults()
set preferencesDictionary to (defaults's persistentDomainForName:domain)'s mutableCopy()
preferencesDictionary's addEntriesFromDictionary:writeDictionary
defaults's setPersistentDomain:preferencesDictionary forName:domain
my defaultsSynchronize()
end defaultsWrite

on defaultsDelete(domain as text, deleteKeys as list)
--require framework: Foundation
set defaults to current application's NSUserDefaults's standardUserDefaults()
set preferencesDictionary to (defaults's persistentDomainForName:domain)'s mutableCopy()
preferencesDictionary's removeObjectsForKeys:deleteKeys
defaults's setPersistentDomain:preferencesDictionary forName:domain
my defaultsSynchronize()
end defaultsDelete

on defaultsSynchronize()
--require framework: Foundation
set success to current application's NSUserDefaults's standardUserDefaults()'s synchronize()
if not success then
error "Synchronization in NSUserDefaults has failed"
end if
end defaultsSynchronize



更新履歴


  • 2016-01-25: Cocoaの機能を使って作成

  • 2016-02-20: defaultsSynchronizeハンドラ作成