Thursday, April 27, 2017

#1 2016-11-05 08:21:12 am

StevenH
Member
Registered: 2016-09-17
Posts: 29

Xcode Object C App -> Execute Applescript

Hi there,

i have a pure ObjectC app written with Xcode.
No i'd like to execute an applescript in the sourceBundle with parameters.

I have this solution and it works for
script "testscript.scpt"
handler "test_message"
parameter message

Applescript:

NSString* path = [[NSBundle mainBundle] pathForResource:@"testscript" ofType:@"scpt"];
NSURL* url = [NSURL fileURLWithPath:path];
NSAppleScript* theScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:nil];
NSDictionary* theErrorDictionary = nil;

// build Apple event to invoke user-defined handler in script
NSAppleEventDescriptor* event = [NSAppleEventDescriptor appleEventWithEventClass: kASAppleScriptSuite
eventID: kASSubroutineEvent
targetDescriptor: NSAppleEventDescriptor.nullDescriptor
returnID: kAutoGenerateReturnID
transactionID: kAnyTransactionID];


// Add positional parameters
NSAppleEventDescriptor* params = [NSAppleEventDescriptor listDescriptor];
[params insertDescriptor: [NSAppleEventDescriptor descriptorWithString:@"I am a message"] atIndex: 1];

// Add Subroutine
[event setDescriptor: [NSAppleEventDescriptor descriptorWithString: @"test_message"]
forKeyword: keyASSubroutineName];
[event setDescriptor: params
forKeyword: keyDirectObject];

NSAppleEventDescriptor* theResult = [theScript executeAppleEvent:event error:&theErrorDictionary];
NSLog( @"Got result: %@", theResult );

This is the applescript

Applescript:

on test_message(message)
   display dialog message
end test_message

No i'd like to pass a dictionary (ASrecord) to applescript, but NSAppleEventdescriptors do not support dictionaries or arrays. Any ideas on how to call an applescript handler with a dictionary parameter?

Offline

 

#2 2016-11-05 08:29:33 am

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

Re: Xcode Object C App -> Execute Applescript

StevenH wrote:

... but NSAppleEventdescriptors do not support dictionaries or arrays.

Hi,

they do support dictionaries (record) and arrays (list), you are using one ([NSAppleEventDescriptor listDescriptor])

Consider to write a bridge to AppleScriptObjC, the framework provides to run scripts without the event descriptor dance.


regards

Stefan

Offline

 

#3 2016-11-05 09:28:53 am

StevenH
Member
Registered: 2016-09-17
Posts: 29

Re: Xcode Object C App -> Execute Applescript

The bridge solution sounds good.

Any good starting point for that? Never done before.

Offline

 

#4 2016-11-05 09:35:27 am

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

Re: Xcode Object C App -> Execute Applescript

Please search on MacScripter for loadAppleScriptObjectiveCScripts.

There are a few related topics.


regards

Stefan

Offline

 

#5 2016-11-05 06:21:43 pm

StevenH
Member
Registered: 2016-09-17
Posts: 29

Re: Xcode Object C App -> Execute Applescript

Thank you!

I now tried a lot of stuff, but basically this line would not work - throws exception

Applescript:

[[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];

Full code from main file

Applescript:

#import <Cocoa/Cocoa.h>
#import <AppleScriptObjC/AppleScriptObjC.h>

int main(int argc, const char * argv[]) {

[[NSBundle mainBundle] loadAppleScriptObjectiveCScripts];

return NSApplicationMain(argc, argv);
}

Exception:

Applescript:

[NSBundle loadAppleScriptObjectiveCScripts]: unrecognized selector sent to instance 0x6000000818b0
2016-11-06 00:16:56.850566 MyApp[2717:77340] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSBundle loadAppleScriptObjectiveCScripts]: unrecognized selector sent to instance 0x6000000818b0'
*** First throw call stack:
(
   0 CoreFoundation 0x00007fffa92147bb __exceptionPreprocess + 171
   1 libobjc.A.dylib 0x00007fffbd981a2a objc_exception_throw + 48
   2 CoreFoundation 0x00007fffa9294d84 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
   3 CoreFoundation 0x00007fffa9187fc3 ___forwarding___ + 1059
   4 CoreFoundation 0x00007fffa9187b18 _CF_forwarding_prep_0 + 120
   5 MyApp 0x000000010000efea main + 74
   6 libdyld.dylib 0x00007fffbe25d255 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

Using Xcode 8, OSX 10.12

Offline

 

#6 2016-11-05 06:28:45 pm

StevenH
Member
Registered: 2016-09-17
Posts: 29

Re: Xcode Object C App -> Execute Applescript

OK solved that one. Forgot the binary/framework include.

Thank you for pointing it out!

Offline

 

#7 2016-11-07 03:39:38 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Re: Xcode Object C App -> Execute Applescript

Add-On:

While the above mentioned way works great for text script files (*.applescript), is there also a possibility to send handler calls (with parameters) to precompiled *.scpt files and return a result?

Offline

 

#8 2016-11-07 05:22:37 am

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

Re: Xcode Object C App -> Execute Applescript

Jonathan Engelhardt wrote:

While the above mentioned way works great for text script files (*.applescript)

They're compiled to .scpt files as part of the build process -- starting out as .applescript is irrelevant.

is there also a possibility to send handler calls (with parameters) to precompiled *.scpt files and return a result?

When -loadAppleScriptObjectiveCScripts is called, it attempts to load all the .scpt files in a bundle. But you obviously need a script name and parent defined to be able to use them.


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

Offline

 

#9 2016-11-07 06:18:16 am

Jonathan Engelhardt
Member
Registered: 2010-06-11
Posts: 57

Re: Xcode Object C App -> Execute Applescript

Great! Thank you!

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.127 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)