アラートダイアログ上に、接続されているUSBデバイスの情報を表示

アラートダイアログ上にTable Viewを表示 v7_サイズ調整あり
↑ こちらのスクリプトを使わせてもらい、アラートダイアログ上に、システムに接続されたUSBデバイスの情報を表示するスクリプトです。単に確認するためのもので、特別何かをしているわけではありません。

-- http://piyocast.com/as/archives/8678

use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

on run

# システムに接続されているUSBデバイスの情報を取得
set usbDevices to do shell script "system_profiler SPUSBDataType"

# 各行毎に分割してリストに
set deviceList to paragraphs of usbDevices

set aList to {}
repeat with i in deviceList
if (contents of i) is "" then
# 空行に「行間」という文字を挿入
set the end of aList to "行間"
else
# 行頭の空白を削除
set the end of aList to regexReplace((contents of i), "^ +", "") of me
end if
end repeat

#「改行」の区切り文字で区切ってテキストを整形
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set deviceList to aList as text
set AppleScript's text item delimiters to oldDelim

#findText」ハンドラを使い、「行間」の文字がある場所をリストアップ
set idList to findText(deviceList, "行間") of me

#「行間」から「行間」の間のものをにリストアップ
set bList to {}
repeat with i from 1 to ((count of idList) - 1)
set idx to (item i of idList) + 3
set idx2 to (item (i + 1) of idList) - 2
set the end of bList to text idx thru idx2 of deviceList
end repeat

# 行末の「:」を使い、デバイス毎にリストにまとめる
set cList to {}
repeat with i from 1 to (count of bList)
if (item i of bList) ends with ":" then
set the end of cList to (item i of bList) & return & (item (i + 1)) of bList
end if
end repeat

# 各パラメータを抽出し、レコード形式にする。それのリストを生成。
set aTableList to {}
repeat with i from 1 to count of cList
if (item i of cList) contains "Location ID:" then
set info to (paragraphs of item i of cList)
set aName to divText((item 1 of info), {":"}, 1) of me
set aName to regexReplace(aName, " +$", "") of me
repeat with i from 1 to count of info
set lineText to item i of info
if lineText starts with "Speed:" then
set aSpeed to divText(lineText, {": "}, 2) of me
set aSpeed to regexReplace(aSpeed, " +$", "") of me
end if
if lineText starts with "Manufacturer:" then
set theManufacturer to divText(lineText, {": "}, 2) of me
set theManufacturer to regexReplace(theManufacturer, " +$", "") of me
end if
if lineText starts with "Current Available" then
set currentAvailable to (divText(lineText, {": "}, 2) of me) & " (mA)"
set currentAvailable to regexReplace(currentAvailable, " +$", "") of me
end if
if lineText starts with "Current Required" then
set currentRequired to (divText(lineText, {": "}, 2) of me) & " (mA)"
set currentRequired to regexReplace(currentRequired, " +$", "") of me
end if
end repeat
set aTableList's end to {|機器の名称|:aName, |転送速度(最大)|:aSpeed, |メーカー|:theManufacturer, |利用可能な電流|:currentAvailable, |必要な電流|:currentRequired}
end if
end repeat

# 指定した項目名(レコードのkey)でアラートダイアログに表示
set optionRec to {aTableList:aTableList}
set optionRec to optionRec & {aSortOrder:{column1:"機器の名称", column2:"転送速度(最大)", column3:"メーカー", column4:"利用可能な電流", column5:"必要な電流"}}

set aMainMes to "項目の選択"
set aSubMes to "適切なものを以下からえらんでください"
set dateObj to my chooseData(aMainMes, aSubMes, optionRec)
end run

# 与えたテキストを区切り文字で区切って、指定した項目番号のアイテムを返すハンドラ
on divText(aStr, aDelim, itemNum)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to aDelim
set aRes to item itemNum of text items of aStr
set AppleScript's text item delimiters to oldDelim
return aRes
end divText

