Apple Event: May 7th at 7 am PT

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

Xcode text

Hi


I am having a problem, hope some of you can help me!!


I started and created applescript for my first cocoa applescript application.


I already got all my buttons hooked up to the code BUT, I can't get the text to work.


In plain applescript, I can get results like time or weather by return text, say text or display dialog.


How do I display text in cocoa applescript?!!!!


I am using Xcode not applescript editor for this project.


Please help me!!


Thank you so much!!

MacBook Pro, OS X Mountain Lion (10.8.2)

Posted on Apr 12, 2013 8:32 PM

Reply
11 replies

Apr 12, 2013 10:10 PM in response to red_menace

Thanks for replying,


set tickerArray to {"GOOG"}

set finalString to ""

repeat with theTicker in tickerArray

set impNumbers to (do shell script "curl \"http://www.google.com/finance?client=ob&q=" & theTicker & "\" | sed 's/<[^>]*>//g' | grep -A12 \"google.finance.renderRelativePerformance();\"")

if character 1 of paragraph 12 of impNumbers = "+" then

set posNeg to true

else if character 1 of paragraph 12 of impNumbers = "-" then

set posNeg to false

end if

set finalString to finalString & (theTicker as string)

if (number of characters of theTicker) < 3 then

set spacer to " "

else

set spacer to " "

end if

set finalString to finalString & spacer & (paragraph 9 of impNumbers)

set finalString to finalString & " " & (character 1 of paragraph 12 of impNumbers) & ((characters 2 through (number of characters of paragraph 12 of impNumbers) of paragraph 12 of impNumbers) as string)


if (character 2 of paragraph 13 of impNumbers) as string = "-" then

set finalString to finalString & " " & (paragraph 13 of impNumbers) & return

else

set finalString to finalString & " (" & (character 1 of paragraph 12 of impNumbers) & (characters 2 through -1 of paragraph 13 of impNumbers) & return

end if

end repeat


return finalString


end


This is one of the codes I have that collect and return information to me.


finalString in this script is google's stock information.



right now it is returning the information but I can also have my computer say it or display it in a display dialog.


I want to use display it in a text view in a cocoa application.


Also, do you know how to get the application to refresh the information every minute or so?


Thanks

Apr 12, 2013 10:43 PM in response to skywalkerkenobi

Typically you would set up an outlet property for the text view, for example:


property myTextView : missing value


...then connect the outlet to the text view (not the scroll view part) in the Interface Editor. Once connected you can use the various NSTextView class (and its parent) methods and put text in the text view by using a statement such as:


myTextView's setString_(finalString)



A timer can be set up when the application is being initialized to repeatedly call your handler, for example:


property refreshTimer: missing value-- so the timer can be manipulated later if desired


--


set refreshTimer to current application's NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(60, me, "yourHandler", missing value, true)


Your handler declaration didn't get pasted (I'm guessing it doesn't have any passed parameters), so just replace yourHandler with the name of your handler - see the NSTimer Class Reference.

Apr 13, 2013 4:01 PM in response to red_menace

script AppDelegate


propertyparent : class"NSObject"

@private

NSWindow *window

myTextView's setString_(finalstring)

property myTextView : missing value set tickerArray to {"GOOG"}

set missing value to finalString

set finalString to ""

repeat with theTicker in tickerArray

set impNumbers to (do shell script"curl \"http://www.google.com/finance?client=ob&q=" & theTicker & "\" | sed 's/<[^>]*>//g' | grep -A12 \"google.finance.renderRelativePerformance();\"")

if character 1 of paragraph 12 of impNumbers = "+" then

set posNeg to true

else if character 1 of paragraph 12 of impNumbers = "-" then

set posNeg to false

end if

set finalString to finalString & (theTicker as string)

if (number of characters of theTicker) < 3 then

set spacer to" "

else

set spacer to" "

end if

set finalString to finalString & spacer & (paragraph 9 of impNumbers)

set finalString to finalString & " " & (character 1 of paragraph 12 of impNumbers) & ((characters 2 through (number of characters of paragraph 12 of impNumbers) of paragraph 12 of impNumbers) as string)

if (character 2 of paragraph 13 of impNumbers) as string = "-" then

set finalString to finalString & " " & (paragraph 13 of impNumbers) & return

else

set finalString to finalString & " (" & (character 1 of paragraph 12 of impNumbers) & (characters 2 through -1 of paragraph 13 of impNumbers) & return

end if

endrepeat

end


end myTextView


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


--------------------------------

This is what I got.


I linked it to a NSTextField in a Cocoa applescript application but it returns on error.


Called: OSACompile Error


OSACompile "Text Test/AppDelegate.applescript"

cd "/Users/Victor/Documents/Xcode Projects/Text Test"

/usr/bin/osacompile -l AppleScript -d -o "/Users/Victor/Library/Developer/Xcode/DerivedData/Text_Test-gcjfkcupycpcwtauqv lttrqnyeme/Build/Products/Debug/Text Test.app/Contents/Resources/AppDelegate.scpt" "Text Test/AppDelegate.applescript"


Text Test/AppDelegate.applescript:12: error: Expected end of line, etc. but found unknown token. (-2741)

Command /usr/bin/osacompile failed with exit code 1



What does this mean?????😼😼😼


Please help me

Apr 13, 2013 5:21 PM in response to skywalkerkenobi

