NetWork

[networkQuality]ネットワークテスト (ファイルに残せるようにした)


AppleScript サンプルコード

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

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
008use AppleScript version "2.8"
009use framework "Foundation"
010use framework "AppKit"
011use scripting additions
012property refMe : a reference to current application
013
014
015set strDefaultAnswer to "/usr/bin/networkQuality -v -s -I en0" as text
016
017################################
018######ダイアログ
019################################
020tell current application
021  set strName to name as text
022end tell
023if strName is "osascript" then
024  tell application "Finder" to activate
025else
026  tell current application to activate
027end if
028###
029display alert "コマンドの実行には30秒ほどかかります" as informational giving up after 1
030###表示用アイコン
031set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns" as alias
032try
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)
034on error
035  log "エラーしました"
036  return "エラーしました"
037end try
038if true is equal to (gave up of recordResponse) then
039  return "時間切れですやりなおしてください"
040end if
041if "OK" is equal to (button returned of recordResponse) then
042  set strResponse to (text returned of recordResponse) as text
043else
044  log "キャンセルしました"
045  return "キャンセルしました"
046end if
047
048set strCommandText to (strResponse) as text
049set strResponse to (do shell script strCommandText) as text
050###改行でリスト化
051set AppleScript's text item delimiters to "\r"
052set listResponse to every text item of strResponse
053set AppleScript's text item delimiters to ""
054###出力初期化
055set strOutPutText to "" as text
056set strDownlink to "" as text
057set strUplink to "" as text
058###SUMMARY行以前は出力しない
059set boolSUMMARY to false as boolean
060###出力行の数だけ繰り返し
061repeat 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
074end repeat
075
076###ダイアログへ戻す
077set strMes to ("ネットワーク スピードテスト\r" & strDownlink & "\r" & strUplink & "\r") as text
078
079
080try
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)
092on error
093  log "エラーしました"
094  return
095end try
096if true is equal to (gave up of recordResult) then
097  return "時間切れですやりなおしてください"
098end if
099if "ファイルに保存" 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  
137else 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
150else
151  return "キャンセル"
152end if
153
154
155
156
157
158
159
160##############################
161### 今の日付日間 テキスト
162##############################
163to 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
176end doGetDateNo
AppleScriptで生成しました

|

今参照しているDNS情報を取得する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
property refMe : a reference to current application

