6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-01-25
  • 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ハンドラ作成
6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?