Problem with passing a variable from KM to AppleScript

I'm stuck again. I want to retrieve the number in a webform like this:

E.g. the number in the 2nd column of the 6th row (6).

I have this macro, where I get an error message saying that thePar doesn't exist:


Test - Retrieve paragraph n from document opened in Ms Word.kmmacros (5.7 KB)

What am I doing wrong here?

Thank you in advance!

As you're using a local variable, you'll need to use:

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set ASVar to getvariable "Local__KMVar" instance inst
end tell

Replace "ASVar" with the name of the AppleScript variable you're trying to set.
Replace "Local__KMVar" with the name of the Keyboard Maestro variable you're trying to reference.

Also, do it all in one script. So in this case:

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set thePar to getvariable "Local__parNum" instance inst
end tell

tell application "Microsoft Word"
	copy object text object of paragraph thePar of active document
end tell
2 Likes

Thanks for your help! Alas, I still get an error:

Screen Shot 2022-04-28 at 17.11.26

Test - Retrieve paragraph n from document opened in Ms Word.kmmacros (6.7 KB)

Does Word perhaps require you to set bookmarks before you can reference paragraph numbers?

No, you can reference them as @ALYB is. But that's the error message for "no such paragraph" -- eg accessing paragraph 92 when the doc only has 6 -- so I suspect that passing the variable is still a problem.

Try checking the number of underscores in the variable name!

1 Like

Good eyes, @tiffle! Or is it the result of being bitten by that before? :wink:

1 Like

:man_facepalming:t2: shoulda seen that.

Sorry guys, I already had suspected that it were those underscores. So I had tried both double and single ones. To no avail.

Local_parNum

I just don't see it. Sorry...

Test - Retrieve paragraph n from document opened in Ms Word.kmmacros (3.7 KB)

Still the same error.

BTW: What's the advised number of underscores for this Local prefix?

Underscores are optional in any variable name. It’s just that in some circumstances, for example KM’s Prompt for User Input action and in the parameters to subroutines, a double underscore has a special significance. You can read about it in the wiki.

2 Likes

Thanks. From the wiki:

If a menu items is separated in to two parts with two underscores (_), then the first part is used as the result, while the second part is displayed in the menu. For example,“B__Good|F__Fail|C__OK|B__Good|A__Excellent”.

1 Like

What happens if, for testing purposes, you dispense with KM variables and just use an explicit number in the Applescript? Is the correct text successfully copied?

There's also personal/team conventions to variable naming. If you use underscores within variable and functions names -- "my_important_var" -- it helps with searches etc to use double-underscores for the prefixes. If, like me, you're a camelCaser then a single underscore is enough.

Total guess before I have a try myself -- but didn't I see somewhere that KM variables are always text? My guess is that Word isn't coercing to a number, so you're trying to get paragraph "3" rather than "paragraph 3".

Try setting the set thePar... line to:

set thePar to 0 + (getvariable "Local_parNum" instance inst)

...to force the coercion, and see what happens.

2 Likes

What happens if, for testing purposes, you dispense with KM variables and just use an explicit number in the Applescript? Is the correct text successfully copied?

Yes it is.

Great. You solved it. Thank you very much.

1

For my own benefit, is that the same as filtering as a calculation?

And for those who want to use this nice technique:

Retrieve paragraph n with styled text from document opened in Ms Word.kmmacros (5.1 KB)

Perhaps I should post it to the macro library?

How would that work?