20260605

[AppleScript]サードパーティアプリケーションのコード署名codesignの取得(com.apple.TCC.configuration-profile-policyの作成補助)

[AppleScript]サードパーティアプリケーションのコード署名codesignの取得(com.apple.TCC.configuration-profile-policyの作成補助)

NOTE記事一覧ですnote.com
 

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

CodeRequirement.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7
004(*
005
006PPPC設定に必要な
007サードパーティ製アプリケーションの署名を取得します
008
009v1 初回作成
010v1.1 こまごま今風に直した
011
012
013com.cocolog-nifty.quicktimer.icefloe *)
014----+----1----+----2----+----3----+----4----+----5----+----6----+----7
015use AppleScript version "2.8"
016use framework "Foundation"
017use framework "AppKit"
018use scripting additions
019property refMe : a reference to current application
020
021
022set listUTI to {"com.apple.application-bundle"} as list
023tell application "SystemUIServer"
024   activate
025   set aliasApplicationPath to (choose file with prompt "アプリケーションを選んでください" default location (path to applications folder from user domain) of type listUTI with invisibles without showing package contents and multiple selections allowed) as alias
026end tell
027
028set strAppPath to (POSIX path of aliasApplicationPath) as text
029
030#バンドルID取得と小文字化
031set recordInfoFor to (info for aliasApplicationPath) as record
032set strBundleID to (bundle identifier of recordInfoFor) as text
033set ocidBundleID to refMe's NSString's stringWithString:(strBundleID)
034set strBundleID to (ocidBundleID's lowercaseString()) as string
035
036#パスのエスケープAS記法
037set strAppPath to doPathEscape(strAppPath)
038
039set strCmd to ("/usr/bin/codesign -dr - \"" & strAppPath & "\"") as text
040set strcodesign to (do shell script strCmd) as text
041set strcodesign to doReplace(strcodesign, "designated => ", "")
042set strcodesign to doReplace(strcodesign, "(", "")
043set strcodesign to doReplace(strcodesign, ")", "")
044
045##set strTCC to "<dict>" & linefeed & "<key>Allowed</key>" & linefeed & "<true/>" & linefeed & "<key>Identifier</key>" & linefeed & "<string>" & strCapText & "</string>" & linefeed & "<key>IdentifierType</key>" & linefeed & "<string>bundleID</string>" & linefeed & "<key>Comment</key>" & linefeed & "<string>Services " & strCapText & "</string>" & linefeed & "<key>CodeRequirement</key>" & linefeed & "<string>" & strcodesign & "</string>" & linefeed & "</dict>" & linefeed & ""
046##set strTCC to "<dict>" & linefeed & "<key>Allowed</key>" & linefeed & "<true/>" & linefeed & "<key>Identifier</key>" & linefeed & "<string>" & strCapText & "</string>" & linefeed & "<key>IdentifierType</key>" & linefeed & "<string>bundleID</string>" & linefeed & "<key>Comment</key>" & linefeed & "<string>Services " & strCapText & "</string>" & linefeed & "<key>CodeRequirement</key>" & linefeed & "<string>" & strcodesign & "</string>" & linefeed & "<key>StaticCode</key>" & linefeed & "<true/>" & linefeed & "</dict>" & linefeed & ""
047##set strTCC to "<dict>" & linefeed & "<key>Allowed</key>" & linefeed & "<true/>" & linefeed & "<key>Identifier</key>" & linefeed & "<string>" & strCapText & "</string>" & linefeed & "<key>IdentifierType</key>" & linefeed & "<string>bundleID</string>" & linefeed & "<key>Comment</key>" & linefeed & "<string>Services " & strCapText & "</string>" & linefeed & "<key>CodeRequirement</key>" & linefeed & "<string>" & strcodesign & "</string>" & linefeed & "<key>StaticCode</key>" & linefeed & "<true/>" & linefeed & "<key>Authorization</key>" & linefeed & "<string>AllowStandardUserToSetSystemService</string>" & linefeed & "</dict>" & linefeed & ""
048
049
050set strTCC to "<dict>" & linefeed & "<key>Allowed</key>" & linefeed & "<true/>" & linefeed & "<key>Identifier</key>" & linefeed & "<string>" & strBundleID & "</string>" & linefeed & "<key>IdentifierType</key>" & linefeed & "<string>bundleID</string>" & linefeed & "<key>Comment</key>" & linefeed & "<string>Services " & strBundleID & "</string>" & linefeed & "<key>CodeRequirement</key>" & linefeed & "<string>" & strcodesign & "</string>" & linefeed & "" & linefeed & "###########ここからAppleEvent用####通常は削除####" & linefeed & "" & linefeed & "<key>AEReceiverIdentifierType</key>" & linefeed & "<string>bundleID</string>" & linefeed & "<key>AEReceiverIdentifier</key>" & linefeed & "<string>" & strBundleID & "</string>" & linefeed & "<key>AEReceiverCodeRequirement</key>" & linefeed & "<string>" & strcodesign & "</string>n" & linefeed & "###########ここまでAppleEvent用####通常は削除####" & linefeed & "" & linefeed & "</dict>" & linefeed & ""
051
052set strDefaultAnswer to ("" & strCmd & linefeed & linefeed & strcodesign & linefeed & linefeed & strTCC & "") as text
053
054##############################
055#   ダイアログ
056set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text
057set aliasIconFilePath to (POSIX file strIconFilePath) as alias
058set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text
059set strTitle to ("処理の戻り値です") as text
060set strOK to ("ファイルに保存する") as text
061set strCancel to ("キャンセル") as text
062set strCopyToClipboard to ("クリップボードにコピー") as text
063set listBotton to {strCopyToClipboard, strCancel, strOK} as list
064try
065   tell application "System Events"
066      activate
067      set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listBotton default button strCopyToClipboard cancel button strCancel with icon aliasIconFilePath giving up after 20 without hidden answer) as record
068   end tell
069on error strErrMsg number numErrNo
070   log strErrMsg & numErrNo
071   return false
072end try
073
074set strResponseButton to (button returned of recordResult) as text
075set boolGaveUp to (gave up of recordResult) as boolean
076if boolGaveUp is true then
077   log "時間切れ"
078   return false
079end if
080set strResponseText to (text returned of recordResult) as text
081if strResponseButton is strOK then
082   set appFileManager to refMe's NSFileManager's defaultManager()
083   set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
084   set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
085   set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
086   set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text
087   set aliasIconFilePath to (POSIX file strIconFilePath) as alias
088   #
089   set strBaseFileName to ("CodeRequirement") as text
090   set strSaveExtension to ("plist.txt") as text
091   set strDefaultFileName to ("" & strBaseFileName & "." & strSaveExtension & "") as text
092   set strMsg to ("名前を決めてください" & return & "拡張子は" & strSaveExtension & "です") as text
093   set strPromptText to "名前を決めてください" as text
094   #ファイル名 ダイアログ
095   tell application "SystemUIServer"
096      activate
097      set aliasSaveFilePath to (choose file name strMsg default location aliasDesktopDirPath default name strDefaultFileName with prompt strPromptText) as «class furl»
098   end tell
099   #出力パス
100   set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text
101   set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath)
102   set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath()
103   set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false)
104   set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
105   #拡張子
106   set strExtension to (ocidSaveFilePathURL's pathExtension()) as text
107   #最後のアイテムがファイル名
108   set strFileName to (ocidSaveFilePathURL's lastPathComponent()) as text
109   #拡張子のつけ忘れ対策
110   if strFileName does not contain strSaveExtension then
111      set strFileName to (strFileName & "." & strSaveExtension) as text
112      set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
113   end if
114   #保存処理
115   set ocidSaveString to refMe's NSMutableString's stringWithString:(strResponseText)
116   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed))
117   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return) withString:(linefeed))
118   set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed))
119   set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
120   return (first item of listDone)
121else if strResponseButton is strCopyToClipboard then
122   #ペーストボードに格納する
123   set appPasteboard to refMe's NSPasteboard's generalPasteboard()
124   set ocidText to (refMe's NSString's stringWithString:(strResponseText))
125   appPasteboard's clearContents()
126   set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
127   return boolDone
128end if
129
130return 0
131
132
133########################
134#パスのエスケープ
135to doPathEscape(strFilePath)
136   
137   set listEscChar to {"\\", "\"", "$", "`"} as list
138   repeat with itemEscChar in listEscChar
139      set strSearchText to (itemEscChar) as text
140      set strReplaceText to ("\\" & itemEscChar & "") as text
141      set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
142   end repeat
143   
144   set strSearchText to ("!") as text
145   set strReplaceText to ("\"'!'\"") as text
146   set strFilePath to doReplace(strFilePath, strSearchText, strReplaceText)
147   
148   return strFilePath
149end doPathEscape
150########################
151#置換
152to doReplace(argOrignalText, argSearchText, argReplaceText)
153   set strDelim to AppleScript's text item delimiters
154   set AppleScript's text item delimiters to argSearchText
155   set listDelim to every text item of argOrignalText
156   set AppleScript's text item delimiters to argReplaceText
157   set strReturn to listDelim as text
158   set AppleScript's text item delimiters to strDelim
159   return strReturn
160end doReplace
161
AppleScriptで生成しました