##コマンド実行
set strCommandText to ("/usr/sbin/scutil --dns") as text
set strResponse to (do shell script strCommandText) as text
#戻り値をテキストに
set ocidReadString to refMe's NSString's stringWithString:(strResponse)
#改行をUNIXに強制
set ocidReadText to (ocidReadString's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n"))
set ocidReadText to (ocidReadText's stringByReplacingOccurrencesOfString:("\r") withString:("\n"))
#改行毎でリストにする
set ocidCharSet to (refMe's NSCharacterSet's newlineCharacterSet)
set ocidLineArray to (ocidReadText's componentsSeparatedByCharactersInSet:(ocidCharSet))
#出力用テキスト
set ocidSaveString to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
#全行処理
repeat with itemArray in ocidLineArray
  #必要な情報のみ出力用テキストに入れていく
  set ocidBool to (itemArray's containsString:("search domain"))
  if ocidBool is (true) then
(ocidSaveString's appendString:(itemArray))
(ocidSaveString's appendString:("\n"))
  end if
  set ocidBool to (itemArray's containsString:("nameserver"))
  if ocidBool is (true) then
(ocidSaveString's appendString:(itemArray))
(ocidSaveString's appendString:("\n"))
  end if
end repeat
set strAns to ocidSaveString as text

###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias


try
  set recordResponse to (display dialog "DNS戻り値" with title "IPアドレス" default answer strAns buttons {"クリップボードにコピー", "OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
log "エラーしました"
return "エラーしました"
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else if button returned of recordResponse is "クリップボードにコピー" then
  set strText to text returned of recordResponse as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  ##結果をペーストボードにテキストで入れる
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
return strText
else
log "キャンセルしました"
return "キャンセルしました" & strText
end if






|

ネットワークスピードテスト(macOS14対応)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# 戻り値がインテントされるようになったのに対応
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application


set strDefaultAnswer to "/usr/bin/networkQuality -v -s -I en0" as text

################################
######ダイアログ
################################
#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
###表示用アイコン
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns" as alias
try
  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)
on error
log "エラーしました"
return "エラーしました"
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else
log "キャンセルしました"
return "キャンセルしました"
end if

set strCommandText to (strResponse) as text
set strResponse to (do shell script strCommandText) as text
###改行でリスト化
set AppleScript's text item delimiters to "\r"
set listResponse to every text item of strResponse
set AppleScript's text item delimiters to ""
###出力初期化
set strOutPutText to "" as text
set strDownlink to "" as text
set strUplink to "" as text
###SUMMARY行以前は出力しない
set boolSUMMARY to false as boolean
###出力行の数だけ繰り返し
repeat with itemLineText in listResponse
  if itemLineText contains "=" then
    set boolSUMMARY to true as boolean
  end if
  if itemLineText contains "Downlink bytes" then
    set strDownlink to itemLineText as text
  end if
  if itemLineText contains "Uplink bytes" then
    set strUplink to itemLineText as text
  end if
  if boolSUMMARY is true then
    set strOutPutText to strOutPutText & itemLineText & "\r" as text
  end if
end repeat

###ダイアログへ戻す
set strMes to ("ネットワーク スピードテスト\r" & strDownlink & "\r" & strUplink & "\r") as text

try
  #####ダイアログを前面に
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  set recordResult to (display dialog strMes with title "ネットワーク スピードテスト" default answer strOutPutText buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
log "エラーしました"
return
end try
if true is equal to (gave up of recordResult) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResult) then
  set theResponse to (text returned of recordResult) as text
else if button returned of recordResult is "クリップボードにコピー" then
  try
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
  on error
    tell application "Finder"
      set the clipboard to strTitle as text
    end tell
  end try
else
return "キャンセル"
end if





|

ping実行例


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


##通常
set strHostName to ("news.yahoo.co.jp") as text

try
  ##オプリョン -c 1 1回だけ送信 -t 2 タイムアウト2秒
  set strCommandText to ("/sbin/ping -c 1 \"" & strHostName & "\" -t 2 &> /dev/null") as text
log (do shell script strCommandText) as text
on error strMes number numErrorNo
  if numErrorNo is 2 then
log strMes
log strHostName & "は名前解決は出来ましたがpingに応答しなかった"
  else if numErrorNo is 68 then
log strHostName
log strHostName & "は名前解決できないのでドメイン落ちしています"
  end if
end try

##名前解決できない場合
set strHostName to ("bahoo.co.jp") as text

try
  set strCommandText to ("/sbin/ping -c 1 \"" & strHostName & "\" -t 2 &> /dev/null") as text
log (do shell script strCommandText) as text
on error strMes number numErrorNo
  if numErrorNo is 2 then
log strMes
log strHostName & "は名前解決は出来ましたがpingに応答しなかった"
  else if numErrorNo is 68 then
log strHostName
log strHostName & "は名前解決できないのでドメイン落ちしています"
  end if
end try

##pingに応答しないケース
set strHostName to ("yahoo.hatena.com") as text

try
  set strCommandText to ("/sbin/ping -c 1 \"" & strHostName & "\" -t 2 &> /dev/null") as text
log (do shell script strCommandText) as text
on error strMes number numErrorNo
  if numErrorNo is 2 then
log strMes
log strHostName & "は名前解決は出来ましたがpingに応答しなかった"
  else if numErrorNo is 68 then
log strHostName
log strHostName & "は名前解決できないのでドメイン落ちしています"
  end if
end try



|

UNIXコマンドの実行


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#ファイルに保存の場合『起動時に削除される項目』内にファイルを作成しますので
#再起動時に削除されますので、普通に閉じてOKなズボラ仕様です
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()


#############################
###ダイアログ
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
############
set strIconFilePath to ("/System/Library/CoreServices/Applications/Wireless Diagnostics.app/Contents/Resources/AppIcon.icns") as text
set aliasIconPath to (POSIX file strIconFilePath) as alias
###設定 ダイアログ テキスト
set strMes to ("オプション指定可能です AaDdfLnoQqRrv\n/sbin/ping6も可能です") as text
set strTitle to ("pingを実行します") as text
###デフォルトのコマンド例
set strDefaultAnswer to ("/sbin/ping -c 10 www.yahoo.co.jp") as text
# set strDefaultAnswer to ("/usr/bin/networkQuality -v -I en0") as text
# set strDefaultAnswer to ("/usr/bin/whois www.yahoo.co.jp") as text
# set strDefaultAnswer to ("/usr/bin/nslookup -type=NS www.yahoo.co.jp") as text
# set strDefaultAnswer to ("/usr/sbin/netstat -i") as text

##ダイアログ
set recordResult to (display dialog strMes with title strTitle default answer strDefaultAnswer buttons {"ファイルにする", "ターミナルで実行", "キャンセル"} default button "ターミナルで実行" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
###
delay 0.1
if (gave up of recordResult) is true then
return "時間切れですやりなおしてください"
else if (gave up of recordResult) is false then
log recordResult as record
end if

if (button returned of recordResult) is "ファイルにする" then
  set strCommandText to (text returned of recordResult) as text
  set strResponse to (do shell script strCommandText) as text
  ##戻り値
  set aliasOpenFile to doExportFile(strResponse) as alias
  ##書き出したファイルURL
  set strOpenFilePath to (POSIX path of aliasOpenFile) as text
  set ocidOpenFilePathStr to refMe's NSString's stringWithString:(strOpenFilePath)
  set ocidOpenFilePath to ocidOpenFilePathStr's stringByStandardizingPath()
  set ocidOpenFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidOpenFilePath) isDirectory:false)
  ##ワークスペースで開く
  set boolResults to (appShardWorkspace's openURL:ocidOpenFilePathURL)
  ##エラーしたらファインダーで開くのを試してみる
  if boolResults is false then
    tell application "Finder"
make new Finder window to aliasFilePathURL
    end tell
  end if
  
return "ファイルにするを実行しました"
else if (button returned of recordResult) is "ターミナルで実行" then
  set strCommandText to (text returned of recordResult) as text
  ###【1】Terminalを全面に
  tell application "Terminal"
activate
  end tell
  ###【2】ウィンドウの数で状態を調べて
  repeat 5 times
    tell application "Terminal"
      set numCntWindow to (count of every window) as integer
    end tell
    if numCntWindow = 0 then
log "window=0はTerminal起動中なので起動を待つ"
delay 0.5
    else if numCntWindow = 1 then
log "window=1は起動済み 全面Terminal無しなので新規windowを生成"
      tell application "Terminal"
do script "\n"
      end tell
      exit repeat
    else
log "Window>1はすでに全面にWindowがある windowがbusy=コマンド実行中か?はこの時点では不明"
      exit repeat
    end if
  end repeat
  ###【3】前面ウィンドウがbusyかを調べる
  tell application "Terminal"
    tell front window
      tell selected tab
        set boolBusy to busy as boolean
      end tell
    end tell
  end tell
  if boolBusy is true then
log "前面Windowコマンド実行中=新規Windowを作成"
    tell application "Terminal"
do script "\n"
    end tell
  else
log "前面Window利用可能"
  end if
  ###【4】全面ウィンドウでコマンドを実行する
  tell application "Terminal"
do script (strCommandText) in selected tab of front window
return "ターミナルで実行"
  end tell
end if



##############################
### テンポラリにテキストファイル
##############################
to doExportFile(argText)
  ##渡されたテキスト
  set strText to argText as text
  set ocidText to refMe's NSString's stringWithString:(strText)
  set ocidReadTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidReadTextM's setString:(ocidText)
  ##改行をUNIX LFに統一
  set ocidReadTextM to ocidReadTextM's stringByReplacingOccurrencesOfString:("\r\n") withString:("\n")
  set ocidReadTextM to ocidReadTextM's stringByReplacingOccurrencesOfString:("\r") withString:("\n")
  ###ディレクトリ
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidTempDirURL to appFileManager's temporaryDirectory()
  ###ユニークになるようにフォルダ名はUUID
  set ocidUUID to refMe's NSUUID's alloc()'s init()
  set ocidUUIDString to ocidUUID's UUIDString
  set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
  ###フォルダを作る
  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
  # 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
  set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
  ###保存するテキストファイルへのパス
  set strTimeNO to doGetDateNo("yyyy-MM-dd")
  set strFileName to (strTimeNO & ".txt") as text
  set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false
  ##保存
  set listDone to ocidReadTextM's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
  ##
  set listDone to appFileManager's setAttributes:(ocidAttrDict) ofItemAtPath:(ocidSaveFilePathURL's |path|) |error|:(reference)
  ###エイリアスでパスを戻す
  set aliasSaveFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
return aliasSaveFilePath
end doExportFile


##############################
### 今の日付日間 テキスト
##############################
to doGetDateNo(argDateFormat)
  ####日付情報の取得
  set ocidDate to current application's NSDate's |date|()
  ###日付のフォーマットを定義
  set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
  set ocidTimeZone to refMe's NSTimeZone's alloc()'s initWithName:"Asia/Tokyo"
ocidNSDateFormatter's setTimeZone:(ocidTimeZone)
ocidNSDateFormatter's setDateFormat:(argDateFormat)
  ###日付フォーマットを適応
  set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate)
  ###テキストに確定させて戻す
  set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo


|

IPアドレス取得

www.cloudflare.com の応答速度の速さが決めて

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set strURL to "https://www.cloudflare.com/cdn-cgi/trace?json" as text
set ocidURL to refMe's NSURL's URLWithString:(strURL)
###URLをリクエスト
set listContents to refMe's NSString's stringWithContentsOfURL:(ocidURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
set ocidContents to item 1 of listContents
###改行で
set ocidCharacterSet to refMe's NSCharacterSet's newlineCharacterSet
###リスト化して
set ocidStringArray to ocidContents's componentsSeparatedByCharactersInSet:(ocidCharacterSet)
###IPアドレスを取得
repeat with itemStringArray in ocidStringArray
  if (itemStringArray as text) starts with "ip" then
    set ocidIPArray to (itemStringArray's componentsSeparatedByString:"=")
    set ocidIPAdd to (ocidIPArray's objectAtIndex:1)
  end if
end repeat
###
set strIPAdd to ocidIPAdd as text
###ホスト名解決
set strCommandText to ("/usr/bin/dig -x " & strIPAdd & " +short")
set strHostName to (do shell script strCommandText) as text

set strCommandText to ("/sbin/ifconfig | grep \"inet.*broadcast\" | awk '{print $2}'") as text
set strLocalIP to (do shell script strCommandText) as text

set strAns to (strHostName & "\r" & strIPAdd & "\r" & strLocalIP & "") as text
##############################
#####ダイアログを前面に出す
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
################################
######ダイアログ
################################
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias
try
  set recordResponse to (display dialog strAns with title "IPアドレス" default answer strAns buttons {"クリップボードにコピー", "OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)
on error
  log "エラーしました"
return "エラーしました"
end try
if true is equal to (gave up of recordResponse) then
return "時間切れですやりなおしてください"
end if
if "OK" is equal to (button returned of recordResponse) then
  set strResponse to (text returned of recordResponse) as text
else if button returned of recordResponse is "クリップボードにコピー" then
  set strText to text returned of recordResponse as text
  ####ペーストボード宣言
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  ##結果をペーストボードにテキストで入れる
  set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
return strText
else
  log "キャンセルしました"
return "キャンセルしました" & strText
end if

log strText as text





|

[SMB]ゲスト接続


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

###接続用のURL
set strURL to ("smb://guest:guest@192.168.0.XXX:445/Shared") as text
###クリップボードに入れておいて
set appPasteboard to refMe's NSPasteboard's generalPasteboard()
appPasteboard's clearContents()
appPasteboard's setString:(strURL) forType:(refMe's NSPasteboardTypeString)

#####ダイアログを前面に
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
##表示プロトコル
set listURL to {"_afpovertcp._tcp", "_smb._tcp", "_cifs_tcp"} as list
##ダイアログ
set strResults to choose URL strURL showing listURL with editable URL
###NSWorkspaceで開く
###そう、ダイアログいらないよね…笑
set ocidURLstr to refMe's NSString's stringWithString:(strResults)
set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLstr)
set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
appShardWorkspace's openURL:(ocidURL)

return

|

ARPのリセットとリセット結果の表示

do shell scriptを利用することで指紋認証使えるのは便利

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application


set strCommandText to "/usr/bin/sudo /usr/sbin/arp -a -d" as text

set strResponse to (do shell script strCommandText with administrator privileges) as text

tell application "TextEdit"
  activate
  properties
  make new document with properties {name:"arp", text:strResponse}
end tell

tell application "TextEdit"
  tell front document
    tell its text
      set its font to "Osaka-mono"
      set its size to 14
    end tell
  end tell
end tell

|

[TOOL]IPアドレスの取得

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application

########################
######ローカルIPアドレスの取得
set objSysInfo to system info
set strLocalIpAdd to (IPv4 address of objSysInfo) as text
set strResponse to "LocalIP: " & strLocalIpAdd & "\n" as text
########################
######グローバルIPGw)の
try
  set strCommandText to "/usr/bin/curl -s api.ipify.org"
  with timeout of 2 seconds
    set strResults to (do shell script strCommandText) as text
  end timeout
on error
  set strCommandText to "/usr/bin/curl -s ifconfig.me"
  with timeout of 10 seconds
    set strResults to (do shell script strCommandText) as text
  end timeout
end try
set strResponse to strResponse & "PublicIP: " & strResults as text
##############################
#####ダイアログを前面に出す
##############################
tell current application
  set strName to name as text
end tell
####スクリプトメニューから実行したら
if strName is "osascript" then
  tell application "Finder"
    activate
  end tell
else
  tell current application
    activate
  end tell
end if
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias
(display dialog strResponse with title "IPアドレス" default answer strResponse buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 20 without hidden answer)

|

[nettop]接続中のネットワークサービスの一覧を出力する

左側にMan出す用意してある自分用

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application


set strCommandText to "/usr/bin/nettop -x -n" as text

tell application "Terminal"
  launch
  activate
  set objTabWindows to do script "\n\n"
  tell objTabWindows
    activate
  end tell
  tell front window
    activate
    set numWidowID to id as integer
  end tell
  
  tell window id numWidowID
    set size to {720, 870}
    set position to {360, 25}
    set origin to {360, 515}
    set frame to {360, 515, 1080, 875}
    set bounds to {360, 25, 1080, 895}
  end tell
  tell objTabWindows
    activate
    do script strCommandText in objTabWindows
  end tell
end tell

set strCommandText to "/usr/bin/man -a nettop"

tell application "Terminal"
  set objTabWindows to do script "\n\n"
  tell front window
    activate
    set numWidowID to id as integer
  end tell
  tell window id numWidowID
    activate
    set size to {360, 870}
    set position to {0, 25}
    set origin to {0, 0}
    set frame to {0, 0, 360, 875}
    set bounds to {0, 25, 360, 900}
  end tell
  tell objTabWindows
    activate
    do script strCommandText in objTabWindows
  end tell
end tell
return

|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom