« [IP]IPアドレスとホスト名を取得する(グローバル・アドレス) | トップページ | [valueForKey]レコードの値を取得 »

[mobileconfig]AppleScriptでの処理の基本(再考)

mobileconfigはMDM(モバイル・デバイス・マネージメント)の設定ファイル
拡張子はmobileconfigですが中身はXMLで、お馴染みplist形式
DOCTYPE
plist
Uniform Type Identifiers
com.apple.mobileconfig
mime-type
text/xml



20220516183453_1912x814x144_0
上記を考慮するとわかりやすいか?と思います



ダウンロード - mobileconfigsmaple.zip



#!/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

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSDictionary : a reference to objMe's NSDictionary
property objNSMutableString : a reference to objMe's NSMutableString
property objNSMutableArray : a reference to objMe's NSMutableArray
property objNSMutableDictionary : a reference to objMe's MutableDictionary
property objNSJSONSerialization : a reference to objMe's NSJSONSerialization

#######値として使うUUIDを作っておく
#set listUUID to {} as list
set ocidListOfUUID to objNSMutableArray's alloc()'s init()

repeat 4 times
set strUUID to (do shell script "uuidgen") as text
####
#copy strUUID to end of listUUID
ocidListOfUUID's addObject:strUUID
end repeat

########################################################
########値として使うIPアドレスを取得
set strIP to (do shell script "curl 'https://api.ipify.org?format=text'")
###NSString
set ocidIP to objNSString's stringWithString:strIP
###ドットを区切り文字にリスト化
set ocidIpNSArray to ocidIP's componentsSeparatedByString:"."
###逆順用のリストを用意して
set ocidIpReverse to objNSMutableString's |string|()
###逆順リストに追加していきく
ocidIpReverse's appendString:(ocidIpNSArray's objectAtIndex:3)
ocidIpReverse's appendString:"."
ocidIpReverse's appendString:(ocidIpNSArray's objectAtIndex:2)
ocidIpReverse's appendString:"."
ocidIpReverse's appendString:(ocidIpNSArray's objectAtIndex:1)
ocidIpReverse's appendString:"."
ocidIpReverse's appendString:(ocidIpNSArray's objectAtIndex:0)
####dns.google
set strDnsCommandText to ("/usr/bin/curl -X GET -H 'Content-Type: application/json' 'https://dns.google/resolve?name=" & (ocidIpReverse as text) & ".in-addr.arpa&type=PTR&cd=true&do=true'") as text
####コマンド実行
set jsonDnsQuery to (do shell script strDnsCommandText) as text
###戻り値のJSONをレコードに格納
set ocidResJson to (objNSJSONSerialization's JSONObjectWithData:((objNSString's stringWithString:jsonDnsQuery)'s dataUsingEncoding:(objMe's NSUTF8StringEncoding)) options:0 |error|:(missing value))
####Authorityを取得
set ocidAnswerArray to ocidResJson's Answer
####Answer』はリストArrayなので1つ目を取得する
set ocidAuthorityDictionary to ocidAnswerArray's objectAtIndex:0
####↑で取得したディレクトリの『data』を取得-->ホスト名
set ocidHostName to ocidAuthorityDictionary's valueForKey:"data"

########################################################
set aliasFile to (choose file with prompt "mobileconfigを選んでください" default location (path to desktop folder from user domain) of type {"com.apple.mobileconfig"} with invisibles and showing package contents without multiple selections allowed) as alias
########################################################
set strFilePath to POSIX path of aliasFile
set ocidFilePath to objNSString's stringWithString:strFilePath
####ファイルの内容をディレクトリ=レコードに
set ocidXmlData to objNSDictionary's dictionaryWithContentsOfFile:ocidFilePath

#####1:読み込みXMLデータ
log ocidXmlData

#####2:PayloadContent=これはArrayでリスト形式
##set ocidListPayloadContent to PayloadContent of ocidXmlData
set ocidListPayloadContent to ocidXmlData's PayloadContent


#####3:Arrayの1つめの項目=ディレクトリ
#set objPayloadContent to item 1 of ocidListPayloadContent
set objPayloadContent to ocidListPayloadContent's objectAtIndex:0

