Monday, October 26, 2020

#1 2020-02-05 06:50:45 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 416

Calling a handler from Script Libraries, AppleScriptObjC

I was trying the AppleScriptObjC code from OP on this link
https://macscripter.net/viewtopic.php?pid=168044

Applescript:

on textReplace(sourceText, searchText, replaceText)
set cocoaString to current application's NSMutableString's stringWithString:sourceText
cocoaString's replaceOccurrencesOfString:(searchText as string) withString:(replaceText as string) options:(current application's NSCaseInsensitiveSearch) range:{0, (cocoaString's |length|())}
return cocoaString as unicode text
end textReplace

I get this 'error' from Mojave 10.14.3
error "NSMutableString doesn’t understand the “stringWithString_” message." number -1708 from NSMutableString

Shane Stanley book Everyday AppleScriptObjC on page 3 it say...
If you just want to save it as a script library for use in OS X 10.10 or later only, you can save it as either a script file (.scpt) or a script bundle (.scptd).

So I wonder why I get this error on (scpt) extension on above handler saved as Script Libraries

error "NSMutableString doesn’t understand the “stringWithString_” message." number -1708 from NSMutableString

It does work with script bundle...

Regards.

Last edited by Fredrik71 (2020-02-05 07:27:50 am)


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#2 2020-02-05 10:07:27 am

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

Re: Calling a handler from Script Libraries, AppleScriptObjC

You have to import the Foundation framework

Applescript:

use framework "Foundation"

And both as string coercions are redundant

Last edited by StefanK (2020-02-05 10:10:05 am)


regards

Stefan

Offline

 

#3 2020-02-05 10:43:33 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 416

Re: Calling a handler from Script Libraries, AppleScriptObjC

Hi Stefan, thanks for your help...

Take a look at this 2 files, the one with is the handle save it to your Script Libraries folder.
I name my copy as: TextLib_ASOC.scpt (this is no applescript bundle)

The other one is the script that call the function from the handler.

So I have use framework "Foundation" is both of this script.

StefanK wrote:

You have to import the Foundation framework

Applescript:

use framework "Foundation"

And both as string coercions are redundant



Applescript:

use framework "Foundation"
use scripting additions

(*
use myLib : script "TextLib_ASOC"
myLib's textReplace("Hxllo World!", "x", "e")
*)


(*
tell script "TextLib_ASOC"
   textReplace("Hxllo World!", "x", "e")
end tell
*)


(*
script "TextLib_ASOC"'s textReplace("Hxllo World!", "x", "e")
*)


-- textReplace("Hxllo World", "x", "e")
on textReplace(sourceText, searchText, replaceText)
   set cocoaString to current application's NSMutableString's stringWithString:sourceText
   set cocoaString to cocoaString's replaceOccurrencesOfString:(searchText as string) withString:(replaceText as string) options:(current application's NSCaseInsensitiveSearch) range:{0, (cocoaString's |length|())}
   return cocoaString as Unicode text
end textReplace

#==============

Applescript:

use framework "Foundation"
use scripting additions

use myLib : script "TextLib_ASOC"

set resultText to myLib's textReplace("Hxllo", "x", "e") as string


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

#4 2020-02-05 11:10:38 am

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

Re: Calling a handler from Script Libraries, AppleScriptObjC

Your second library script doesn't work. It will always return "1" if the operation succeeds. You have to delete set cocoaString to in the replace line.

If you are using a Cocoa framework import also AppleScript. However Scripting Additions are not used in your library script

Applescript:

use AppleScript version "2.5"
use framework "Foundation"

on textReplace(sourceText, searchText, replaceText)
   set cocoaString to current application's NSMutableString's stringWithString:sourceText
   cocoaString's replaceOccurrencesOfString:searchText withString:replaceText options:(current application's NSCaseInsensitiveSearch) range:{0, (cocoaString's |length|())}
   return cocoaString as text
end textReplace

And –as mentioned – remove the redundant coercions

In the calling script importing the library script is sufficient as there is no Scripting Addition nor Foundation terminology

Applescript:

use myLib : script "TextLib_ASOC"

set resultText to myLib's textReplace("Hxllo", "x", "e")

And – once again – remove the redundant coercion


regards

Stefan

Offline

 

#5 2020-02-05 11:39:20 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 416

Re: Calling a handler from Script Libraries, AppleScriptObjC

Thanks so much Stefan it works now as script.

The strange thing was Applescript bundle didn't care or complain about any error... (1st script)
So I thought for a second my code was correct or it confused me little.

The 2nd script 'handle' I did some change but thanks to point out for me the right direction. smile

Best Regards.


I could teach you to cook but I couldn't do anything if you do not have desire or commitment for it.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)