How can I acquire text from text filed

82 views Asked by At

I am new to programming and am trying to design a Fortune Teller game according to a book "Applescript studio programming for the absolute beginner".

When the player types the question in the Text Field and then clicks the "Ask" button, the program will randomly give the answer "Yes" "No" "Maybe".

I did as the book instructed. But when I press build and go in Xcode, it returns "2018-04-08 22:22:01.189 Fortune Teller[14813:245050] *** -[AppDelegate clicked:]: Can’t get every text of TextField of class "NSObject". (error -1728)"

Can anyone tell me how to fix that problem? Thank you!

The following is the interface. enter image description here

The following is the code.

-- This code runs whenever the player clicks on the Ask button on clicked_(theObject)

-- Assign the text entered by the player clicks on the Ask button set the question to contents of text field "textbox" of window "main"

-- Display an error message if the player did not enter any text if question = "" then display dialogue ¬ "Sorry, but you did not ask a question. Please try again."¬ buttons {"OK"} return end if -- Assign a random number between 1 and 3 to a variable named random set randomNo to a random number from 1 to 3

if randomNo = 1 then set answer to "Yes" end if

if randomNo = 2 then set answer to "No" end if

if randomNo = 3 then set answer to "Maybe" end if

beep -- Play a beep sound to get player's intention

display display dialog "Question:" & question & "Answer:" & answer & buttons {"OK"} end clicked_

1

There are 1 answers

2
foo On

Ouch. Get a new book.

Mac OS X 10.6 replaced AppleScript Studio with AppleScript-ObjC. Unlike ASS, which gave you AppleScript-style commands and classes for controlling Cocoa, ASOC is just a thin wrapper around Cocoa’s Objective-C APIs, meaning you need to learn those APIs before you can use it.

Shane Stanley used to have an e-book on GUI programming with ASOC, but I think it’s out of print unfortunately. There was an introductory chapter to GUI programming with ASOC at the end of Apress’s Learn AppleScript, 3rd edition (which I co-wrote), and the AppleScript Users mailing list/MacScripter.net forum can no doubt point you to whatever other resources are out there. But all in all, documentation and support is not great. You pretty much have to learn ObjC in order to use ASOC effectively, even if just to read the Cocoa documentation and translate it to AS syntax in your head. And it hasn’t been updated in several years so lacks support for newer ObjC features such as blocks, which means you end up writing ObjC code anyway if you want to use those APIs.

..

Frankly, if your goal is to write your own GUI Apps (rather than automate existing ones), I strongly recommend you bite the bullet and learn Apple’s Swift. While it’s a bloated mess of a language, it’s well-documented, widely-used, and 100% supported by Apple [unlike AppleScript]. With Apple recently announcing a new GUI framework for writing iOS and macOS apps, it’s clear that Swift is the future.

If you need a bit of AppleScript to talk to other apps, it is possible to mix Swift and AS in the same app; but that’s a different question for a separate post.