The OSACompile Error means that there is a syntax error in the AppleScript. The Xcode editor doesn't have a compile option like the AppleScript Editor, but you can open a script in the AppleScript Editor by right-clicking on a script file (in the Project Navigator) and choose Open with External Editor. You have a few errors in your script, mainly dealing with your organization (it also looks like an Objective-C piece or two got put in there).


AppleScriptObjC in Xcode does not have an implicit run handler, so you need to make sure your script statements are placed inside a handler - either your own or a delegate handler. Since there is no run handler per se and the application is typically driven by user events (mouse clicks, button presses, etc), you also need a way to start your script (or respond to an event). This can be done by using an IB action handler connected to a button or call a handler when the application starts up, for example from the applicationDidFinishLaunching delegate handler.


I rearranged (and tested) your code using one of my templates - the myTextView property is connected to the text view, and a refresh button (I am guessing that is what you are using) is connected to the doButton_ handler, which gets new information and updates the text view:


script AppDelegate                ##################################################      # MARK: -      # MARK: Script Properties      #      property parent : class "NSObject"      property tickerArray : {"GOOG"} -- the tickers to get                ##################################################          # MARK: -      # MARK: Interface Editor Outlets      #      property myTextView : missing value -- connected to the text view                ##################################################          # MARK: -      # MARK: Interface Editor Actions      #      on doButton_(sender) -- connected to the refresh button           doStuff()      end doButton_                ##################################################          # MARK: -      # MARK: Delegate Handlers      #      on applicationWillFinishLaunching_(aNotification) -- initialize UI items           myTextView's setString_("")      end applicationWillFinishLaunching_                on applicationShouldTerminateAfterLastWindowClosed_(theApplication)           return true      end applicationShouldTerminateAfterLastWindowClosed_                on applicationShouldTerminate_(sender)           return current application's NSTerminateNow      end applicationShouldTerminate_                ##################################################          # MARK: -      # MARK: Script Handlers      #      on doStuff() -- get new information and add to the text view           set finalString to ""           repeat with theTicker in tickerArray                set impNumbers to (do shell script "curl \"http://www.google.com/finance?client=ob&q=" & theTicker & "\" | sed 's/<[^>]*>//g' | grep -A12 \"google.finance.renderRelativePerformance();\"")                if character 1 of paragraph 12 of impNumbers = "+" then                     set posNeg to true                else if character 1 of paragraph 12 of impNumbers = "-" then                     set posNeg to false                end if                set finalString to finalString & (theTicker as string)                if (number of characters of theTicker) < 3 then                     set spacer to "                              "                else                     set spacer to "                     "                end if                set finalString to finalString & spacer & (paragraph 9 of impNumbers)                set finalString to finalString & "          " & (character 1 of paragraph 12 of impNumbers) & ((characters 2 through (number of characters of paragraph 12 of impNumbers) of paragraph 12 of impNumbers) as string)                               if (character 2 of paragraph 13 of impNumbers) as string = "-" then                     set finalString to finalString & "          " & (paragraph 13 of impNumbers) & return                else                     set finalString to finalString & "          (" & (character 1 of paragraph 12 of impNumbers) & (characters 2 through -1 of paragraph 13 of impNumbers) & return                end if           end repeat           set currentContents to myTextView's |string|() as text           set newContents to currentContents & finalString & return -- append           myTextView's setString_(newContents)           myTextView's scrollRangeToVisible_({length of currentContents, length of newContents}) -- scroll to end      end doStuff           end script

Apr 13, 2013 11:39 PM in response to red_menace

2013-04-14 02:35:00.901 Stock Text[25823:303] -[NSTextField setString:]: unrecognized selector sent to instance 0x1006bd040

2013-04-14 02:35:00.909 Stock Text[25823:303] *** -[AppDelegate applicationWillFinishLaunching:]: -[NSTextField setString:]: unrecognized selector sent to instance 0x1006bd040 (error -10000)



The build sucessed but this error was displayed in the all output box.


Also, am I suppose to use label in the object library?


I connected myTextView to a label and doButton to a push button.


Am I doing anything wrong??

Apr 14, 2013 12:44 AM in response to skywalkerkenobi

A label is a type of text field. The error you are getting tells me that you are using a text field instead of a text view. You mentioned a text view earlier, so I figured you were using one so that you could append new ticker stuff to the existing text by pressing a button to refresh/update. In addition to not having scroll bars, a text field uses a different method to set the text, so if you are planning on expanding your list of tickers or want to be able to scroll back to previous information, a text view might be the object to use.


You can either remove the text field you are using now and replace it with a text view, or stick with using a text field and change my posted code to use that. To use a text field, change the last 4 lines of the doStuff handler to the following (I'll leave changing the name of the variable as an exercise for you):

myTextView's setStringValue_(finalString)


Edit: you'll also need to change the line in the applicationWillFinishLaunching handler to

myTextView's setStringValue_("")

Apr 14, 2013 1:16 AM in response to red_menace

Thanks,


I got the text in Text View a NSTextContainer🙂😁, but text field and label still return the error message.


Text View is a NSTextView in an NSScrollView. Label is just NSTextField. and textField is NSTextField.


I reviewed your code and it seems it was already written for NSTextView.


Does this mean I should just replace all the NSTextView with NSTextField?😕

Apr 14, 2013 1:26 AM in response to skywalkerkenobi

Yes, you should probably just replace the text field stuff you have now with the text view (NSTextView in an NSScrollView) and connect the myTextView property to the text view (not the scroll view). The quick project I set up uses a text view so that when a refresh button is clicked new information is appended to the end of the old - it will handle more text better than a text field will.

Xcode text

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