
[AppleScript]サードパーティアプリケーションのコード署名codesignの取得(com.apple.TCC.configuration-profile-policyの作成補助)
【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7 |
| 004 | (* |
| 005 | |
| 006 | PPPC設定に必要な |
| 007 | サードパーティ製アプリケーションの署名を取得します |
| 008 | |
| 009 | v1 初回作成 |
| 010 | v1.1 こまごま今風に直した |
| 011 | |
| 012 | |
| 013 | com.cocolog-nifty.quicktimer.icefloe *) |
| 014 | ----+----1----+----2----+----3----+----4----+----5----+----6----+----7 |
| 015 | use AppleScript version "2.8" |
| 016 | use framework "Foundation" |
| 017 | use framework "AppKit" |
| 018 | use scripting additions |
| 019 | property refMe : a reference to current application |
| 020 | |
| 021 | |
| 022 | set listUTI to {"com.apple.application-bundle"} as list |
| 023 | tell 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 |
| 026 | end tell |
| 027 | |
| 028 | set strAppPath to (POSIX path of aliasApplicationPath) as text |
| 029 | |
| 030 | #バンドルID取得と小文字化 |
| 031 | set recordInfoFor to (info for aliasApplicationPath) as record |
| 032 | set strBundleID to (bundle identifier of recordInfoFor) as text |
| 033 | set ocidBundleID to refMe's NSString's stringWithString:(strBundleID) |
| 034 | set strBundleID to (ocidBundleID's lowercaseString()) as string |
| 035 | |
| 036 | #パスのエスケープAS記法 |
| 037 | set strAppPath to doPathEscape(strAppPath) |
| 038 | |
| 039 | set strCmd to ("/usr/bin/codesign -dr - \"" & strAppPath & "\"") as text |
| 040 | set strcodesign to (do shell script strCmd) as text |
| 041 | set strcodesign to doReplace(strcodesign, "designated => ", "") |
| 042 | set strcodesign to doReplace(strcodesign, "(", "") |
| 043 | set 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 | |
| 050 | set 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 | |
| 052 | set strDefaultAnswer to ("" & strCmd & linefeed & linefeed & strcodesign & linefeed & linefeed & strTCC & "") as text |
| 053 | |
| 054 | ############################## |
| 055 | # ダイアログ |
| 056 | set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text |
| 057 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 058 | set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text |
| 059 | set strTitle to ("処理の戻り値です") as text |
| 060 | set strOK to ("ファイルに保存する") as text |
| 061 | set strCancel to ("キャンセル") as text |
| 062 | set strCopyToClipboard to ("クリップボードにコピー") as text |
| 063 | set listBotton to {strCopyToClipboard, strCancel, strOK} as list |
| 064 | try |
| 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 |
| 069 | on error strErrMsg number numErrNo |
| 070 | log strErrMsg & numErrNo |
| 071 | return false |
| 072 | end try |
| 073 | |
| 074 | set strResponseButton to (button returned of recordResult) as text |
| 075 | set boolGaveUp to (gave up of recordResult) as boolean |
| 076 | if boolGaveUp is true then |
| 077 | log "時間切れ" |
| 078 | return false |
| 079 | end if |
| 080 | set strResponseText to (text returned of recordResult) as text |
| 081 | if 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) |
| 121 | else 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 |
| 128 | end if |
| 129 | |
| 130 | return 0 |
| 131 | |
| 132 | |
| 133 | ######################## |
| 134 | #パスのエスケープ |
| 135 | to 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 |
| 149 | end doPathEscape |
| 150 | ######################## |
| 151 | #置換 |
| 152 | to 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 |
| 160 | end doReplace |
| 161 | |
| AppleScriptで生成しました | |
