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 ("print") as text
035#ラベルカラー
036set numLabelIndex to 7 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      
077      #サードパティの設定ファイルにラベルを付与
078      set ocidLabelNo to (refMe's NSNumber's numberWithInteger:(numLabelIndex))
079      set ListDone to (itemURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error|:(reference))
080      (ocidThirdPartyArray's addObject:(strDirName))
081      if (item 1 of ListDone) is true then
082      else if (item 2 of ListDone) ≠ (missing value) then
083         set strErrorNO to (item 2 of ListDone)'s code() as text
084         set strErrorMes to (item 2 of ListDone)'s localizedDescription() as text
085         refMe's NSLog("■:" & strErrorNO & strErrorMes)
086         return "エラーしました" & strErrorNO & strErrorMes
087      end if
088   else
089      #Appleの設定ファイルには何もしない
090   end if
091end repeat
092##############################
093#ダイアログ
094set ocidSortedArray to ocidThirdPartyArray's sortedArrayUsingSelector:("localizedStandardCompare:")
095set listSortedArray to ocidSortedArray as list
096set strTitle to ("選んでください") as text
097set strPrompt to ("選んだフォルダをゴミ箱に入れます") as text
098set strOK to ("OK") as text
099set strCancel to ("キャンセル") as text
100try
101   tell application "System Events"
102      activate
103      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
104   end tell
105on error strErrMes number numErrNo
106   tell application "System Events" to quit
107   log strErrMes & numErrNo
108   return false
109end try
110if (first item of listResponse) is false then
111   tell application "System Events" to quit
112   error "ユーザによってキャンセルされました。" number -128
113else if (class of listResponse) is list then
114   if listResponse is {} then
115      tell application "System Events" to quit
116      error "Error 何も選んでいません" number -128
117   end if
118end if
119
120##############################
121#ゴミ箱に入れる
122set appFileManager to refMe's NSFileManager's defaultManager()
123set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
124set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
125set ocidContainersDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Preferences") isDirectory:(true)
126
127repeat with itemDirName in listResponse
128   log itemDirName as text
129   set ocidTargetDirPathURL to (ocidContainersDirPathURL's URLByAppendingPathComponent:(itemDirName) isDirectory:(true))
130   
131   
132   set ListDone to (appFileManager's trashItemAtURL:(ocidTargetDirPathURL) resultingItemURL:(ocidTargetDirPathURL) |error|:(reference))
133   if (item 2 of ListDone) ≠ (missing value) then
134      set strErrorNO to (item 2 of ListDone)'s code() as text
135      set strErrorMes to (item 2 of ListDone)'s localizedDescription() as text
136      refMe's NSLog("■:" & strErrorNO & strErrorMes)
137      log "エラーしました" & strErrorNO & strErrorMes
138   end if
139   
140end repeat
141
142
143##############################
144#開く
145set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
146set boolDone to appSharedWorkspace's openURL:(ocidContainersDirPathURL)
147
148#ログ用にテキストを用意して
149set ocidJoinText to ocidSortedArray's componentsJoinedByString:(return)
150#終了
151return ocidJoinText as text
AppleScriptで生成しました