可修复“无法将{button返回:“连接”,返回文本:“ test”}更改为Unicode类型的文本。”

时间:2019-07-03 10:07:44

标签: terminal applescript automator

我正在尝试创建一个小型自动化应用程序,以通过可能会隐藏网络的 ssh'd 设备上的终端连接到无线网络。

一系列选项导致一个选项,只需键入 SSID 并连接设备即可。

set userSSID to display dialog "Type the SSID for the network you require below" buttons ["Connect", "Cancel"] default answer "" default button 1

            if button returned of result = "Cancel" then
                error number -128

            else

                if button returned of result = "Connect" then

                    tell application "Terminal"
                        set currentTab to do script ("networksetup -setairportpower en1 on")
                        delay 2
                        do script ("networksetup -setairportnetwork en1 '" & userSSID & "'") in currentTab
                    end tell
                end if

我希望它以输入的方式连接到SSID,在本例中为“测试”,但是出现错误:

  

“运行AppleScript”操作遇到错误:“无法创建{按钮   返回:“连接”,返回的文本:“ test”}为Unicode文本类型。”

1 个答案:

答案 0 :(得分:1)

display dialog返回记录{button returned, text returned}作为错误状态。

要使用text returned的值,必须写

do script ("networksetup -setairportnetwork en1 '" & text returned of userSSID & "'") in currentTab

但是,您的代码中存在一些冗余,例如,按下Cancel按钮会立即中止脚本。

代码可以简化为

set {text returned:userSSID} to display dialog "Type the SSID for the network you require below" buttons ["Connect", "Cancel"] default answer "" default button 1
tell application "Terminal"
    set currentTab to do script ("networksetup -setairportpower en1 on")
    delay 2
    do script ("networksetup -setairportnetwork en1 '" & userSSID & "'") in currentTab
end tell

,甚至可能没有Terminal.app

set {text returned:userSSID} to display dialog "Type the SSID for the network you require below" buttons ["Connect", "Cancel"] default answer "" default button 1
do shell script "networksetup -setairportpower en1 on"
delay 2
do shell script "networksetup -setairportnetwork en1 " & quoted form of userSSID