Sunday, November 19, 2017

#1 2017-09-04 03:01:48 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Doesn't instantiated script objects (fully) support AppleScriptObjC?

As the title describes, somehow I cannot run ASObjC code from instantiated script objects. I may have forgotten something quite obvious, or didn't get the memo that this isn't possible with ASObjC. Does anyone know why the example below throws an error on NSMakeRect?

Applescript:

use AppleScript version "2.5"
use framework "AppKit"

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
   script x
       on init()
           return me
       end init
       
       on doSomething()
           current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
       end doSomething
   end script
   x's init()
end createInstanceOfX

Offline

 

#2 2017-09-04 07:17:31 pm

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

Re: Doesn't instantiated script objects (fully) support AppleScriptObjC?

I'm not sure why, but FWIW you can make it work in a script library. Change it like this:

Applescript:

use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit"

script forInheritance
   -- required for inheritance
end script

on createInstanceOfX()
   script
       property parent : forInheritance
       on init()
           return me
       end init
       
       on doSomething()
           current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
       end doSomething
   end script
   set x to result
   x's init()
end createInstanceOfX

Save is as a library and call it like this:

Applescript:

use thisLib : script "TestLibx" version "1.0"
use scripting additions

set instance to thisLib's createInstanceOfX()
set x to instance's doSomething()


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#3 2017-09-05 03:45:46 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Doesn't instantiated script objects (fully) support AppleScriptObjC?

I can get it to work without having to turn it into a library by either putting the use framework "Foundation" statement in the script object itself, but not in the main script:

Applescript:

use AppleScript version "2.4"

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
   script
       use framework "Foundation"
       
       on init()
           return me
       end init
       
       on doSomething()
           current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
       end doSomething
   end script
   return result's init()
end createInstanceOfX

… or by using an inheritance script object instantiated at run time:

Applescript:

use AppleScript version "2.4"
use framework "Foundation"

global forInheritance
script
end script
set forInheritance to result

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
   script
       property parent : forInheritance
       
       on init()
           return me
       end init
       
       on doSomething()
           current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
       end doSomething
   end script
   return result's init()
end createInstanceOfX

Perplexingly, the inheritance script object can itself be returned by a handler and everything will work, but the inheriting script object has to inherit from it, not directly from the main script. I suspect that the problem with the original script is some artifact of the compilation process.

Applescript:

use AppleScript version "2.4"
use framework "Foundation"

set forInheritance to createInheritanceObject()

set instance to createInstanceOfX(forInheritance)
instance's doSomething()

on createInheritanceObject()
   script
   end script
   return result
end createInheritanceObject

on createInstanceOfX(forInheritance)
   script
       property parent : forInheritance
       
       on init()
           return me
       end init
       
       on doSomething()
           current application's NSMakeRect(0.0, 0.0, 500.0, 80.0)
       end doSomething
   end script
   return result's init()
end createInstanceOfX


NG

Offline

 

#4 2017-09-05 06:49:05 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2724
Website

Re: Doesn't instantiated script objects (fully) support AppleScriptObjC?

Thank you Shane and Nigel. Nigel's last example will do the trick for me but I cannot think of any logic reason for it why it behaves like this. Just to clarify to make sure that I understand this well:

It's not possible to use AppleScriptObjC both in the root level script object and the child script object at the same time. However to make AppleScriptObjC work in different script objects we have to put an (anonymous) script object in between. Then you can use AppleScriptObjC in the root level script object and the child child script object.

Thanks again!

Last edited by DJ Bazzie Wazzie (2017-09-05 07:13:16 am)

Offline

 

#5 2017-09-05 08:43:27 am

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

Re: Doesn't instantiated script objects (fully) support AppleScriptObjC?

Nigel Garvey wrote:

I suspect that the problem with the original script is some artefact of the compilation process.



Yes, it looks like it has to be created at run time, but something also needs to exist at compile time for the parent property to compile.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#6 2017-09-05 10:45:40 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4421

Re: Doesn't instantiated script objects (fully) support AppleScriptObjC?

Another thing which works is to make sure the script object uses main script's instance of current application:

Applescript:

use AppleScript version "2.4"
use framework "Foundation"

global currentApp
set currentApp to current application

set instance to createInstanceOfX()
instance's doSomething()

on createInstanceOfX()
   script
       on init()
           return me
       end init
       
       on doSomething()
           currentApp's NSMakeRect(0.0, 0.0, 500.0, 80.0)
       end doSomething
   end script
   return result's init()
end createInstanceOfX


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)