20260602

[システム設定]初期設定フォルダメンテナンス(サードパーティの設定ファイルにラベルをつける)

[システム設定]初期設定フォルダメンテナンス(サードパーティの設定ファイルにラベルをつける)

NOTE記事一覧ですnote.com

【Safari・FireFox用Script Editorで開く】 |

[メンテナンス] Preferencesのサードパーティにラベルを入れる.applescript.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005
006メンテナンス用のヘルパー
007/Users/ユーザーID/Library/Preferences
008いわゆる『サンドボックス・アプリケーション』のディレクトリで
009サードバーティ製品のディレクトリに
010緑のタグを付けることで
011
012未使用になったアプリの設定を削除するのを手伝います。
013
014サードバーティ製品のリストをダイアログに出します
015選択したフォルダをゴミ箱に入れます
016ゴミ箱に入れるには
017セキュリティ設定の『フルディスクアクセス』が必要です。
018
019心配な場合は、実行しないでください
020
021
022
023com.cocolog-nifty.quicktimer.icefloe *)
024----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
025use AppleScript version "2.8"
026use framework "Foundation"
027use framework "AppKit"
028use scripting additions
029
030property refMe : a reference to current application
031
032####################################
033#検索語句 設定項目 この語句にマッチしないファイルにラベルを入れる
034set strSearchWord to ("apple") as text
035#ラベルカラー
036set numLabelIndex to 2 as integer
037(* 
0380:ラベル無し
0391:グレー
0402:グリーン
0413:パープル
0424:ブルー
0435:イエロー
0446:レッド
0457:オレンジ  *)
046
047
048
049####################################
050#Containers
051set appFileManager to refMe's NSFileManager's defaultManager()
052set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
053set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
054set ocidTargetDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Preferences") isDirectory:(true)
055#
056set ocidPropertiesForKeys to refMe's NSMutableArray's alloc()'s init()
057ocidPropertiesForKeys's addObject:(refMe's NSURLIsRegularFileKey)
058ocidPropertiesForKeys's addObject:(refMe's NSURLNameKey)
059ocidPropertiesForKeys's addObject:(refMe's NSURLPathKey)
060set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles)
061#URLの収集
062set listResponse to appFileManager's contentsOfDirectoryAtURL:(ocidTargetDirPathURL) includingPropertiesForKeys:(ocidPropertiesForKeys) options:(ocidOption) |error|:(reference)
063set ocidItemURLArray to (item 1 of listResponse)
064
065#ログ用
066set ocidThirdPartyArray to refMe's NSMutableArray's alloc()'s init()
067
068##############################
069# サードパーティチェック
070#Apple以外のコンテナの場合は グリーンのインデックスをつける
071repeat with itemURL in ocidItemURLArray
072   #フォルダ名が
073   set strDirName to (itemURL's lastPathComponent()) as text
074   #Apple
075   if strDirName contains strSearchWord then
076      #Appleの設定ファイルには何もしない
077   else
078      #サードパティの設定ファイルにラベルを付与
079      set ocidLabelNo to (refMe's NSNumber's numberWithInteger:(numLabelIndex))
080      set ListDone to (itemURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error|:(reference))
081      (ocidThirdPartyArray's addObject:(strDirName))
082      if (item 1 of ListDone) is true then
083      else if (item 2 of ListDone) ≠ (missing value) then
084         set strErrorNO to (item 2 of ListDone)'s code() as text
085         set strErrorMes to (item 2 of ListDone)'s localizedDescription() as text
086         refMe's NSLog("■:" & strErrorNO & strErrorMes)
087         return "エラーしました" & strErrorNO & strErrorMes
088      end if
089   end if
090end repeat
091##############################
092#ダイアログ
093set ocidSortedArray to ocidThirdPartyArray's sortedArrayUsingSelector:("localizedStandardCompare:")
094set listSortedArray to ocidSortedArray as list
095set strTitle to ("選んでください") as text
096set strPrompt to ("選んだフォルダをゴミ箱に入れます") as text
097set strOK to ("OK") as text
098set strCancel to ("キャンセル") as text
099try
100   tell application "System Events"
101      activate
102      set listResponse to (choose from list listSortedArray with title strTitle with prompt strPrompt default items (last item of listSortedArray) OK button name strOK cancel button name strCancel with multiple selections allowed without empty selection allowed) as list
103   end tell
104on error strErrMes number numErrNo
105   tell application "System Events" to quit
106   log strErrMes & numErrNo
107   return false
108end try
109if (first item of listResponse) is false then
110   tell application "System Events" to quit
111   error "ユーザによってキャンセルされました。" number -128
112else if (class of listResponse) is list then
113   if listResponse is {} then
114      tell application "System Events" to quit
115      error "Error 何も選んでいません" number -128
116   end if
117end if
118
119##############################
120#ゴミ箱に入れる
121set appFileManager to refMe's NSFileManager's defaultManager()
122set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
123set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
124set ocidContainersDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Preferences") isDirectory:(true)
125
126repeat with itemDirName in listResponse
127   log itemDirName as text
128   set ocidTargetDirPathURL to (ocidContainersDirPathURL's URLByAppendingPathComponent:(itemDirName) isDirectory:(true))
129   
130   
131   set ListDone to (appFileManager's trashItemAtURL:(ocidTargetDirPathURL) resultingItemURL:(ocidTargetDirPathURL) |error|:(reference))
132   if (item 2 of ListDone) ≠ (missing value) then
133      set strErrorNO to (item 2 of ListDone)'s code() as text
134      set strErrorMes to (item 2 of ListDone)'s localizedDescription() as text
135      refMe's NSLog("■:" & strErrorNO & strErrorMes)
136      log "エラーしました" & strErrorNO & strErrorMes
137   end if
138   
139end repeat
140
141
142##############################
143#開く
144set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
145set boolDone to appSharedWorkspace's openURL:(ocidContainersDirPathURL)
146
147#ログ用にテキストを用意して
148set ocidJoinText to ocidSortedArray's componentsJoinedByString:(return)
149#終了
150return ocidJoinText as text
AppleScriptで生成しました