« [Shortcuts]macOS14.5 スクリーンキャプチャーが利用できない場合の回避方法 | トップページ | [メモ]VMware Fusion 個人用無料版ダウンロードURL »

モデル名を取得する

20240517034654962x229
あくまでも参考にしてください

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

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004#com.cocolog-nifty.quicktimer.icefloe
005----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
006use AppleScript version "2.8"
007use framework "Foundation"
008use framework "AppKit"
009use scripting additions
010property refMe : a reference to current application
011
012##############################
013#####model_name取得
014##############################
015set strCommandText to "/usr/sbin/sysctl -n hw.model" as text
016set strHWmodelName to (do shell script strCommandText) as text
017
018##############################
019#####本処理
020##############################
021###【1】URL
022set ocidComponents to refMe's NSURLComponents's alloc()'s init()
023ocidComponents's setScheme:("https")
024ocidComponents's setHost:("hw-model-names.services.jamfcloud.com")
025ocidComponents's setPath:("/1/computerModels.xml")
026set ocidURL to ocidComponents's |URL|
027
028### 【2】NSDATA
029set ocidOption to (refMe's NSDataReadingMappedIfSafe)
030set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference)
031if (item 2 of listResponse) = (missing value) then
032  log "正常処理"
033  set ocidReadData to (item 1 of listResponse)
034else if (item 2 of listResponse) ≠ (missing value) then
035  log (item 2 of listResponse)'s code() as text
036  log (item 2 of listResponse)'s localizedDescription() as text
037  return "XML エラーしました"
038end if
039
040### 【3】XMLDOCにして
041set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML)
042set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error| :(reference)
043if (item 2 of listResponse) = (missing value) then
044  log "正常処理"
045else if (item 2 of listResponse) ≠ (missing value) then
046  log (item 2 of listResponse)'s code() as text
047  log (item 2 of listResponse)'s localizedDescription() as text
048  log "NSXMLDocumentエラー 警告がありました"
049end if
050set ocidXMLDoc to (item 1 of listResponse)
051
052### 【4】computer_model
053set ocidRoot to ocidXMLDoc's rootDocument()
054set listResponse to (ocidRoot's nodesForXPath:("//computer_model") |error| :(reference))
055if (item 2 of listResponse) = (missing value) then
056  log "正常処理"
057else if (item 2 of listResponse) ≠ (missing value) then
058  log (item 2 of listResponse)'s code() as text
059  log (item 2 of listResponse)'s localizedDescription() as text
060  log "nodesForXPathエラー 警告がありました"
061end if
062set ocidComputerModelArray to (item 1 of listResponse)
063
064###【5】Array to Dict
065set ocidModelNameDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
066##Array =エレメントの数
067set numCntArray to ocidComputerModelArray's |count|()
068##エレメントの数だけ繰り返し
069repeat with itemNo from 0 to (numCntArray - 1) by 1
070  set ociidItemArray to (ocidComputerModelArray's objectAtIndex:(itemNo))
071  set ocidModelName to (ociidItemArray's elementsForName:("model_name"))'s firstObject()
072  set ocidDisplayName to (ociidItemArray's elementsForName:("display_name"))'s firstObject()
073  (ocidModelNameDict's setObject:(ocidDisplayName's stringValue()) forKey:(ocidModelName's stringValue()))
074end repeat
075
076set strDisplayName to ocidModelNameDict's valueForKey:((strHWmodelName) as text)
077
078set strMes to ("戻り値\n") as text
079set strMes to strMes & ("モデル名: " & strHWmodelName & "\n") as text
080set strMes to strMes & ("表示名: " & strDisplayName & "") as text
081
082##############################
083#####ダイアログ
084##############################
085##前面に出す
086set strName to (name of current application) as text
087if strName is "osascript" then
088  tell application "Finder" to activate
089else
090  tell current application to activate
091end if
092###アイコンパス
093set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias
094
095set recordResult to (display dialog strMes with title "戻り値です" default answer strMes buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer)
096###自分自身を再実行
097if button returned of recordResult is "再実行" then
098  tell application "Finder"
099    set aliasPathToMe to (path to me) as alias
100  end tell
101  run script aliasPathToMe
102end if
103
104###クリップボードコピー
105if button returned of recordResult is "IPアドレスをクリップボードにコピー" then
106  set strText to (text returned of recordResult) as text
107  try
108    ####ペーストボード宣言
109    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
110    set ocidText to (refMe's NSString's stringWithString:(strIP))
111    appPasteboard's clearContents()
112    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
113  on error
114    tell application "Finder"
115      set the clipboard to strIP as text
116    end tell
117  end try
118end if
119
AppleScriptで生成しました

|

« [Shortcuts]macOS14.5 スクリーンキャプチャーが利用できない場合の回避方法 | トップページ | [メモ]VMware Fusion 個人用無料版ダウンロードURL »

Admin Device Management」カテゴリの記事