I'm trying to get a Mac Word faux-plugin to work and I'm running into a few weird issues. Sorry - wall of text incoming!
I realize I'm touching on the intersection of several voodoo topics here, so please bear with me.
Private Declare Sub MyLibOpenCustomUI Lib "/Library/Frameworks/MyFramework.framework/MyFramework" ()
data parameter is nil
NSLog(@"fieldText : %@", [field fieldText]);//data parameter is nil
set theIndex to theIndex as integer
tell application "Microsoft Word"
set theField to field theIndex of active document
return field text of theField
return missing value
Microsoft Word got an error: Can’t get active document. (error -1728)
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
'this works from the VBA debugger
'I _cannot_ get it to work from the VBA when launched from a button in a document...
'window closes immediately w/o input
Dim scriptToRun As String
Dim output As String
Dim command As String
command = "osascript ~/Library/Application\ Scripts/com.microsoft.Word/OpenMyUI.scpt"
output = popen(command, "r")
'If you want this to stick around, you have to do something like...
'MsgBox "stick around, window"
--reference our framework
use framework "/Library/Frameworks/MyFramework.framework"
property theWindowLauncher : class "MyWindowLauncher"
set theReturn to theWindowLauncher's openUpdateOutput
I still can't answer the issue with option 1 (straight C library reference and broken Word fields), but for option 2 (call out to AppleScript via osascript), I figured out that the window actually did appear - but behind every other window I had up (across all apps) so I never realized it was there.
Solution (option 2): It was then a matter of telling the window to come to the foreground and it "worked" - albeit with the caveat that it was now a globally modal window (not desired).
//wc is our window controller NSWindow* theWindow = [wc window]; [theWindow setTitle:windowTitle]; [NSApp activateIgnoringOtherApps:YES]; [theWindow setLevel:NSMainMenuWindowLevel]; [theWindow makeKeyAndOrderFront: self]; [NSApp runModalForWindow: theWindow];
For the moment that's "ok enough" to get by. Not what anyone wants, but OK.
I don't really like this process at all (vba -> C popen -> osascript -> AppleScript -> obj-c class method).
Why go through popen -> osascript -> AppleScript? For the life of me I couldn't get MacScript to successfully call the AppleScript file. Also tried using embedded AppleScript. Neither worked. Just got the usual "error 5."
My AppleScript utilizes
use framework to make this work, so my guess is MacScript doesn't support this (but that's just a guess). Other test AppleScript (file or embedded) that didn't incorporate
use framework ("Hello, World!") worked fine.
Later I'll probably circle back and see if I can make option 1 work by moving the Word interaction to some sort of external setup - that might work to bypass the weird "data is nil" issue and the periodic inability to get ahold of the active document. Wish I understood more of what's going on with that.