Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript text field

Hey everyone!


I have recently been doing a lot of AppleScript and Xcode 4. I have also been combining them, (making Cocoa AppleScript projects in Xcode) and today started on a project to convert text into speech. I already know how to do most of the code (like say "phrase" etc.) but I am stuck with 1 thing. I have a text box, and a button. I want to make it so that when I click the button, it will read what the user has typed in the text box and convert it into a variable which I can then make the computer speak aloud. PLEASE tell me how I can get it to convert the text in the text box into a variable... I have been trying for hours and got nowhere. I feel like I've searched every online place I can!


Here is some additional information.


My Code:



Image version...

User uploaded file


Text version...


--

-- AppDelegate.applescript

-- TestingApplication

--

-- Created by Felix Packard on 29/04/13.

-- Copyright (c) 2013 Felix Packard. All rights reserved.

--


script AppDelegate

propertyparent : class"NSObject"

on speechButtonClicked_(sender)

end speechButtonClicked_

on helpButtonClicked_(sender)

set description to"Date created: 30-4-2013\n\nMade by: Felix Packard\n\nVersion: 1.0\n\nDescription: This applications purpose is very simple: It converts text into speech. It will be updated and have more featured over time."

display dialog description with title "Info" buttons {"Done"} default button 1

end helpButtonClicked_

on applicationWillFinishLaunching_(aNotification)


-- Insert code here to initialize your application before any files are opened

end applicationWillFinishLaunching_


on applicationShouldTerminate_(sender)


-- Insert code here to do any housekeeping before your application quits

return current application's NSTerminateNow

end applicationShouldTerminate_


endscript




Application Interface:


User uploaded file




Button handler linked to 'Say Text' button (from code higher up in question):


on speechButtonClicked_(sender)

end speechButtonClicked_




Button handler linked to 'Help' button (from code higher up in question):



on helpButtonClicked_(sender)

set description to"Date created: 30-4-2013\n\nMade by: Felix Packard\n\nVersion: 1.0\n\nDescription: This applications purpose is very simple: It converts text into speech. It will be updated and have more featured over time."

display dialog description with title "Info" buttons {"Done"} default button 1

end helpButtonClicked_



All help is greatly appreciated. Thanks in advance! 🙂

iMac, Mac OS X (10.7.5)

Posted on Apr 29, 2013 10:08 PM

Reply
Question marked as Best reply

Posted on Apr 30, 2013 6:40 AM

You need to create a property for the text field and attach it.



script AppDelegate


propertyparent : class"NSObject"


property textInputField : missing value



on applicationWillFinishLaunching_(aNotification)


-- Insert code here to initialize your application before any files are opened

end applicationWillFinishLaunching_


on applicationShouldTerminate_(sender)


-- Insert code here to do any housekeeping before your application quits

return current application's NSTerminateNow

end applicationShouldTerminate_


on buttonPushed_(id)

set aa to textInputField's stringValue()

display dialog aa as text

end


endscript


User uploaded file

6 replies
Question marked as Best reply

Apr 30, 2013 6:40 AM in response to Jwpackard

You need to create a property for the text field and attach it.



script AppDelegate


propertyparent : class"NSObject"


property textInputField : missing value



on applicationWillFinishLaunching_(aNotification)


-- Insert code here to initialize your application before any files are opened

end applicationWillFinishLaunching_


on applicationShouldTerminate_(sender)


-- Insert code here to do any housekeeping before your application quits

return current application's NSTerminateNow

end applicationShouldTerminate_


on buttonPushed_(id)

set aa to textInputField's stringValue()

display dialog aa as text

end


endscript


User uploaded file

Apr 30, 2013 7:09 AM in response to Jwpackard

The usual way to refer to user interface items such as buttons and text fields in your script is to create outlet properties (set to missing value so they will show up in the Interface Builder), then you can use the various methods from their classes - for example:



propertytextField : missing value


propertyspeechButton : missing value

To get the text entered in a text field (note that a text field is different from, and has different methods than, a text view), you would then do something like:


settheSpeechtotextField's stringValue() astext


Also note that Xcode doesn't have syntax checking like the AppleScript Editor, so if you happen to use a reserved word (such as description in your help handler) it won't complain, but your program may not compile or run properly. To use a reserved word (and some of the Cocoa method names do use them), surround the term with pipes - for example:


display dialog|description|with title"Info"buttons {"Done"} default button1


I also have an example speech synthesizer project on my AppleScriptObjC page. It uses a speech synthesizer instead of the say command, but it does use various buttons and a text field. It can be download here.

AppleScript text field

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.