Xojo not getting the value returned by the AppleScript

Hi There

I use Xojo 2022r3.2 on a Mac mini M1. I am currently trying to port on MacOS a Xojo app developed for windows. My app uses Office automation to interact with Microsoft Word. I therefore need to replace the OLE commands by Applescripts.

I therefore created an Applescript to get some of the styles from a word document, putting the styles in a list and returning the list as text to my Xojo app. It works under the script editor, I can see the list returned by the script. The script has been dragged-dropped to the XOJO UI and appears in the list of scripts available for my app.

But the code in Xojo receives an empty result. No error, just the result Xojo gets is empty.

My script “listerstyles” is this:

on run
	tell application "Microsoft Word"
		set monmodel to make new document
		set maliste to {}
		set b to count of Word styles of monmodel
			repeat with a from 1 to b
			set nomitem to name local of Word style a of monmodel as text
			if nomitem contains "titre" or nomitem contains "heading" then
				set end of maliste to nomitem & ","
			end if
			end repeat
		close monmodel saving no
	end tell
return maliste as text
end run

My Xojo code is this:

dim malistemac() as string
dim applescriptresult as string 
applescriptresult = listerstyles
MessageBox(applescriptresult) 'this is for verification propose; this is supposed to display the items returned, but mess box piping up is empty
malistemac() = applescriptresult.Split(",")

In the Xojo debug window the “applescriptresult” variable appears empty (in fact there is nothing in “text” tab, and there are some hexa data in the left column of the binary tab),

Any idea why Xojo does not receive the value returned by the AppleScript?

Many thanks

Pierre

Did you drag the script into the left pane of the IDE ? Your code does not seem to call it.

Probably the plist for getting permissions to do the AppleScript is missing from the Xojo app.

Michel, yes the script is listed in the left panel.

Beatrix, I saw some posts on the “plist” topic but I did not yet figure out how to create it, what to put in it, and how to attach it to my app.

This will be my next task to continue to learn the MAC world, completely new to me.

Thank you

Pierre

You may want to verify that the applescript actually gets the proper value. This will display an applescript dialog showing maliste. If the dialog does not show in the IDE, try the same script in the script editor.

on run
	tell application "Microsoft Word"
		set monmodel to make new document
		set maliste to {}
		set b to count of Word styles of monmodel
			repeat with a from 1 to b
			set nomitem to name local of Word style a of monmodel as text
			if nomitem contains "titre" or nomitem contains "heading" then
				set end of maliste to nomitem & ","
			end if
			end repeat
		close monmodel saving no
	end tell
display alert maliste
return maliste as text
end run

Put this text into a file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>NSAppleEventsUsageDescription</key>
  <string>Demo</string>
</dict>
</plist>

Name it info.plist. Drag it into the project. You should now get a dialog asking you if you want to allow the AppleScript.

3 Likes

Awesome! it works perfectly!

I will look into it closely to understand how this works.

Many thanks Beatrix!

Pierre