モデル名を取得する
あくまでも参考にしてください
行番号 | ソース |
---|---|
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 |
006 | use AppleScript version "2.8" |
007 | use framework "Foundation" |
008 | use framework "AppKit" |
009 | use scripting additions |
010 | property refMe : a reference to current application |
011 | |
012 | ############################## |
013 | #####model_name取得 |
014 | ############################## |
015 | set strCommandText to "/usr/sbin/sysctl -n hw.model" as text |
016 | set strHWmodelName to (do shell script strCommandText) as text |
017 | |
018 | ############################## |
019 | #####本処理 |
020 | ############################## |
021 | ###【1】URL |
022 | set ocidComponents to refMe's NSURLComponents's alloc()'s init() |
023 | ocidComponents's setScheme:("https") |
024 | ocidComponents's setHost:("hw-model-names.services.jamfcloud.com") |
025 | ocidComponents's setPath:("/1/computerModels.xml") |
026 | set ocidURL to ocidComponents's |URL| |
027 | |
028 | ### 【2】NSDATA |
029 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
030 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference) |
031 | if (item 2 of listResponse) = (missing value) then |
032 | log "正常処理" |
033 | set ocidReadData to (item 1 of listResponse) |
034 | else 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 エラーしました" |
038 | end if |
039 | |
040 | ### 【3】XMLDOCにして |
041 | set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyXML) |
042 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error| :(reference) |
043 | if (item 2 of listResponse) = (missing value) then |
044 | log "正常処理" |
045 | else 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エラー 警告がありました" |
049 | end if |
050 | set ocidXMLDoc to (item 1 of listResponse) |
051 | |
052 | ### 【4】computer_model |
053 | set ocidRoot to ocidXMLDoc's rootDocument() |
054 | set listResponse to (ocidRoot's nodesForXPath:("//computer_model") |error| :(reference)) |
055 | if (item 2 of listResponse) = (missing value) then |
056 | log "正常処理" |
057 | else 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エラー 警告がありました" |
061 | end if |
062 | set ocidComputerModelArray to (item 1 of listResponse) |
063 | |
064 | ###【5】Array to Dict |
065 | set ocidModelNameDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
066 | ##Array =エレメントの数 |
067 | set numCntArray to ocidComputerModelArray's |count|() |
068 | ##エレメントの数だけ繰り返し |
069 | repeat 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())) |
074 | end repeat |
075 | |
076 | set strDisplayName to ocidModelNameDict's valueForKey:((strHWmodelName) as text) |
077 | |
078 | set strMes to ("戻り値\n") as text |
079 | set strMes to strMes & ("モデル名: " & strHWmodelName & "\n") as text |
080 | set strMes to strMes & ("表示名: " & strDisplayName & "") as text |
081 | |
082 | ############################## |
083 | #####ダイアログ |
084 | ############################## |
085 | ##前面に出す |
086 | set strName to (name of current application) as text |
087 | if strName is "osascript" then |
088 | tell application "Finder" to activate |
089 | else |
090 | tell current application to activate |
091 | end if |
092 | ###アイコンパス |
093 | set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias |
094 | |
095 | set 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 | ###自分自身を再実行 |
097 | if 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 |
102 | end if |
103 | |
104 | ###クリップボードコピー |
105 | if 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 |
118 | end if |
119 | |
AppleScriptで生成しました |
| 固定リンク
「Admin Device Management」カテゴリの記事
- デバイスUUIDの取得(2024.11.11)
- モデル名を取得する(2024.05.17)
- TB Default Item Identifiers(com.apple.finder.plist)(2023.12.22)
- [profiles] profiles コマンド書き出したバックアップを各ファイルに書き出す(2023.11.12)
- [profiles]現在のユーザー・プロファイル設定をバックアップ(2023.10.31)