[AppleScript]com.apple.TCC.configuration-profile-policyの作成補助 v2
mobileconfigファイルとplistファイルを生成します
com.apple.TCC.configuration-profile-policyの作成補助.scpt
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # モバイルコンフィグのアプリケーションの署名部分CodeRequirementの |
005 | # 値を取得します |
006 | # configuration-profile-policy限定なのでMDM登録必須 |
007 | # com.cocolog-nifty.quicktimer.icefloe |
008 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
009 | use framework "Foundation" |
010 | use AppleScript version "2.4" |
011 | use framework "Foundation" |
012 | use framework "AppKit" |
013 | use scripting additions |
014 | property refMe : a reference to current application |
015 | |
016 | ###### |
017 | #メインのアプリケーション選択 |
018 | set {strBundleID, strAppFilePath} to doGetBundleID() |
019 | #メインアプリの署名コード |
020 | set strCodesign to doGetCodesign(strAppFilePath) |
021 | |
022 | ###### |
023 | #ダイアログ |
024 | #サービス一覧リスト |
025 | set listServices to {"Accessibility", "AddressBook", "AppleEvents", "BluetoothAlways", "Calendar", "Camera", "FileProviderPresence", "ListenEvent", "MediaLibrary", "Microphone", "Photos", "PostEvent", "Reminders", "ScreenCapture", "SpeechRecognition", "SystemPolicyAllFiles", "SystemPolicyAppBundles", "SystemPolicyAppData", "SystemPolicyDesktopFolder", "SystemPolicyDocumentsFolder", "SystemPolicyDownloadsFolder", "SystemPolicyNetworkVolumes", "SystemPolicyRemovableVolumes", "SystemPolicySysAdminFiles"} as list |
026 | #ダイアログ前面に |
027 | set strName to (name of current application) as text |
028 | if strName is "osascript" then |
029 | tell application "SystemUIServer" to activate |
030 | else |
031 | tell current application to activate |
032 | end if |
033 | set strTitle to ("選んでください") as text |
034 | set strPrompt to ("ひとつ選んでください") as text |
035 | try |
036 | tell application "SystemUIServer" |
037 | #Activateは必須 |
038 | activate |
039 | set valueResponse to (choose from list listServices with title strTitle with prompt strPrompt default items (item 1 of listServices) OK button name "OK" cancel button name "キャンセル" with empty selection allowed and multiple selections allowed) |
040 | end tell |
041 | on error |
042 | log "Error choose from list" |
043 | return false |
044 | end try |
045 | if (class of valueResponse) is boolean then |
046 | log "Error キャンセルしました" |
047 | return false |
048 | else if (class of valueResponse) is list then |
049 | if valueResponse is {} then |
050 | log "Error 何も選んでいません" |
051 | return false |
052 | else |
053 | set listMakeService to valueResponse as list |
054 | end if |
055 | end if |
056 | ######### |
057 | #出力テキスト保存先 |
058 | set appFileManager to refMe's NSFileManager's defaultManager() |
059 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
060 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
061 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Mobileconfig/Codesign") isDirectory:(true) |
062 | #フォルダ作成 |
063 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
064 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
065 | set ocidMakeDirPath to ocidSaveDirPathURL's |path|() |
066 | set boolDirExists to appFileManager's fileExistsAtPath:(ocidMakeDirPath) isDirectory:(true) |
067 | if boolDirExists is false then |
068 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error| :(reference) |
069 | if (item 1 of listDone) is false then |
070 | set strErrorNO to (item 2 of listDone)'s code() as text |
071 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
072 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
073 | log "createDirectoryAtURL エラーしました" & strErrorNO & strErrorMes |
074 | return false |
075 | end if |
076 | end if |
077 | |
078 | ########################## |
079 | #Payload |
080 | #初期化 |
081 | set ocidPayloadDict to refMe's NSMutableDictionary's alloc()'s init() |
082 | #PayloadType |
083 | set strTypeID to ("com.apple.TCC.configuration-profile-policy") as text |
084 | ocidPayloadDict's setValue:(strTypeID) forKey:("PayloadType") |
085 | #PayloadUUID |
086 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
087 | set strUUID to ocidUUID's UUIDString() as text |
088 | ocidPayloadDict's setValue:(strUUID) forKey:("PayloadUUID") |
089 | #日付テキストの生成=バージョン番号に利用 |
090 | set strDateNo to doGetDateNo("yyyyMMdd") as text |
091 | set numDateNo to strDateNo as integer |
092 | ocidPayloadDict's setValue:(numDateNo) forKey:("PayloadVersion") |
093 | #PayloadIdentifier |
094 | set strSetValue to ("" & strTypeID & "." & strUUID & "") as text |
095 | ocidPayloadDict's setValue:(strSetValue) forKey:("PayloadIdentifier") |
096 | #PayloadDescription |
097 | ocidPayloadDict's setValue:("TCC: プライバシー設定 アクセス制御ので権限管理で有効(許可)を設定します") forKey:("PayloadDescription") |
098 | #PayloadDisplayName |
099 | |
100 | #ServicesDICT |
101 | set ocidServicesDict to refMe's NSMutableDictionary's alloc()'s init() |
102 | |
103 | |
104 | #選んだサービスの数だけ繰り返し |
105 | repeat with itemService in listMakeService |
106 | set strServiceName to itemService as text |
107 | #ServicesARRAY |
108 | set ocidServicesArray to refMe's NSMutableArray's alloc()'s init() |
109 | |
110 | # |
111 | #処理分岐 |
112 | if strServiceName is "AppleEvents" then |
113 | #サブルーチンで処理 |
114 | set {strReceiverBundleID, strAppFilePath} to doGetBundleID() |
115 | set strReceiverCodesign to doGetCodesign(strAppFilePath) |
116 | # |
117 | set ocidItemDict to refMe's NSMutableDictionary's alloc()'s init() |
118 | (ocidItemDict's setValue:("Allow") forKey:("Authorization")) |
119 | (ocidItemDict's setValue:(true) forKey:("StaticCode")) |
120 | (ocidItemDict's setValue:("bundleID") forKey:("IdentifierType")) |
121 | set strSetValue to ("Services:" & strServiceName & " BundleID: " & strBundleID & "") as text |
122 | (ocidItemDict's setValue:(strSetValue) forKey:("Comment")) |
123 | (ocidItemDict's setValue:(strCodesign) forKey:("CodeRequirement")) |
124 | (ocidItemDict's setValue:(strBundleID) forKey:("Identifier")) |
125 | # |
126 | (ocidItemDict's setValue:(strReceiverCodesign) forKey:("AEReceiverCodeRequirement")) |
127 | (ocidItemDict's setValue:(strReceiverBundleID) forKey:("AEReceiverIdentifier")) |
128 | (ocidItemDict's setValue:("bundleID") forKey:("AEReceiverIdentifierType")) |
129 | |
130 | |
131 | else if strServiceName is "ListenEvent" or itemService is "ScreenCapture" then |
132 | set ocidItemDict to refMe's NSMutableDictionary's alloc()'s init() |
133 | (ocidItemDict's setValue:("AllowStandardUserToSetSystemService") forKey:("Authorization")) |
134 | (ocidItemDict's setValue:(true) forKey:("StaticCode")) |
135 | (ocidItemDict's setValue:("bundleID") forKey:("IdentifierType")) |
136 | set strSetValue to ("Services:" & strServiceName & " BundleID: " & strBundleID & "") as text |
137 | (ocidItemDict's setValue:(strSetValue) forKey:("Comment")) |
138 | (ocidItemDict's setValue:(strCodesign) forKey:("CodeRequirement")) |
139 | (ocidItemDict's setValue:(strBundleID) forKey:("Identifier")) |
140 | else |
141 | set ocidItemDict to refMe's NSMutableDictionary's alloc()'s init() |
142 | (ocidItemDict's setValue:("Allow") forKey:("Authorization")) |
143 | (ocidItemDict's setValue:(true) forKey:("StaticCode")) |
144 | (ocidItemDict's setValue:("bundleID") forKey:("IdentifierType")) |
145 | set strSetValue to ("Services:" & strServiceName & " BundleID: " & strBundleID & "") as text |
146 | (ocidItemDict's setValue:(strSetValue) forKey:("Comment")) |
147 | (ocidItemDict's setValue:(strCodesign) forKey:("CodeRequirement")) |
148 | (ocidItemDict's setValue:(strBundleID) forKey:("Identifier")) |
149 | end if |
150 | |
151 | (ocidServicesArray's addObject:(ocidItemDict)) |
152 | (ocidServicesDict's setObject:(ocidServicesArray) forKey:(strServiceName)) |
153 | |
154 | end repeat |
155 | # |
156 | (ocidPayloadDict's setObject:(ocidServicesDict) forKey:("Services")) |
157 | |
158 | |
159 | set ocidSetPayloadArray to refMe's NSMutableArray's alloc()'s init() |
160 | ocidSetPayloadArray's addObject:(ocidPayloadDict) |
161 | |
162 | ########################## |
163 | #PayloadContent部分 |
164 | #初期化 |
165 | set ocidPayloadContentDict to refMe's NSMutableDictionary's alloc()'s init() |
166 | #PayloadOrganization |
167 | set strDomainName to ("com.cocolog-nifty.quicktimer") as text |
168 | ocidPayloadContentDict's setValue:(strDomainName) forKey:("PayloadOrganization") |
169 | #PayloadUUID |
170 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
171 | set ocidUUID to ocidUUID's UUIDString() |
172 | ocidPayloadContentDict's setValue:(ocidUUID) forKey:("PayloadUUID") |
173 | #PayloadIdentifier |
174 | set strSetValue to (strDomainName & "." & ocidUUID) as text |
175 | ocidPayloadContentDict's setValue:(strSetValue) forKey:("PayloadIdentifier") |
176 | #日付テキストの生成=バージョン番号に利用 |
177 | set strDateNo to doGetDateNo("yyyyMMdd") as text |
178 | set numDateNo to strDateNo as integer |
179 | ocidPayloadContentDict's setValue:(numDateNo) forKey:("PayloadVersion") |
180 | #TargetDeviceType |
181 | ocidPayloadContentDict's setValue:(5) forKey:("TargetDeviceType") |
182 | #PayloadType |
183 | ocidPayloadContentDict's setValue:("Configuration") forKey:("PayloadType") |
184 | #PayloadScope |
185 | ocidPayloadContentDict's setValue:("System") forKey:("PayloadScope") |
186 | #PayloadDescription |
187 | ocidPayloadContentDict's setValue:("TCC: プライバシー設定 アクセス制御ので権限管理で有効(許可)を設定します") forKey:("PayloadDescription") |
188 | #PayloadDisplayName |
189 | set strSetValue to ("TCC." & strBundleID & "") as text |
190 | ocidPayloadContentDict's setValue:(strSetValue) forKey:("PayloadDisplayName") |
191 | #ConsentText |
192 | set ocidContentText to refMe's NSMutableDictionary's alloc()'s init() |
193 | ocidContentText's setValue:("TCC プライバシー設定 アクセス制御ので権限管理で有効(許可)を設定します") forKey:("default") |
194 | ocidPayloadContentDict's setObject:(ocidContentText) forKey:("ConsentText") |
195 | # |
196 | ocidPayloadContentDict's setObject:(ocidSetPayloadArray) forKey:("PayloadContent") |
197 | |
198 | # |
199 | |
200 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
201 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPayloadContentDict) format:(ocidFormat) options:0 |error| :(reference) |
202 | if (item 2 of listResponse) = (missing value) then |
203 | set ocidPlistData to (item 1 of listResponse) |
204 | else if (item 2 of listResponse) ≠ (missing value) then |
205 | set strErrorNO to (item 2 of listResponse)'s code() as text |
206 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
207 | current application's NSLog("■:" & strErrorNO & strErrorMes) |
208 | return "エラーしました" & strErrorNO & strErrorMes |
209 | end if |
210 | |
211 | |
212 | #拡張子はお好みで |
213 | set strSaveFileName to ("" & strBundleID & "." & itemService & ".xml") as text |
214 | set strSaveFileName to ("" & strBundleID & "." & itemService & ".plist") as text |
215 | |
216 | #保存先 |
217 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
218 | #保存 |
219 | set ocidOption to (current application's NSDataWritingAtomic) |
220 | set listDone to ocidPlistData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference) |
221 | if (item 1 of listDone) is true then |
222 | log "正常終了" |
223 | else if (item 1 of listDone) is false then |
224 | set strErrorNO to (item 2 of listResponse)'s code() as text |
225 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
226 | current application's NSLog("■:" & strErrorNO & strErrorMes) |
227 | return "エラーしました" & strErrorNO & strErrorMes |
228 | end if |
229 | |
230 | |
231 | set strSaveFileName to ("" & strBundleID & "." & itemService & ".mobileconfig") as text |
232 | set ocidMobileconfigFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
233 | |
234 | # |
235 | set listDone to (appFileManager's trashItemAtURL:(ocidMobileconfigFilePathURL) resultingItemURL:(ocidMobileconfigFilePathURL) |error| :(reference)) |
236 | # |
237 | set listDone to (appFileManager's copyItemAtURL:(ocidSaveFilePathURL) toURL:(ocidMobileconfigFilePathURL) |error| :(reference)) |
238 | if (item 1 of listDone) is true then |
239 | log "正常終了" |
240 | else if (item 1 of listDone) is false then |
241 | set strErrorNO to (item 2 of listDone)'s code() as text |
242 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
243 | current application's NSLog("■:" & strErrorNO & strErrorMes) |
244 | return "エラーしました" & strErrorNO & strErrorMes |
245 | end if |
246 | |
247 | |
248 | |
249 | #保存先を開く |
250 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
251 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
252 | |
253 | |
254 | return |
255 | ########################## |
256 | # コード署名の取得 |
257 | to doGetCodesign(argAppFilePath) |
258 | |
259 | set strCommandText to ("/usr/bin/codesign -dr - \"" & argAppFilePath & "\"") as text |
260 | set {boolDone, strResnponse} to doZshShellScript(strCommandText) |
261 | #署名を必要な部分のみにしておく |
262 | set ocidResnponse to refMe's NSString's stringWithString:(strResnponse) |
263 | set ocidResnponse to (ocidResnponse's stringByReplacingOccurrencesOfString:("designated => ") withString:("")) |
264 | set ocidResnponse to (ocidResnponse's stringByReplacingOccurrencesOfString:(")") withString:("")) |
265 | set ocidResnponse to (ocidResnponse's stringByReplacingOccurrencesOfString:("(") withString:("")) |
266 | set strCodesign to ocidResnponse as text |
267 | log strCodesign as text |
268 | |
269 | return strCodesign |
270 | end doGetCodesign |
271 | |
272 | ########################## |
273 | # バンドルID |
274 | to doGetBundleID() |
275 | |
276 | #ダイアログ前面に |
277 | set strName to (name of current application) as text |
278 | if strName is "osascript" then |
279 | tell application "SystemUIServer" to activate |
280 | else |
281 | tell current application to activate |
282 | end if |
283 | #デフォルト |
284 | set aliasApplicationDir to (path to applications folder from local domain) as alias |
285 | #ダイアログ呼び出し |
286 | tell application "SystemUIServer" |
287 | activate |
288 | set aliasAppFilePath to (choose file with prompt "アプリケーションを選んでください" default location aliasApplicationDir of type {"com.apple.application-bundle"} with invisibles without showing package contents and multiple selections allowed) as alias |
289 | end tell |
290 | #アプリケーションのパス |
291 | set strAppFilePath to (POSIX path of aliasAppFilePath) as text |
292 | set ocidAppFilePathStr to refMe's NSString's stringWithString:(strAppFilePath) |
293 | set ocidAppFilePath to ocidAppFilePathStr's stringByStandardizingPath() |
294 | set ocidAppFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidAppFilePath) isDirectory:(false) |
295 | #アプリケーションからバンドルID |
296 | set ocidBundle to refMe's NSBundle's bundleWithURL:(ocidAppFilePathURL) |
297 | set ocidBundleID to ocidBundle's bundleIdentifier() |
298 | set strBundleID to (ocidBundleID's lowercaseString()) as string |
299 | log strBundleID as text |
300 | return {strBundleID, strAppFilePath} |
301 | end doGetBundleID |
302 | |
303 | |
304 | ########################## |
305 | # 【N】ZSH 実行 |
306 | to doZshShellScript(argCommandText) |
307 | set strCommandText to argCommandText as text |
308 | log "コマンド開始\r" & strCommandText & "\r" |
309 | set strExec to ("/bin/zsh -c '" & strCommandText & "'") as text |
310 | ########## |
311 | #コマンド実行 |
312 | try |
313 | set strResnponse to (do shell script strExec) as text |
314 | log "コマンド終了" |
315 | on error |
316 | return {false, strResnponse} |
317 | end try |
318 | return {true, strResnponse} |
319 | end doZshShellScript |
320 | |
321 | ########################## |
322 | # 日付テキスト |
323 | to doGetDateNo(strDateFormat) |
324 | ####日付情報の取得 |
325 | set ocidDate to current application's NSDate's |date|() |
326 | ###日付のフォーマットを定義 |
327 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
328 | ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
329 | ocidNSDateFormatter's setDateFormat:strDateFormat |
330 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate |
331 | set strDateAndTime to ocidDateAndTime as text |
332 | return strDateAndTime |
333 | end doGetDateNo |
334 | |
335 | |
336 | |
337 | |
338 | |
AppleScriptで生成しました |
| 固定リンク