Applescript and Cocoa

Hi all,
I’m working to update some old apps which make extensive use of Applescripts. They interact with InDesign and need to work with values returned from the Applescripts. Everything has been fine and dandy using Carbon calls but under Cocoa some critical calls are failing.

For the record, I can get basic ‘Hello World’ scripts to work fine so I can confirm the scripts are being called correctly. However, if I try to run the following, it fails with an “AppleEvent timed out” error

on run try set mypath to ((path to application support from user domain) as string) return mypath on error errmess return errmess end try end run

The script runs fine from Script Editor. When run as Carbon under RS 2012 it also works fine. I do note in the XOJO docs “Cocoa does not support (yet ?) sending AppleEvents” but I’m hoping someone can give me an idea of how to get the above example to work under Cocoa.

I’d prefer to go sans plugins although I do have access to the MBS complete set (I did look for options there but the docs left me lost). I’ve not used declares previously but am willing to give them a try if it can get things working and returning results.

Any assistance would be greatly appreciated.

PS. Oh, and before anyone suggests using XOJO to get the path in the example above, it MUST to be traced from the Applescript based on some information returned from the InDesign document. :slight_smile:

I really can’t imagine what you want to do. Anyways…

You can try using the shell for your AppleScripts. MBS is a bit more complicated. But I have a wrapper for NSApplescriptMBS. See http://www.mothsoftware.com/content/xojo/ . The API is a bit ugly because it’s very old. But you can use the wrapper to do your AppleScripts dynamically with Xojo code.

Thanks, but I’ve already tried it and I got the same error. I’ve been playing with options for the past few hours and yours was one which had popped up.

I also tried a direct call through a shell but couldn’t get it to return a result.

:slight_smile: Worth a try.

I’m not in front of my computer so I can’t check. But I remember getting error reports like this from some customers. But I don’t remember what I did to fix this. Restart the computer?

Have you tried to simplify the AppleScript. The run handler isn’t necessary here.

If you are starting with the shell try a one liner like

Is that the whole AppleScript ? Don’t you want to

tell application "System Events"

The example above is the smallest useful script I could test to try and get a result. The actual script I need to call in this instance is over 1100 lines.

[quote=211097:@Michel Bujardet]Is that the whole AppleScript ? Don’t you want to

tell application "System Events"

No, it’s not needed.

Interestingly I’ve discovered it will work if I wrap a Finder tell block around it (although it shouldn’t be necessary). The problem is that’s it’s already running in the middle of a lengthy InDesign tell block and it will involve a lot of to-ing and fro-ing.

If you are starting with the shell try a one liner like
return ((path to application support from user domain) as string)

I’m afraid that failed as well.

The Tell Finder block is not the answer. It would need a complete rewrite of the core of the script.

in the try part mypath is filled but not defined.

in the onerror part errmess is not defined nor filled.

@Emile Schwarz: that’s the way AppleScript works. Both myPath and errmess are fine.

@Kim: I’ll have a look in the afternoon. But 1.100 lines of AppleScript is masochistic.

:slight_smile: It has to do a lot - it’s quite impressive (if I do say so myself) - but yes, masochistic could be a reasonable description.

Beatrix:

You are right (as surprising to me it can be). Sorry for the buz.

[quote=211085:@Kim Kohen]For the record, I can get basic ‘Hello World’ scripts to work fine so I can confirm the scripts are being called correctly. However, if I try to run the following, it fails with an “AppleEvent timed out” error

on run try set mypath to ((path to application support from user domain) as string) return mypath on error errmess return errmess end try end run[/quote]

The plot thickens. I created a script in Script Editor with that very code, called it “Test”, saved it, dragged the scpt into a brand new Desktop project, and in a Button, went :

MsgBox Test

It gives me the path, as expected… No error.

2015R2.4.

Is there anything special about your app, like it being sandboxed or something ?

I forgot to say what Michel wrote above:

It gives me the path, as expected… No error.

(but I put the code in a running project instead of a brand new, i a MouseDown event).

Nota: on Window1.Open, I get an error message.

[quote=211129:@Michel Bujardet]The plot thickens. I created a script in Script Editor with that very code, called it “Test”, saved it, dragged the scpt into a brand new Desktop project, and in a Button, went :

MsgBox Test

It gives me the path, as expected… No error.

2015R2.4.

Is there anything special about your app, like it being sandboxed or something ?[/quote]

It gives me the Timeout Error

I have this Timeout Error problem since upgrading to OS X 10.10.5


using a constant and shell gives me the path

constant ( name = test)

/usr/bin/osascript <<-EOF on run try set mypath to ((path to application support from user domain) as string) return mypath on error errmess return errmess end try end run EOF

button

[code]dim sh as new shell
sh.Execute test

MsgBox sh.Result[/code]

[quote=211129:@Michel Bujardet]The plot thickens. I created a script in Script Editor with that very code, called it “Test”, saved it, dragged the scpt into a brand new Desktop project, and in a Button, went :

MsgBox Test

It gives me the path, as expected… No error.

2015R2.4.

Is there anything special about your app, like it being sandboxed or something ?[/quote]

No, in fact what you did is exactly what I did when I started having problems. New script with the code above and dragged into a new project. The only thing different is I’m calling it from the App.open event (the app runs with no interface).

[quote=211133:@Emile Schwarz]

Nota: on Window1.Open, I get an error message.[/quote]

You’re right, it does not work on Open Event

in a timer (mode off) Action
MsgBox Test

in Open
Timer1.mode = 1

works

[quote=211134:@Axel Schneider]It gives me the Timeout Error

I have this Timeout Error problem since upgrading to OS X 10.10.5


using a constant and shell gives me the path

constant ( name = test)

/usr/bin/osascript <<-EOF on run try set mypath to ((path to application support from user domain) as string) return mypath on error errmess return errmess end try end run EOF

button

[code]dim sh as new shell
sh.Execute test

MsgBox sh.Result[/code][/quote]

Thanks Alex, that is working. I tried a shell before but I was getting errors - I don’t think it liked the run handlers. The big question will be if I can get this to run from within the InDesign block but I can’t check that properly until tomorrow morning.

I’m a little intrigued by the timeout errors though. Just FYI I’m still on 10.10.3 so I don’t think it’s OS version related. The fact that the scripts run fine from Script Editor - and when called from RS 2012 - tends to point to the Cocoa AppleEvent compatibility issue mentioned in the docs.

Thanks to everyone who sent a response, I’ll report back tomorrow after testing the app with the full script.