6 Replies Latest reply: May 1, 2013 5:19 PM by Jwpackard
Jwpackard Level 1 Level 1 (0 points)

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

Screen Shot 2013-04-30 at 2.55.39 PM.png

 

Text version...

 

--

--  AppDelegate.applescript

--  TestingApplication

--

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

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

--

 

script AppDelegate

property parent : 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_

 

end script

 

 

 

Application Interface:

 

Screen Shot 2013-04-30 at 3.02.03 PM.png

 

 

 

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)
  • Frank Caggiano Level 7 Level 7 (25,715 points)

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

     

     

    script AppDelegate

      property parent : 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

     

    end script

     

    Screen Shot 2013-04-30 at 09.37.31.png

  • red_menace Level 6 Level 6 (14,905 points)

    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:

     

      property textField : missing value

      property speechButton : 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:

     

      set theSpeech to textField's stringValue() as text

     

    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 button 1

     

    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.

  • Jwpackard Level 1 Level 1 (0 points)

    Hey thanks very very much!!   I know I said that red_menace's answer solved my question, but yours was just as good just so you know and I had to pick one of the posts to say it solved my question... So, just thought I would say that!   Thanks very much for your answer!

  • Jwpackard Level 1 Level 1 (0 points)

    Hey red_menace, thanks very much for your answer!!   This helped loads.

  • Frank Caggiano Level 7 Level 7 (25,715 points)

    Glad it helped, good luck.

     

    I have found Learn AppleScript by Apress to be  both  a good  turorial and reference guide. The bonus is it has a very good section on Applescript/Objective-C.  He has 4 or so complete projects that cover a good deal, from the basic to more complex Objective-C AppleScript interactions

     

    regards

  • Jwpackard Level 1 Level 1 (0 points)

    Thanks very much!