[networkQuality]ネットワークテスト (ファイルに残せるようにした)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # 戻り値がインテントされるようになったのに対応 |
005 | # 戻り値をファイルに残せるようにした |
006 | #com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "AppKit" |
011 | use scripting additions |
012 | property refMe : a reference to current application |
013 | |
014 | |
015 | set strDefaultAnswer to "/usr/bin/networkQuality -v -s -I en0" as text |
016 | |
017 | ################################ |
018 | ######ダイアログ |
019 | ################################ |
020 | tell current application |
021 | set strName to name as text |
022 | end tell |
023 | if strName is "osascript" then |
024 | tell application "Finder" to activate |
025 | else |
026 | tell current application to activate |
027 | end if |
028 | ### |
029 | display alert "コマンドの実行には30秒ほどかかります" as informational giving up after 1 |
030 | ###表示用アイコン |
031 | set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns" as alias |
032 | try |
033 | set recordResponse to (display dialog "コマンドオプションを入力してください\rOK押してから結果が出るまで30秒ほどかかります" with title "ネットワークスピードテスト" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer) |
034 | on error |
035 | log "エラーしました" |
036 | return "エラーしました" |
037 | end try |
038 | if true is equal to (gave up of recordResponse) then |
039 | return "時間切れですやりなおしてください" |
040 | end if |
041 | if "OK" is equal to (button returned of recordResponse) then |
042 | set strResponse to (text returned of recordResponse) as text |
043 | else |
044 | log "キャンセルしました" |
045 | return "キャンセルしました" |
046 | end if |
047 | |
048 | set strCommandText to (strResponse) as text |
049 | set strResponse to (do shell script strCommandText) as text |
050 | ###改行でリスト化 |
051 | set AppleScript's text item delimiters to "\r" |
052 | set listResponse to every text item of strResponse |
053 | set AppleScript's text item delimiters to "" |
054 | ###出力初期化 |
055 | set strOutPutText to "" as text |
056 | set strDownlink to "" as text |
057 | set strUplink to "" as text |
058 | ###SUMMARY行以前は出力しない |
059 | set boolSUMMARY to false as boolean |
060 | ###出力行の数だけ繰り返し |
061 | repeat with itemLineText in listResponse |
062 | if itemLineText contains "=" then |
063 | set boolSUMMARY to true as boolean |
064 | end if |
065 | if itemLineText contains "Downlink bytes" then |
066 | set strDownlink to itemLineText as text |
067 | end if |
068 | if itemLineText contains "Uplink bytes" then |
069 | set strUplink to itemLineText as text |
070 | end if |
071 | if boolSUMMARY is true then |
072 | set strOutPutText to strOutPutText & itemLineText & "\r" as text |
073 | end if |
074 | end repeat |
075 | |
076 | ###ダイアログへ戻す |
077 | set strMes to ("ネットワーク スピードテスト\r" & strDownlink & "\r" & strUplink & "\r") as text |
078 | |
079 | |
080 | try |
081 | #####ダイアログを前面に |
082 | tell current application |
083 | set strName to name as text |
084 | end tell |
085 | ####スクリプトメニューから実行したら |
086 | if strName is "osascript" then |
087 | tell application "Finder" to activate |
088 | else |
089 | tell current application to activate |
090 | end if |
091 | set recordResult to (display dialog strMes with title "ネットワーク スピードテスト" default answer strOutPutText buttons {"クリップボードにコピー", "キャンセル", "ファイルに保存"} default button "ファイルに保存" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer) |
092 | on error |
093 | log "エラーしました" |
094 | return |
095 | end try |
096 | if true is equal to (gave up of recordResult) then |
097 | return "時間切れですやりなおしてください" |
098 | end if |
099 | if "ファイルに保存" is equal to (button returned of recordResult) then |
100 | set theResponse to (text returned of recordResult) as text |
101 | #### |
102 | ##保存用テキスト |
103 | set ocidSavestring to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
104 | ocidSavestring's appendString:(theResponse) |
105 | #解説付与 |
106 | set ocidSavestring to (ocidSavestring's stringByReplacingOccurrencesOfString:("-\rCapacity:") withString:("-\rCapacity:帯域\rUplink上り Downlink下り")) |
107 | # |
108 | set ocidSavestring to (ocidSavestring's stringByReplacingOccurrencesOfString:("-\rLatency:") withString:("-\rLatency:遅延\rUplink上り Downlink下り\r50ms良好 300ms良 500ms遅延あり 1秒以上遅延")) |
109 | |
110 | set ocidSavestring to (ocidSavestring's stringByReplacingOccurrencesOfString:("Transport-layer info:") withString:("Transport-layer info:ECN輻輳")) |
111 | |
112 | set ocidSavestring to (ocidSavestring's stringByReplacingOccurrencesOfString:("Responsiveness: Low") withString:("Responsiveness: Low 低速")) |
113 | |
114 | set ocidSavestring to (ocidSavestring's stringByReplacingOccurrencesOfString:("Responsiveness: Medium") withString:("Responsiveness: Medium 中速")) |
115 | |
116 | set ocidSavestring to (ocidSavestring's stringByReplacingOccurrencesOfString:("Responsiveness: High") withString:("Responsiveness: High 高速")) |
117 | #保存先フォルダ作成 |
118 | set appFileManager to refMe's NSFileManager's defaultManager() |
119 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDocumentDirectory) inDomains:(refMe's NSUserDomainMask)) |
120 | set ocidDocumentDirPathURL to ocidURLsArray's firstObject() |
121 | set ocidSaveDirPathURL to ocidDocumentDirPathURL's URLByAppendingPathComponent:("Apple/Wifi/networkQuality") isDirectory:(true) |
122 | #フォルダ生成 |
123 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
124 | # 777-->511 755-->493 700-->448 766-->502 |
125 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
126 | set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
127 | #ファイル名 |
128 | set strFileName to doGetDateNo("yyyyMMdd-hhmm") |
129 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false) |
130 | set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:("txt") |
131 | #保存 |
132 | set listDone to ocidSavestring's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference) |
133 | #ファイルを開く |
134 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
135 | set boolDone to appSharedWorkspace's openURL:(ocidSaveFilePathURL) |
136 | |
137 | else if button returned of recordResult is "クリップボードにコピー" then |
138 | try |
139 | set strText to text returned of recordResult as text |
140 | ####ペーストボード宣言 |
141 | set appPasteboard to refMe's NSPasteboard's generalPasteboard() |
142 | set ocidText to (refMe's NSString's stringWithString:(strText)) |
143 | appPasteboard's clearContents() |
144 | appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString) |
145 | on error |
146 | tell application "Finder" |
147 | set the clipboard to strTitle as text |
148 | end tell |
149 | end try |
150 | else |
151 | return "キャンセル" |
152 | end if |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | ############################## |
161 | ### 今の日付日間 テキスト |
162 | ############################## |
163 | to doGetDateNo(argDateFormat) |
164 | ####日付情報の取得 |
165 | set ocidDate to current application's NSDate's |date|() |
166 | ###日付のフォーマットを定義 |
167 | set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init() |
168 | set ocidLocale to current application's NSLocale's localeWithLocaleIdentifier:("ja_JP_POSIX") |
169 | ocidNSDateFormatter's setLocale:(ocidLocale) |
170 | set ocidTimeZone to current application's NSTimeZone's alloc()'s initWithName:("Asia/Tokyo") |
171 | ocidNSDateFormatter's setTimeZone:(ocidTimeZone) |
172 | ocidNSDateFormatter's setDateFormat:(argDateFormat) |
173 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate) |
174 | set strDateAndTime to ocidDateAndTime as text |
175 | return strDateAndTime |
176 | end doGetDateNo |
AppleScriptで生成しました |
| 固定リンク