#####4:Arrayの内容=ディレクトリ=WIFI設定部の値を変更
##objPayloadContent's setObject:("" & (item 3 of listUUID) & "") forKey:"PayloadUUID"
##objPayloadContent's setObject:("" & (item 4 of listUUID) & "") forKey:"PayloadIdentifier"

objPayloadContent's setObject:("" & (ocidListOfUUID's objectAtIndex:2) & "") forKey:"PayloadUUID"
objPayloadContent's setObject:("" & (ocidListOfUUID's objectAtIndex:3) & "") forKey:"PayloadIdentifier"

##set AutoJoin of objPayloadContent to true
##set CaptiveBypass of objPayloadContent to true
##set DisableAssociationMACRandomization of objPayloadContent to true
##set HIDDEN_NETWORK of objPayloadContent to false
##set IsHotspot of objPayloadContent to false

objPayloadContent's setObject:true forKey:"AutoJoin"
objPayloadContent's setObject:true forKey:"CaptiveBypass"
objPayloadContent's setObject:true forKey:"DisableAssociationMACRandomization"
objPayloadContent's setObject:false forKey:"HIDDEN_NETWORK"
objPayloadContent's setObject:false forKey:"IsHotspot"
-->booleanはダブルクオテーション無し

##set EncryptionType of objPayloadContent to "WPA3"
##set PayloadDescription of objPayloadContent to "Wi-Fi設定"
##set PayloadType of objPayloadContent to "com.apple.wifi.managed"
##set ProxyType of objPayloadContent to "None"

objPayloadContent's setObject:"WPA3" forKey:"EncryptionType"
objPayloadContent's setObject:"Wi-Fi設定" forKey:"PayloadDescription"
objPayloadContent's setObject:"com.apple.wifi.managed" forKey:"PayloadType"
objPayloadContent's setObject:"None" forKey:"ProxyType"

##set PayloadVersion of objPayloadContent to 1
objPayloadContent's setObject:1 forKey:"PayloadVersion"
-->integerはダブルクオテーション無し

##set SSID_STR of objPayloadContent to "ここにSSID"
##set Password of objPayloadContent to "ここにパスワード"
objPayloadContent's setObject:"ここにSSID" forKey:"SSID_STR"
objPayloadContent's setObject:"ここにパスワード" forKey:"Password"


#####5:ここは『ocidXmlData』=ROOTのデータの値の変更
##ocidXmlData's setObject:("" & (item 1 of listUUID) & "") forKey:"PayloadUUID"
##ocidXmlData's setObject:("" & (item 2 of listUUID) & "") forKey:"PayloadIdentifier"

ocidXmlData's setObject:("" & (ocidListOfUUID's objectAtIndex:0) & "") forKey:"PayloadUUID"
ocidXmlData's setObject:("" & (ocidListOfUUID's objectAtIndex:1) & "") forKey:"PayloadIdentifier"

ocidXmlData's setObject:"Configuration" forKey:"PayloadType"
ocidXmlData's setObject:("" & ocidHostName & "") forKey:"PayloadOrganization"

ocidXmlData's setObject:"WIFIの設定用設定" forKey:"PayloadDescription"
ocidXmlData's setObject:"com.apple.wifi.managed" forKey:"PayloadDisplayName"

ocidXmlData's setObject:1 forKey:"PayloadVersion"
ocidXmlData's setObject:false forKey:"PayloadRemovalDisallowed"

####
set ocidDictConsentText to ocidXmlData's ConsentText
ocidDictConsentText's setObject:"WIFIの設定SSIDとパスワードが記載されています" forKey:"default"

####ふぁいる保存
ocidXmlData's writeToFile:ocidFilePath atomically:true

log objPayloadContent as list
log ocidXmlData as list
log className() of objPayloadContent as text

|

« [IP]IPアドレスとホスト名を取得する(グローバル・アドレス) | トップページ | [valueForKey]レコードの値を取得 »

Admin Mobileconfig」カテゴリの記事

XML Plist」カテゴリの記事

Admin Device Management」カテゴリの記事