Wednesday, December 13, 2017

#51 2010-10-21 06:10:08 am

andymees
Member
Registered: 2006-07-13
Posts: 7

Re: AppleScriptObjC in Xcode Part 1

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 roll

EDIT 2: Aha! Should have used POSIX path.

Last edited by andymees (2010-10-21 09:54:27 am)

Offline

 

#52 2011-01-06 04:01:23 pm

axwack
Member
Registered: 2010-12-24
Posts: 14

Re: AppleScriptObjC in Xcode Part 1

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

 

#53 2011-01-07 05:21:22 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: AppleScriptObjC in Xcode Part 1

axwack wrote:

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/

Offline

 

#54 2011-01-07 08:42:50 am

axwack
Member
Registered: 2010-12-24
Posts: 14

Re: AppleScriptObjC in Xcode Part 1

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

 

#55 2011-01-07 04:41:57 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: AppleScriptObjC in Xcode Part 1

axwack wrote:

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/

Offline

 

#56 2011-05-30 10:57:10 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: AppleScriptObjC in Xcode Part 1

Craig Williams wrote:

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

 

#57 2011-05-30 05:19:36 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5199

Re: AppleScriptObjC in Xcode Part 1

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/

Offline

 

#58 2011-05-31 09:11:49 am

crgand
Member
From:: Milano, Italia
Registered: 2008-11-15
Posts: 39
Website

Re: AppleScriptObjC in Xcode Part 1

Shane Stanley wrote:

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

 

#59 2011-06-01 05:52:18 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: AppleScriptObjC in Xcode Part 1

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 05:52:39 am)


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)