# アラートダイアログでtableviewを表示
on chooseData(aMainMes, aSubMes, optionRec)
script MyDialog
property parent : AppleScript
use AppleScript
use scripting additions
use framework "Foundation"
property _retrieve_data : missing value
on make
set aClass to me
script
property parent : aClass
end script
end make
## ダイアログの呼び出し
on call(aMainMes, aSubMes, optionRec)
set paramObj to {myMessage:aMainMes, mySubMessage:aSubMes, myOption:optionRec}
parent's performSelectorOnMainThread:"raize:" withObject:paramObj waitUntilDone:true
if (my _retrieve_data) is missing value then error number -128
return (my _retrieve_data)
end call
## ダイアログの生成
on raize:paramObj
###
set mesText to paramObj's myMessage
set infoText to paramObj's mySubMessage
set paramObj to paramObj's myOption
### set up view
set {theView, makeObj} to my makeContentView(paramObj)
### set up alert
tell current application's NSAlert's new()
setMessageText_(mesText)
setInformativeText_(infoText)
addButtonWithTitle_("OK")
addButtonWithTitle_("Cancel")
setAccessoryView_(theView)
tell |window|()
setInitialFirstResponder_(theView)
end tell
#### show alert in modal loop
if runModal() is (current application's NSAlertSecondButtonReturn) then return
end tell
### retrieve date
my retrieveData(makeObj)
end raize:

## ContentView を作成
property _data_source : missing value
on makeContentView(paramObj)
## 準備
set aList to (paramObj's aTableList) as list
set keyRec to (paramObj's aSortOrder) as record
set keyDict to (current application's NSDictionary's dictionaryWithDictionary:keyRec)

set my _data_source to current application's NSArrayController's new()
repeat with anItem in aList
set aDict to (current application's NSDictionary's dictionaryWithDictionary:anItem)
((my _data_source)'s addObject:aDict)
end repeat

## NSTableView
tell current application's NSTableView's alloc()
tell initWithFrame_(current application's CGRectZero)
setAllowsEmptySelection_(false)
setDataSource_(me)
setDelegate_(me)
setDoubleAction_("doubleAction:")
setGridStyleMask_(current application's NSTableViewSolidVerticalGridLineMask)
setSelectionHighlightStyle_(current application's NSTableViewSelectionHighlightStyleRegular)
setTarget_(me)
setUsesAlternatingRowBackgroundColors_(true)

set thisRowHeight to rowHeight() as integer
set aTableObj to it
end tell
end tell

## NSTableColumn
### Columnの並び順を指定する
set viewWidth to 0
set columnsCount to keyDict's |count|()
repeat with colNum from 1 to columnsCount

set keyName to "column" & colNum as text
set aTitle to (keyDict's objectForKey:keyName)

tell (current application's NSTableColumn's alloc()'s initWithIdentifier:(colNum as text))
tell headerCell()
setStringValue_(aTitle)
set thisHeaderHeight to cellSize()'s height
end tell

set aTableColumn to it
end tell

### Columnの横幅を調整
tell aTableObj
addTableColumn_(aTableColumn)
--reloadData()
set colWidthMax to 0
set rowCount to numberOfRows()

repeat with rowNum from 1 to rowCount
tell preparedCellAtColumn_row_(colNum - 1, rowNum - 1)
set thisWidthSize to cellSize()'s width
--log result
if thisWidthSize is greater than or equal to colWidthMax then set colWidthMax to thisWidthSize
end tell
end repeat
end tell
--log colWidthMax

set colWidthMax to colWidthMax + 5 --> 5は余白
tell aTableColumn
setWidth_(colWidthMax)
end tell

set viewWidth to viewWidth + colWidthMax
end repeat

## NSScrollView
### Viewの高さを計算
set viewHeight to (thisRowHeight + 2) * rowCount + thisHeaderHeight --> 2を足さないと高さが合わない
set vSize to current application's NSMakeRect(0, 0, viewWidth + 80, viewHeight) --> 80を足すといい感じの横幅になる

### Viewを作成
tell current application's NSScrollView's alloc()
tell initWithFrame_(vSize)
setBorderType_(current application's NSBezelBorder)
setDocumentView_(aTableObj)
--setHasHorizontalScroller_(true)
--setHasVerticalScroller_(true)
set aScroll to it
end tell
end tell

return {aScroll, aTableObj}
end makeContentView

## retrieve data
on retrieveData(aTableObj)
set aRow to aTableObj's selectedRow()
log result
if aRow is -1 then
set my _retrieve_data to {}
else
set my _retrieve_data to ((my _data_source)'s arrangedObjects()'s objectAtIndex:aRow) as record
end if
end retrieveData


# NSTableViewDatasource
on numberOfRowsInTableView:aTableView
--log "numberOfRowsInTableView:"
return (my _data_source)'s content()'s |count|()
end numberOfRowsInTableView:
# NSTableViewDelegate
on tableView:aTableView objectValueForTableColumn:aColumn row:aRow
--log "objectValueForTableColumn"
set keyName to aColumn's headerCell()'s title()
set aDict to (my _data_source)'s arrangedObjects()'s objectAtIndex:aRow
set aStr to aDict's objectForKey:keyName
--log result as text
return aStr
end tableView:objectValueForTableColumn:row:
# テーブル内のセルが編集できるか
on tableView:aTableView shouldEditTableColumn:aColumn row:aRow
return false
end tableView:shouldEditTableColumn:row:
# テーブル内をダブルクリックしたらOKボタンを押す
on doubleAction:sender
--log "doubleAction"
--log sender's |className|() as text
set theEvent to current application's NSEvent's ¬
keyEventWithType:(current application's NSEventTypeKeyDown) ¬
location:(current application's NSZeroPoint) ¬
modifierFlags:0 ¬
timestamp:0.0 ¬
windowNumber:(sender's |window|()'s windowNumber()) ¬
context:(current application's NSGraphicsContext's currentContext()) ¬
|characters|:return ¬
charactersIgnoringModifiers:(missing value) ¬
isARepeat:false ¬
keyCode:0
current application's NSApp's postEvent:theEvent atStart:(not false)
end doubleAction:
end script

##
tell (make MyDialog)
return call(aMainMes, aSubMes, optionRec)
end tell
end chooseData

# 正規表現でテキストを置き換えるハンドラ
on regexReplace(aText as text, pattern as text, replacement as text)
--require framework: Foundation
set regularExpression to current application's NSRegularExpression's regularExpressionWithPattern:pattern options:0 |error|:(missing value)
return (regularExpression's stringByReplacingMatchesInString:aText options:0 range:{location:0, |length|:count aText} withTemplate:replacement) as text
end regexReplace

# 与えたテキストに指定した文字列が出現する箇所を返すハンドラ
on findText(theText, serchStr)
set tmp to AppleScript's text item delimiters
set AppleScript's text item delimiters to serchStr
set theText to every text item of theText
set theList to {}
set x to 0
set delNum to number of serchStr
repeat with curItem in theText
set x to x + (number of curItem)
set end of theList to x + 1
set x to x + delNum
end repeat
set AppleScript's text item delimiters to tmp
if (number of theList) = 1 then return {}
return items 1 thru -2 of theList
end findText

更新履歴

inserted by FC2 system