20260413

コマンド例を書くときに、閲覧者が自分でOpenAIを使ってチェックできるようにする

コマンド例を書くときに、閲覧者が自分でOpenAIを使ってチェックできるようにする

ダウンロードはこちら
NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

コマンドライン用評価リンクメーカー.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
004(*
005
006コマンドライン用評価リンクメーカー
007主にワンライナー用
008
009自分が作成したコマンド例をユーザーが簡易に安全性の確認をできるようにしています
010
011v0 テスト開始
012v1 エンコードを数値指定しないとエラーになるのに対応して とりあえずOK公開版
013
014
015com.cocolog-nifty.quicktimer.icefloe *)
016----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
017use AppleScript version "2.8"
018use framework "Foundation"
019use framework "AppKit"
020use framework "UniformTypeIdentifiers"
021use scripting additions
022
023property refMe : a reference to current application
024
025##############################
026#   クリップボードの中身取り出し
027set appPasteboard to refMe's NSPasteboard's generalPasteboard()
028set ocidPastBoardTypeArray to appPasteboard's types()
029set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSStringPboardType)
030if (boolContain as boolean) is true then
031   set ocidString to appPasteboard's stringForType:(refMe's NSStringPboardType)
032   set strDefaultAnswer to ocidString as text
033else
034   set strDefaultAnswer to ("コマンド入力ワンライナーで") as text
035end if
036
037##############################
038#   ダイアログ
039set strIconFilePath to ("/System/Applications/Utilities/Terminal.app/Contents/Resources/Terminal.icns") as text
040set aliasIconFilePath to (POSIX file strIconFilePath) as alias
041set strMsg to ("テキスト(コマンド部分のみ)を入力してください" & return & "URLに変換します") as text
042set strTitle to ("入力してください") as text
043set strOK to ("OK") as text
044set strCancel to ("キャンセル") as text
045set listBotton to {strOK, strCancel} as list
046try
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
051on error strErrMsg number numErrNo
052   log strErrMsg & numErrNo
053   return false
054end try
055try
056   
057end try
058set strResponseButton to (button returned of recordResult) as text
059set boolGaveUp to (gave up of recordResult) as boolean
060if boolGaveUp is true then
061   log "時間切れ"
062   return false
063else if strResponseButton is strOK then
064   set strReturnedText to (text returned of recordResult) as text
065end if
066##############################
067#   改行だけ置換しておく
068set strLF to ("" & linefeed & "") as text
069set strCR to ("" & return & "") as text
070set strCRLF to ("" & return & linefeed & "") as text
071set strSpace to ("" & space & "") as text
072set strTab to ("" & tab & "") as text
073
074set strDelim to AppleScript's text item delimiters
075set AppleScript's text item delimiters to strCR
076set listReturnText to every text item of strReturnedText
077set AppleScript's text item delimiters to strLF
078set strReturn to listReturnText as text
079set AppleScript's text item delimiters to strDelim
080
081##############################
082#   クエリー部分生成
083set strBase to ("このコードの安全性を評価してください。" & strLF & "- 何をするコマンドか?" & strLF & "- 危険性の有無と安全性の評価" & strLF & "- 初心者が実行してよいかを★5で評価してください" & strLF & "- コードはそのまま実行してよいか?" & strLF & "コード:" & strLF & "") as text
084set strQuery to ("" & strBase & strReturn & "") as text
085#文末改行は削除する
086set ocidQueryString to refMe's NSMutableString's stringWithString:(strQuery)
087set boolHasSuf to (ocidQueryString's hasSuffix:(strLF))
088if boolHasSuf is true then
089   set ocidCharSet to refMe's NSCharacterSet's newlineCharacterSet()
090   set ocidQueryString to ocidQueryString's stringByTrimmingCharactersInSet:(ocidCharSet)
091end if
092
093##############################
094#   URL生成
095#URLコンポーネント初期化
096set ocidURLComponents to refMe's NSURLComponents's alloc()'s init()
097###スキーム を追加
098ocidURLComponents's setScheme:("https")
099###ホストを追加
100ocidURLComponents's setHost:("chat.openai.com")
101###パスを追加
102ocidURLComponents's setPath:("/")
103##クエリー部
104set ocidQueryItems to refMe's NSMutableArray's alloc()'s init()
105ocidQueryItems's addObject:(refMe's NSURLQueryItem's alloc()'s initWithName:("q") value:(ocidQueryString))
106##クエリーをセットする
107ocidURLComponents's setQueryItems:(ocidQueryItems)
108##URLに戻して テキストにしておく
109set ocidOpenURL to ocidURLComponents's |URL|()
110set strURL to ocidOpenURL's absoluteString() as text
111log strURL
112
113##############################
114#
115set strAns to (strURL) as text
116set strAns to (strAns & strCR & strCR & strCR & "【このコード安全性をChatGPTで確認する】") as text
117set strAelement to ("【<a href=\"" & strURL & "\" target=\"_blank\" rel=\"noopener noreferrer\">このコードの安全性をChatGPTで確認する</a>】") as text
118set strAns to (strAns & strCR & strCR & strCR & strAelement & "") as text
119
120set strDefaultAnswer to strAns as text
121
122##############################
123#   ダイアログ
124set strIconFilePath to ("/System/Library/UserNotifications/Bundles/com.apple.identityservicesd.firewall.bundle/Contents/Resources/Apple Care_Mac.icns") as text
125set aliasIconFilePath to (POSIX file strIconFilePath) as alias
126set strMsg to ("テキストの戻り値です" & return & "コピーできます") as text
127set strTitle to ("処理の戻り値です") as text
128set strOK to ("ファイルに保存する") as text
129set strCancel to ("キャンセル") as text
130set strCopyToClipboard to ("クリップボードにコピー") as text
131set listBotton to {strCopyToClipboard, strCancel, strOK} as list
132try
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
137on error strErrMsg number numErrNo
138   log strErrMsg & numErrNo
139   return false
140end try
141
142set strResponseButton to (button returned of recordResult) as text
143set boolGaveUp to (gave up of recordResult) as boolean
144if boolGaveUp is true then
145   log "時間切れ"
146   return false
147end if
148set strResponseText to (text returned of recordResult) as text
149if 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)
189else 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   
224end if
225
226return 0
AppleScriptで生成しました