You are not logged in.
Hi Craig
I've tried to modify your example such that the "Set Text" button loads an rtf document from a file and displays it in the Text View. Here's the crux of what I thought was a simple edit to the setTextViewFromTextField subroutine :
Applescript:
on setTextViewFromFile_(sender)
textView's readRTFDFromFile_(choose file with prompt "Select your subtitles file:" of type {"rtf"} without invisibles)
end setTextViewFromFile_
But this is throwing up an error as follows:
-[NSButton readRTFDFromFile:]: unrecognized selector sent to instance
Which seems to suggest to my badly out of my depth cocoa-newbieness that textView is thinking it belongs to the NSButton class rather than the NSTextView class ... clearly I've messed something up. Can you point me in the right direction so I can get where I'm trying to go.
Ultimately I'd like my little test app to read in and display an rtf file, then I would want to parse that file line by line and extract info for font, size and style, and to spit that out as an XML file. So far I have all that working via AppleScripting the TextEdit app (and then programmatically saving that out the parsed file as an XML) but as a next step I'd like to be able to skip the TextEdit step and do it in AppleScriptObjC, including importantly the displaying it part!
Greatly appreciate any help you can offer, especially what the heck is screwy in my logic in the above seemingly oversimplified code
Cheers
Andy
EDIT: So I think I realized the first of my errors above was in not correctly connecting the App Delegate to the NSTextView item in Interface Builder ... having connected that up properly (I think) I'm now no longer getting the NSButton readRTFDFromFile:]: unrecognized selector error, but instead am now getting:
-[NSAppleEventDescriptor length]: unrecognized selector sent to instance
Bugger. Out of the frying pan and into the fire
EDIT 2: Aha! Should have used POSIX path.
Last edited by andymees (2010-10-21 08:54:27 am)
Offline
How is the performance relative to Objective C? I don't want to learn Objective C and Applescript notation is good enough for what I need to do.
What are everyone's thoughts?
Offline
How is the performance relative to Objective C?
It depends very much on what you're doing. For some things, AS will be too slow -- but then for some things, Objective-C is too slow and people resort to straight C.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
Can you give an example of when to use this Language as opposed to Objective C? I come from the windows world and this seems like what Microsoft did with .NET and CLR.
I prefer the syntax of Applescript because the Objective C syntax is foreign to me (Java/C#).
When should you use Applescript? Is this interpreted? Don't you get compiled runtimes in the end?
Offline
Can you give an example of when to use this Language as opposed to Objective C?
The main use case is when you are using AppleScript already -- so generally when you are scripting another app or apps. That's not to say it can't be used for stand-alone stuff, but that's no really what it's designed for, nor is it ideal for that.
I come from the windows world and this seems like what Microsoft did with .NET and CLR.
Not really. It's more about giving scripts that drive other apps access to Cocoa -- mainly for UI, but as is the nature of such things, anything else that will help them.
I prefer the syntax of Applescript because the Objective C syntax is foreign to me (Java/C#).
If you know C, Objective-C syntax probably takes half-a-day at most to get the hang of. And you use a similar approach in AppleScriptObjC -- the same goes for using the other bridges, for Python, Ruby, etc. Cocoa and Objective-C are closely entwined.
Is this interpreted? Don't you get compiled runtimes in the end?
You get a mixture. The AS component is loaded at run-time -- it depends very much on the dynamic nature of the Cocoa runtime.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
Applescript:
on setTextViewFromTextField_(sender)
set textFieldValue to textField's stringValue()
textView's setString_(textFieldValue)
end setTextViewFromTextField_
I wonder how can I check if textField is empty?
I've add if is not "" but don't work
Applescript:
on setTextViewFromTextField_(sender)
set textFieldValue to textField's stringValue()
if textFieldValue is not "" then
textView's setString_(textFieldValue)
beep
end if
end setTextViewFromTextField_
It worked with:
Applescript:
on setTextViewFromTextField_(sender)
set textFieldValue to textField's stringValue()
if textFieldValue's isEqualToString_("") then
else
textView's setString_(textFieldValue)
beep
end if
end setTextViewFromTextField_
but should be a more elegant/correct way to avoid else statement?
P.S. using Xcode 4.0.1
Offline
You have to coerce the result to text. Something like:
if textFieldValue as text = "" then
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline
You have to coerce the result to text. Something like:
if textFieldValue as text = "" then
Thanks, I need not equal so this work fine:
if textFieldValue as text isn't "" then
Offline
My favorite way to determine an empty string is the length method.
There's no overhead and you can compare directly to an AppleScript integer value
Applescript:
on setTextViewFromTextField_(sender)
set textFieldValue to textField's stringValue()
if textFieldValue's |length|() is not 0 then
textView's setString_(textFieldValue)
beep
end
end setTextViewFromTextField_
Last edited by StefanK (2011-06-01 04:52:39 am)
regards
Stefan
Offline