« The Best of Me (かけがえのない人) | トップページ | 歳とって 旧き凄さを 思い知る… »

LaunchAgents・LaunchDaemonsの起動項目を停止する

アプリケーションを削除するときに
忘れがちなLaunchAgentsとLaunchDaemonsを削除(といっても移動して取っておくけど)する
最近wacomのタブレット・ドライバの削除時に
なぜか?削除されずに残ったので作成しておいた
ポイントは

/bin/launchctl' unload

削除するまえにちゃんとアンロードするでしょうか?
(まぁ削除しちゃっても、次回の電源OFF〜On時に読まれなくなるので不要と言えばナニだけど)


(*

DelLaunchPlist.scpt
20170202 初回作成

あらかじめDisabled用のディレクトリを作って
LaunchAgents
LaunchDaemons
ある、起動項目をアン・ロードして停止
停止後、にDisabledディレクトリに移動します。

*)




on run
---プロンプトの文言改行が使えます\nを入れます
set theWithPrompt to ""
---ファイル選択ダイアログのデフォルトのディレクトリ
set theDefLoc to (((the path to library folder from user domain) as text) & "LaunchAgents:") as alias
---Uniform Type Identifier指定
---詳しくは http://goo.gl/6jAQa Uniform Type Identifier
set theFileType to "com.apple.property-list" as text
---のファイルタイプをリスト形式に整形する
set AppleScript's text item delimiters to {","}
set theFileTypeList to every text item of theFileType
---ダイアログを出して選択されたファイルは「open」に渡す
open (choose file default location theDefLoc ¬
with prompt theWithPrompt ¬
of type theFileTypeList ¬
invisibles true ¬
with multiple selections allowed without showing package contents)
end run






on open ObjDropOrOpen

---ユーザードメインのライブラリにLaunchAgents(Disabled)フォルダを作ります
try
set theCmdCom to ""
set theUserLaunchAgents to (((the path to library folder from user domain) as text) & "LaunchAgents:") as alias
set theUserLaunchAgentsPath to POSIX path of theUserLaunchAgents as text
set theUserLaunchAgentsPathDisabled to doReplace(theUserLaunchAgentsPath, "LaunchAgents/", "LaunchAgents(Disabled)")
---コマンドライン整形
set theCmdCom to ("'/bin/mkdir' -p '" & theUserLaunchAgentsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom
end try
---ローカルドメインのライブラリにLaunchAgents(Disabled)フォルダを作ります
try
set theCmdCom to ""
set theLocalLaunchAgents to (((the path to library folder from local domain) as text) & "LaunchAgents:") as alias
set theLocalLaunchAgentsPath to POSIX path of theLocalLaunchAgents as text
set theLocalLaunchAgentsPathDisabled to doReplace(theLocalLaunchAgentsPath, "LaunchAgents/", "LaunchAgents(Disabled)")
---コマンドライン整形
set theCmdCom to ("'/bin/mkdir' -p '" & theLocalLaunchAgentsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try
---ローカルドメインのライブラリにLaunchDaemons(Disabled)フォルダを作ります
try
set theCmdCom to ""
set theLocalLaunchDaemons to (((the path to library folder from local domain) as text) & "LaunchDaemons:") as alias
set theLocalLaunchDaemonsPath to POSIX path of theLocalLaunchDaemons as text
set theLocalLaunchDaemonsPathDisabled to doReplace(theLocalLaunchDaemonsPath, "LaunchDaemons/", "LaunchDaemons(Disabled)")
---コマンドライン整形
set theCmdCom to ("'/bin/mkdir' -p '" & theLocalLaunchDaemonsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try
---システムドメインのライブラリにLaunchAgents(Disabled)フォルダを作ります
try
set theCmdCom to ""
set theSystemLaunchAgents to (((the path to library folder from system domain) as text) & "LaunchAgents:") as alias
set theSystemLaunchAgentsPath to POSIX path of theSystemLaunchAgents as text
set theSystemLaunchAgentsPathDisabled to doReplace(theSystemLaunchAgentsPath, "LaunchAgents/", "LaunchAgents(Disabled)")
---コマンドライン整形
set theCmdCom to ("'/bin/mkdir' -p '" & theSystemLaunchAgentsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try
---システムドメインのライブラリにLaunchDaemons(Disabled)フォルダを作ります
try
set theCmdCom to ""
set theSystemLaunchDaemons to (((the path to library folder from system domain) as text) & "LaunchDaemons:") as alias
set theSystemLaunchDaemonsPath to POSIX path of theSystemLaunchDaemons as text
set theSystemLaunchDaemonsPathDisabled to doReplace(theSystemLaunchDaemonsPath, "LaunchDaemons/", "LaunchDaemons(Disabled)")
---コマンドライン整形
set theCmdCom to ("'/bin/mkdir' -p '" & theSystemLaunchDaemonsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try


---複数選択した場合もあるのでリピート

repeat with objFiles in ObjDropOrOpen
---選択したファイルのエイリアスを取得
set theAlias to objFiles as alias
---UNIXパスに変換
set thePlistPath to POSIX path of theAlias as text
---アクセス権を調整
try
---コマンドライン整形
set theCmdCom to ("'/bin/chmod' 644 '" & thePlistPath & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try
---オーナーをrootに変更
try
---コマンドライン整形
set theCmdCom to ("'/usr/sbin/chown' root '" & thePlistPath & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try
---ここでようやくアン・ロード(停止)させます
try
---コマンドライン整形
set theCmdCom to ("'/bin/launchctl' unload -w '" & thePlistPath & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end try
---停止後にDisabledディレクトリに移動
if thePlistPath contains theUserLaunchAgentsPath then
---コマンドライン整形
set theCmdCom to ("'/bin/mv' -f '" & thePlistPath & "' '" & theUserLaunchAgentsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges

else if thePlistPath contains theSystemLaunchAgentsPath then
---コマンドライン整形
set theCmdCom to ("'/bin/mv' -f '" & thePlistPath & "' '" & theSystemLaunchAgentsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges

else if thePlistPath contains theSystemLaunchDaemonsPath then
---コマンドライン整形
set theCmdCom to ("'/bin/mv' -f '" & thePlistPath & "' '" & theSystemLaunchDaemonsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges

else if thePlistPath contains theLocalLaunchAgentsPath then
---コマンドライン整形
set theCmdCom to ("'/bin/mv' -f '" & thePlistPath & "' '" & theLocalLaunchAgentsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges

else if thePlistPath contains theLocalLaunchDaemonsPath then
---コマンドライン整形
set theCmdCom to ("'/bin/mv' -f '" & thePlistPath & "' '" & theLocalLaunchDaemonsPathDisabled & "'") as text
---コマンド実行
do shell script theCmdCom with administrator privileges
end if
end repeat
end open




-----#文字の置き換え
to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace

「DelLaunchPlist.scpt.zip」をダウンロード

|

« The Best of Me (かけがえのない人) | トップページ | 歳とって 旧き凄さを 思い知る… »

OSX」カテゴリの記事