3

I am an experienced Cocoa programmer and an Applescript newbie. I need to program a Microsoft-based report generator. I started with the Applescript bridge and basically hit a wall with Word. I am now trying with ApplescriptObjC, and I am struggling like hell.

I am even stuck in the stupid step of opening a file when its path is reported by a method written in Objective-C. The objective-C class code is very simple (as a contrived example):

@implementation Reporter
- (NSString *)templatePath:(NSString *)fileName
{
    NSString *fullPath = [self.currentDirectory stringByAppendingPathComponent:fileName];
    return  fullPath;
}
@end

Side Note: the current directory is not the built app directory. Here is one of my tries to use it:

on createReport()
    set my myReporter to Reporter's alloc()'s init()
    set WordApp to the application "Microsoft Word"
    set FinderApp to the application "Finder"
    set theWordTemplatePath to myReporter's templatePath:"Rapport.docx"
    set theWordTemplatePath to theWordTemplatePath as text
    tell FinderApp to open POSIX file theWordTemplatePath
end createReport

This one gets this error:

*** -[CMDAppDelegate applicationWillFinishLaunching:]: Finder got an error: Can’t make «class ocid» id «data optr0000000080B00E0080600000» into type integer. (error -1700)

I tried many variations, and despite many hours trying, I still can't have Word open my file.

What am I doing wrong?

1 Answer 1

2

For some reason, forms like POSIX file x and alias y often have to be rewritten as pPath as POSIX file and hfsPath as alias in ASObjC.

Try this:

set my myReporter to Reporter's alloc()'s init()
set theWordTemplatePath to myReporter's templatePath:"Rapport.docx"
set tPath to ((theWordTemplatePath as text) as POSIX file) as alias
tell application "Microsoft Word" to open tPath
Sign up to request clarification or add additional context in comments.

2 Comments

You are right that the solution is to write open theWordTemplatePath as POSIX file instead of open POSIX file theWordTemplatePath. You are wrong regarding the syntax however. The infix syntax with colons is correct.
Since a part of my answer is wrong, I edited my answer to remove this part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.