property theAutoJoin : "true" as text
property theCaptiveBypass : "false" as text
property theDisableAssociationMACRandomization : "true" as text
property theEncryptionType : "WPA3" as text
property theHIDDEN_NETWORK : "false" as text
property theIsHotspot : "false" as text
property theProxyType : "None" as text
property theApiUrl : "https://chart.googleapis.com/chart?" as text
property theCht : "qr" as text
property theChs : "540x540" as text
property theChoe : "UTF-8" as text
property theChld : "Q" as text
on run
try
tell application "Finder"
activate
set objDialog to (display dialog "SSID(WIFIアクセスポイント):WifiQRコードの読み取り結果でも可" default answer "" with title "WIFIアクセスポイント")
end tell
set theSSID to (text returned of objDialog) as text
on error
"パスワードを取得できませんでした"
return
error number -128
end try
set theBoolText to (characters 1 thru 5 of theSSID) as text
if theBoolText is "WIFI:" then
set AppleScript's text item delimiters to ";"
set listSSIDData to (every text item of theSSID) as list
set AppleScript's text item delimiters to ""
set theLineDateSSID to (item 1 of listSSIDData) as text
set theLineDateSec to (item 2 of listSSIDData) as text
set theLineDatePW to (item 3 of listSSIDData) as text
set theSSID to doReplace(theLineDateSSID, "WIFI:S:", "") as text
set theEncryptionType to doReplace(theLineDateSec, "T:", "") as text
set thePW to doReplace(theLineDatePW, "P:", "") as text
set theSSID to doReplace(theSSID, ";", "") as text
set theEncryptionType to doReplace(theEncryptionType, ";", "") as text
set thePW to doReplace(thePW, ";", "") as text
set theSSID to doReplace(theSSID, ":", "") as text
set theEncryptionType to doReplace(theEncryptionType, ":", "") as text
set thePW to doReplace(thePW, ":", "") as text
set theSSID to doReplace(theSSID, " ", "") as text
set theEncryptionType to doReplace(theEncryptionType, " ", "") as text
set thePW to doReplace(thePW, " ", "") as text
else
try
tell application "Finder"
activate
set objDialog to (display dialog "WIFIパスワード" default answer "" with title "WIFIアクセスポイント・パスワード")
end tell
set thePW to (text returned of objDialog) as text
on error
"パスワードを取得できませんでした"
return
error number -128
end try
end if
set listReturned to {theSSID, thePW} as list
open (listReturned)
end run
on open listReturned
set theCommandText to ("uuidgen") as text
set theUUIDPayloadA to (do shell script theCommandText)
set theCommandText to ("uuidgen") as text
set theUUIDPayloadB to (do shell script theCommandText)
set theSSID to (item 1 of listReturned) as text
set thePW to (item 2 of listReturned) as text
set theCommandText to ("curl http://smile.s12.xrea.com/getip/hostname.php") as text
set theHOSTname to (do shell script theCommandText)
set theMobileconfig to ("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><dict><key>HasRemovalPasscode</key><false/><key>PayloadContent</key><array><dict><key>AutoJoin</key><" & theAutoJoin & "/><key>CaptiveBypass</key><" & theCaptiveBypass & "/><key>DisableAssociationMACRandomization</key><" & theDisableAssociationMACRandomization & "/><key>EncryptionType</key><string>" & theEncryptionType & "</string><key>HIDDEN_NETWORK</key><" & theHIDDEN_NETWORK & "/><key>IsHotspot</key><" & theIsHotspot & "/><key>Password</key><string>" & thePW & "</string><key>PayloadDescription</key><string>Wi-Fi設定を構成します</string><key>PayloadDisplayName</key><string>Wi-Fi</string><key>PayloadIdentifier</key><string>com.apple.wifi.managed." & theUUIDPayloadA & "</string><key>PayloadType</key><string>com.apple.wifi.managed</string><key>PayloadUUID</key><string>" & theUUIDPayloadA & "</string><key>PayloadVersion</key><integer>1</integer><key>ProxyType</key><string>" & theProxyType & "</string><key>SSID_STR</key><string>" & theSSID & "</string></dict></array><key>PayloadDisplayName</key><string>" & theSSID & "</string><key>PayloadIdentifier</key><string>" & theHOSTname & "." & theUUIDPayloadB & "</string><key>PayloadOrganization</key><string>" & theHOSTname & "</string><key>PayloadRemovalDisallowed</key><true/><key>PayloadType</key><string>Configuration</string><key>PayloadUUID</key><string>" & theUUIDPayloadB & "</string><key>PayloadVersion</key><integer>1</integer></dict></plist>") as text
tell application "Finder"
try
make new folder at (path to documents folder from user domain) with properties {name:"Wifi", displayed name:"Wifi"}
end try
end tell
tell application "Finder"
set theWifiFolder to (folder "Wifi" of (path to documents folder from user domain)) as alias
try
make new file at theWifiFolder with properties {name:("" & theSSID & ".mobileconfig")}
end try
set theFile to (((theWifiFolder) as text) & (("" & theSSID & ".mobileconfig") as text)) as alias
end tell
tell application "TextEdit"
open theFile
tell document 1
set its text to theMobileconfig
end tell
save document 1
close document 1
end tell
set theFilePath to (POSIX path of theFile) as text
set theCommandText to ("open \"" & theFilePath & "\" | open \"x-apple.systempreferences:com.apple.preferences.configurationprofiles\"") as text
do shell script theCommandText
set theQRcode to ("WIFI:S:" & theSSID & ";T:" & theEncryptionType & ";P:" & thePW & ";;") as text
set theOpenUrl to ("" & theApiUrl & "&cht=" & theCht & "&chs=" & theChs & "&choe=" & theChoe & "&chld=" & theChld & "&chl=" & theQRcode & "") as text
set theCommandText to ("curl -L -o \"$HOME/Documents/Wifi/" & theSSID & ".png\" '" & theOpenUrl & "' --connect-timeout 20") as text
do shell script theCommandText
tell application "Finder"
set theWifiFolder to (folder "Wifi" of (path to documents folder from user domain)) as alias
open theWifiFolder
end tell
end open
to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace