
我想製作一個顯示框,其中有一個文字框,使用者可以在其中輸入數字,稍後將其分配給變數。我該怎麼做?
答案1
開啟 AppleScript 編輯器,輸入以下內容並另存為腳本:
tell application "Terminal"
repeat while true
set input to display dialog "Enter a number:" default answer ""
if button returned of input is equal to "OK" then
try
return (text returned of input) as number
end try
end if
end repeat
end tell
(我們需要tell application
,因為否則osascript
不允許使用者互動)
然後像這樣運行:
$ osascript path/to/script.scpt
程式的輸出是使用者輸入的數字。
bash
像這樣儲存在變數中:
$ foo=$( osascript path/to/script.scpt )
$ echo $foo
42