How can I acquire text from text filed - xcode

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.
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_

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.

Related

How to learn Applescript

I'm new in programming. I can write some Python code and I would like to write some script on a Macbook with the Script Editor.
So what is the best way to learn AppleScript? What material do I need?
I have tried to learn AppleScript with the language guide and Window Anatomy.
But when I tried to read some code, I can't understand many of the statements.
Example:
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound" --I can't understand "com.apple.preference.sound".What's the rule of strings like this?
end tell
tell application "System Events"
tell application process "System Preferences"
tell tab group of window "Sound"
click radio button "output" --There,I don't exactly know what's radio button,pane,table is.
if (selected of row 1 of table 1 of scroll area 1) then
set selected of row 2 of table 1 of scroll area 1 to true
set deviceselected to "外置音响"
else if (selected of row 2 of table 1 of scroll area 1) then
set selected of row 3 of table 1 of scroll area 1 to true
set deviceselected to "LG"
else
set selected of row 1 of table 1 of scroll area 1 to true
set deviceselected to "MAC"
end if
end tell
end tell
end tell
It seems I don't know these concepts outside of the language syntax. How can I learn these things?
The string is a string (a sequence of characters; surrounded by quote marks when written in literal—code—form). The content of that particular string is a Uniform Type Identifier which is just a naming convention, much like domain names in URLs except backwards.
Knowing what the various GUI widgets are called takes a passing familiarity with GUI programming. e.g. Radio buttons are the circular buttons which come in groups where you can pick any one. There is an helpful app for GUI Scripting if you really, really need it. Also, GUI Scripting is a klunky fragile brittle pig and only used as the last resort when apps don’t provide a scripting interface of their own. Which brings us to a more general point…
Knowing what the various types of objects that exist in an AppleScriptable application are called, plus what information they contain and how they can be manipulated with commands, requires reading that application’s AppleScript dictionary (File > Open Dictionary in Script Editor) plus some intelligent guesswork (since app dictionaries are almost always famously inadequate on details). A good AppleScript book will help you to develop that skill, and will save you a lot of head-scratching and time in the long run. (I was lead author on #BernardR’s book #2, btw.)
If you are serious about learning AppleScript, I strongly recommend you get Script Debugger. It is far superior to Apple’s Script Editor not only for exploring application dictionaries but also the live objects within the running application itself. The user-to-user forum there is also good for AppleScript-specific advice.
Lastly, if you are familiar with an Object-Oriented Programming language such as Python, you will find AppleScript uses a lot of the same jargon when talking about scriptable applications: classes, objects, etc. Do not be fooled. Application scripting (Apple event IPC) is not OOP: it is remote procedure calls which pass simple queries as arguments. The queries identify the data you wish a command to operate on (e.g. size of first word of every paragraph of front document) and the command tells the application what to do with whatever data it finds (e.g. set (size of…) to 18). Thus in AppleScript you can perform operations such as:
tell application "TextEdit"
set size of first word of every paragraph of front document to 18
end tell
which is not a concept you can express in OOP; it’s a lot more powerful than that. (BTW, this is why programmers hate AppleScript: because they incorrectly assume application scripting is OOP; then when it behaves in some profoundly non-OOP way it confuses the hell out of them.)
--
p.s. Good Luck. You will need it, as it is awesome and frustrating in equal measure. (Although the awesome parts usually make it worth all the hair pulling it takes in getting there.)
The name of a preference pane is not a part of the AppleScript language.
The authority on the Sound preference pane is /System/Library/PreferencePanes/Sound.prefPane/Contents/Info.plist. There, you can notice
<key>CFBundleIdentifier</key>
<string>com.apple.preference.sound</string>
This is the name of the pane.
You can consult How to know the name of UI elements using Accessibility inspector (or any other tool) and AppleScript – How can I get UI elements' names, attributes, properties, classes programmatically without "guessing" via AccessibilityInspector? to figure out how to know about radio button "output" part.
I can recommend 2 books to learn AppleScript:
AppleScript 1-2-3, A Self-Paced Guide to Learning AppleScript, by Sal Soghoian and Bill Cheeseman
Learn AppleScript, The Comprehensive Guide to Scripting and Automation on Mac OS X (3rd Edition), by Hamish Sanderson and Hanaan Rosenthal
The first book is good for scripting beginners but both books complement each other.
There are many example scripts and the authors of both books discuss key concepts that are fundamental to scripting with AppleScript.
The author of the first book, Sal Soghoian, was for several years Product Manager for Automation Technologies at Apple.

AppleScript: dialog with multiple rows of buttons

A code like this display dialog "choose" buttons {"yes", "no"} creates a dialog with 2 buttons, in a single row, as expected.
I am trying to find out if it is possible to create more than one row of buttons.
For example, first row could have buttons {"red", "yellow", "blue"}, second row could be {"large", "small"}, and a third row could just be {"cancel"}.
Is it possible?
Because AppleScript was designed to be a automation system, as opposed to a full-fledged programming language, it has always restricted its GUI to simple 'alert'-style panels. The idea was that a script should bother the user as little as possible, showing dialogs that ask for a single piece of information and then getting out of the way so that the script could work more or less invisibly. Its native alerts have a maximum of three buttons (on the "Yes", "No", "Cancel" model). The only native way to deal with a choice of more than three items is to use the choose from list command, e.g.:
set meal to choose from list {"Hamburger", "Pizza", "Spaghetti", "Cucumber Salad", "Pork Chops", "Green Eggs and Ham"} with title "Meal Time" with prompt "What do you want to eat?"
which produces an alert like this:
While it's possible to create a multi-button alert with rows of buttons using AppleScriptObjC, it's a non-trivial task: a lot of effort for little reward. Basically one has to create a new alert using NSAlert, then create and attach an accessory-view in which you've added and positioned your rows of NSButtons. You'll also have to add some logic so the accessory view buttons you've created act as 'alert' buttons (meaning they dismiss the alert when clicked). Stack Overflow isn't really meant for coding services — though people do occasionally break that rule — but if you want to work through the bulk of the coding yourself, we'll happily assist you with difficulties.
You may want to take a look at Dialog Toolkit. It is a scripting addition which can be used in Script Editor:
Dialog Toolkit Plus v1.0.1 and Dialog Toolkit v2.0.3
Dialog Toolkit and Dialog Toolkit Plus are ASObjC-based script libraries for showing enhanced dialogs. Add multiple text entry fields, checkboxes, popup menus, radio buttons, path controls, extra buttons and rules to dialogs. Dialog Toolkit v2.0.3 is a compatible update that fixes an issue with macOS High Sierra (10.13), while Dialog Toolkit Plus is an enhanced version that gives greater flexibility but requires macOS 10.10 or later. You can download the libraries and documentation here. (Direct Download Link)
Here are some screenshots of sample dialogs created using the Dialog Tool Kit script library, in Script Editor.app

Voice Recognition MIT app inventor

I am creating a voice recognition app with MIT AppInventor that can match the voice I recorded that will activate the alarm. Problem is that I don't know how to match the voice I recorded to my voice recognition app to be able for the phone to activate the alarm.
All you need to do is add a series of codes that save your recorded audio/speech recognizer input to a TinyDB. To do this, you create a button or something to trigger the block "call speech recognizer.get text". After that is done you place down the "After SpeechRecognizer.get text" block and inside it place the "call DB1.store value", and set the value to the variable "result" provided by "After speechrecognizer.get text". I made an example with a few other things that make it seem larger than it actually is, mainly other means of security. The screenshot is outdated, I added a few things after but they're just minor preference tweaks that have no effect on the functionality of the program. Please enjoy!
https://docs.google.com/document/d/14PunjHwjBxdyJcaKtoNydpz7U5w2FgQwPXUrRjGNqpk/edit?usp=sharing.

How to find out, programmatically, which language is currently selected for Dictation? (Apple Watch)

I have an Apple watch app that works exclusively with English dictation. The reason no more languages are available is a long story so bear with me a while.
I know that doing a "hard press" on the screen, while dictation is going on with the nice voice graph, brings up a "choose language" menu where the user can select one of his available languages to do dictation with.
I want to find out programmatically, which language is currently being used for dictation, so that I can prompt the user to switch to English, if any other language is currently selected.
The menus, and everything on my phone is in English, but I also use German, Catalan and Spanish.
So far, I am using this code:
let language:NSString=NSLocale.preferredLanguages().first!
print("ASR(?) Language: "+(language as String))
But this, in fact, returns the System Language that is set in the main iOS Watch Map: General > Language and Region > System Language, not the Dictation Language.
...and this is the code I use for Dictation:
self.presentTextInputControllerWithSuggestions([], allowedInputMode: WKTextInputMode.Plain, completion:{(results) -> Void in
let aResult = results?[0] as? String
if(!(aResult == nil)){
print(aResult)
print("ASR says: "+aResult!)
}//end if
})//end show voice menu
Is it possible to detect which language is really the one being used for dictation?

looking for (N) steps GUI kind of thing

i dont sure if this question is for this group but i don't know where to ask
im looking for GUI examples that gives the user for example make X in 4 steps
kind of GUI especial for none teachi folks
Do you mean like a Dialog Wizard type interface?
The idea of a wizard is you have a single window where you can step through a number of frames containing different selections using a next button and a back button to go back and change selections? Sometimes you have a finish button to use the default selections on the remaining frames.
What language and GUI toolkit are you using???

Resources