| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | コマンドライン用評価リンクメーカー |
| 007 | 主にワンライナー用 |
| 008 |
|
| 009 | 自分が作成したコマンド例をユーザーが簡易に安全性の確認をできるようにしています |
| 010 |
|
| 011 | v0 テスト開始 |
| 012 | v1 エンコードを数値指定しないとエラーになるのに対応して とりあえずOK公開版 |
| 013 |
|
| 014 |
|
| 015 | com.cocolog-nifty.quicktimer.icefloe *) |
| 016 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 017 | use AppleScript version "2.8" |
| 018 | use framework "Foundation" |
| 019 | use framework "AppKit" |
| 020 | use framework "UniformTypeIdentifiers" |
| 021 | use scripting additions |
| 022 |
|
| 023 | property refMe : a reference to current application |
| 024 |
|
| 025 | ############################## |
| 026 | # クリップボードの中身取り出し |
| 027 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
| 028 | set ocidPastBoardTypeArray to appPasteboard's types() |
| 029 | set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType) |
| 030 | if (boolContain as boolean) is true then |
| 031 | set ocidString to appPasteboard's stringForType:(refMe's NSStringPboardType) |
| 032 | set strDefaultAnswer to ocidString as text |
| 033 | else |
| 034 | set strDefaultAnswer to ("コマンド入力ワンライナーで") as text |
| 035 | end if |
| 036 |
|
| 037 | ############################## |
| 038 | # ダイアログ |
| 039 | set strIconFilePath to ("/System/Applications/Utilities/Terminal.app/Contents/Resources/Terminal.icns") as text |
| 040 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 041 | set strMsg to ("テキスト(コマンド部分のみ)を入力してください" & return & "URLに変換します") as text |
| 042 | set strTitle to ("入力してください") as text |
| 043 | set strOK to ("OK") as text |
| 044 | set strCancel to ("キャンセル") as text |
| 045 | set listBotton to {strOK, strCancel} as list |
| 046 | try |
| 047 | tell application "System Events" |
| 048 | activate |
| 049 | set recordResult to (display dialog strMsg with title strTitle default answer strDefaultAnswer buttons listBotton default button strOK cancel button strCancel with icon aliasIconFilePath giving up after 20 without hidden answer) as record |
| 050 | end tell |
| 051 | on error strErrMsg number numErrNo |
| 052 | log strErrMsg & numErrNo |
| 053 | return false |
| 054 | end try |
| 055 | try |
| 056 | |
| 057 | end try |
| 058 | set strResponseButton to (button returned of recordResult) as text |
| 059 | set boolGaveUp to (gave up of recordResult) as boolean |
| 060 | if boolGaveUp is true then |
| 061 | log "時間切れ" |
| 062 | return false |
| 063 | else if strResponseButton is strOK then |
| 064 | set strReturnedText to (text returned of recordResult) as text |
| 065 | end if |
| 066 | ############################## |
| 067 | # 改行だけ置換しておく |
| 068 | set strLF to ("" & linefeed & "") as text |
| 069 | set strCR to ("" & return & "") as text |
| 070 | set strCRLF to ("" & return & linefeed & "") as text |
| 071 | set strSpace to ("" & space & "") as text |
| 072 | set strTab to ("" & tab & "") as text |
| 073 |
|
| 074 | set strDelim to AppleScript's text item delimiters |
| 075 | set AppleScript's text item delimiters to strCR |
| 076 | set listReturnText to every text item of strReturnedText |
| 077 | set AppleScript's text item delimiters to strLF |
| 078 | set strReturn to listReturnText as text |
| 079 | set AppleScript's text item delimiters to strDelim |
| 080 |
|
| 081 | ############################## |
| 082 | # クエリー部分生成 |
| 083 | set strBase to ("このコードの安全性を評価してください。" & strLF & "- 何をするコマンドか?" & strLF & "- 危険性の有無と安全性の評価" & strLF & "- 初心者が実行してよいかを★5で評価してください" & strLF & "- コードはそのまま実行してよいか?" & strLF & "コード:" & strLF & "") as text |
| 084 | set strQuery to ("" & strBase & strReturn & "") as text |
| 085 | #文末改行は削除する |
| 086 | set ocidQueryString to refMe's NSMutableString's stringWithString:(strQuery) |
| 087 | set boolHasSuf to (ocidQueryString's hasSuffix:(strLF)) |
| 088 | if boolHasSuf is true then |
| 089 | set ocidCharSet to refMe's NSCharacterSet's newlineCharacterSet() |
| 090 | set ocidQueryString to ocidQueryString's stringByTrimmingCharactersInSet:(ocidCharSet) |
| 091 | end if |
| 092 |
|
| 093 | ############################## |
| 094 | # URL生成 |
| 095 | #URLコンポーネント初期化 |
| 096 | set ocidURLComponents to refMe's NSURLComponents's alloc()'s init() |
| 097 | ###スキーム を追加 |
| 098 | ocidURLComponents's setScheme:("https") |
| 099 | ###ホストを追加 |
| 100 | ocidURLComponents's setHost:("chat.openai.com") |
| 101 | ###パスを追加 |
| 102 | ocidURLComponents's setPath:("/") |
| 103 | ##クエリー部 |
| 104 | set ocidQueryItems to refMe's NSMutableArray's alloc()'s init() |
| 105 | ocidQueryItems's addObject:(refMe's NSURLQueryItem's alloc()'s initWithName:("q") value:(ocidQueryString)) |
| 106 | ##クエリーをセットする |
| 107 | ocidURLComponents's setQueryItems:(ocidQueryItems) |
| 108 | ##URLに戻して テキストにしておく |
| 109 | set ocidOpenURL to ocidURLComponents's |URL|() |
| 110 | set strURL to ocidOpenURL's absoluteString() as text |
| 111 | log strURL |
| 112 |
|
| 113 | ############################## |
| 114 | # |
| 115 | set strAns to (strURL) as text |
| 116 | set strAns to (strAns & strCR & strCR & strCR & "【このコード安全性をChatGPTで確認する】") as text |
| 117 | set strAelement to ("【<a href=\"" & strURL & "\" target=\"_blank\" rel=\"noopener noreferrer\">このコードの安全性をChatGPTで確認する</a>】") as text |
| 118 | set strAns to (strAns & strCR & strCR & strCR & strAelement & "") as text |
| 119 |
|
| 120 | set strDefaultAnswer to strAns as text |
| 121 |
|
| 122 | ############################## |
| 123 | # ダイアログ |
| 124 | set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text |
| 125 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 126 | set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text |
| 127 | set strTitle to ("処理の戻り値です") as text |
| 128 | set strOK to ("ファイルに保存する") as text |
| 129 | set strCancel to ("キャンセル") as text |
| 130 | set strCopyToClipboard to ("クリップボードにコピー") as text |
| 131 | set listBotton to {strCopyToClipboard, strCancel, strOK} as list |
| 132 | try |
| 133 | tell application "System Events" |
| 134 | activate |
| 135 | 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 120 without hidden answer) as record |
| 136 | end tell |
| 137 | on error strErrMsg number numErrNo |
| 138 | log strErrMsg & numErrNo |
| 139 | return false |
| 140 | end try |
| 141 |
|
| 142 | set strResponseButton to (button returned of recordResult) as text |
| 143 | set boolGaveUp to (gave up of recordResult) as boolean |
| 144 | if boolGaveUp is true then |
| 145 | log "時間切れ" |
| 146 | return false |
| 147 | end if |
| 148 | set strResponseText to (text returned of recordResult) as text |
| 149 | if strResponseButton is strOK then |
| 150 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 151 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 152 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 153 | set aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 154 | set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text |
| 155 | set aliasIconFilePath to (POSIX file strIconFilePath) as alias |
| 156 | # |
| 157 | set strBaseFileName to ("リンクサンプル") as text |
| 158 | set strSaveExtension to ("txt") as text |
| 159 | set strDefaultFileName to ("" & strBaseFileName & "." & strSaveExtension & "") as text |
| 160 | set strMsg to ("名前を決めてください" & return & "拡張子は" & strSaveExtension & "です") as text |
| 161 | set strPromptText to "名前を決めてください" as text |
| 162 | #ファイル名 ダイアログ |
| 163 | tell application "SystemUIServer" |
| 164 | activate |
| 165 | set aliasSaveFilePath to (choose file name strMsg default location aliasDesktopDirPath default name strDefaultFileName with prompt strPromptText) as «class furl» |
| 166 | end tell |
| 167 | #出力パス |
| 168 | set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text |
| 169 | set ocidSaveFilePathStr to refMe's NSString's stringWithString:(strSaveFilePath) |
| 170 | set ocidSaveFilePath to ocidSaveFilePathStr's stringByStandardizingPath() |
| 171 | set ocidSaveFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveFilePath) isDirectory:false) |
| 172 | set ocidContainerDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent() |
| 173 | #拡張子 |
| 174 | set strExtension to (ocidSaveFilePathURL's pathExtension()) as text |
| 175 | #最後のアイテムがファイル名 |
| 176 | set strFileName to (ocidSaveFilePathURL's lastPathComponent()) as text |
| 177 | #拡張子のつけ忘れ対策 |
| 178 | if strFileName does not contain strSaveExtension then |
| 179 | set strFileName to (strFileName & "." & strSaveExtension) as text |
| 180 | set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName) |
| 181 | end if |
| 182 | #保存処理 |
| 183 | set ocidSaveString to refMe's NSMutableString's stringWithString:(strResponseText) |
| 184 | set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return & linefeed) withString:(linefeed)) |
| 185 | set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(return) withString:(linefeed)) |
| 186 | set ocidSaveString to (ocidSaveString's stringByReplacingOccurrencesOfString:(linefeed & linefeed) withString:(linefeed)) |
| 187 | set listDone to ocidSaveString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference) |
| 188 | return (first item of listDone) |
| 189 | else if strResponseButton is strCopyToClipboard then |
| 190 | #NSMutableString's |
| 191 | set ocidElementString to refMe's NSString's alloc()'s initWithString:(strAelement) |
| 192 | #NSDATA |
| 193 | set ocidElementData to ocidElementString's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
| 194 | #RTFテキストに変換して |
| 195 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 196 | ocidAttrDict's setObject:(refMe's NSHTMLTextDocumentType) forKey:(refMe's NSDocumentTypeDocumentAttribute) |
| 197 | ocidAttrDict's setObject:(refMe's NSUTF8StringEncoding as integer) forKey:(refMe's NSCharacterEncodingDocumentAttribute) |
| 198 | # |
| 199 | set ocidOptionDict to refMe's NSMutableDictionary's alloc()'s init() |
| 200 | ocidOptionDict's setObject:(refMe's NSHTMLTextDocumentType) forKey:(refMe's NSDocumentTypeDocumentAttribute) |
| 201 | ocidOptionDict's setObject:(refMe's NSUTF8StringEncoding as integer) forKey:(refMe's NSCharacterEncodingDocumentAttribute) |
| 202 | #変換 |
| 203 | set listResponse to refMe's NSAttributedString's alloc()'s initWithData:(ocidElementData) options:(ocidOptionDict) documentAttributes:(ocidAttrDict) |error|:(reference) |
| 204 | set ocidAttrString to (first item of listResponse) |
| 205 | #データに戻して |
| 206 | set ocidLength to ocidAttrString's |length|() |
| 207 | set ocidRange to refMe's NSRange's NSMakeRange(0, ocidLength) |
| 208 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 209 | ocidAttrDict's setObject:(refMe's NSRTFTextDocumentType) forKey:(refMe's NSDocumentTypeDocumentAttribute) |
| 210 | ocidAttrDict's setObject:(refMe's NSUTF8StringEncoding) forKey:(refMe's NSCharacterEncodingDocumentAttribute) |
| 211 | set ocidRTFdata to ocidAttrString's RTFFromRange:(ocidRange) documentAttributes:(ocidAttrDict) |
| 212 | #ペーストボードに格納する |
| 213 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
| 214 | appPasteboard's clearContents() |
| 215 | if ocidRTFdata = (missing value) then |
| 216 | set ocidText to (refMe's NSString's stringWithString:(strResponseText)) |
| 217 | set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
| 218 | return boolDone |
| 219 | else |
| 220 | set boolDone to appPasteboard's setData:(ocidRTFdata) forType:(refMe's NSPasteboardTypeRTF) |
| 221 | return boolDone |
| 222 | end if |
| 223 | |
| 224 | end if |
| 225 |
|
| 226 | return 0 |