Need help with a macro

'''AppleScript
--- select all text

set inst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"

set theTitle to getvariable "Local_Title" instance inst

set theDate to getvariable "Loal_Date" instance inst

set theSeries to getvariable "Local Series" instance inst

end tell

tell application "System Events" to tell process "Word"

click menu item "Select All" of menu 1 of menu bar item "Edit" of menu bar 1

end tell

tell application "Microsoft Word"

set myFind to find object of selection

tell myFind

execute find find text "?Title" replace with "" replace replace all

execute find find text "?Series" replace with "" replace replace all

execute find find text "?sdat" replace with " , " replace replace all

end tell

-- replace footer as well

set findObject to (find object of text object of (get footer of section 1 of document 1 index header footer primary))

tell findObject

execute find find text "?Title" replace with "" replace replace all

end tell

end tell

Question? Should the Local_Title be ?Title instead?

Can you please share the actual macro? I explained how to do so above. Without it, we're all just still guessing.

-rob.

asTitle Macro (v11.0.2)

asTitle.kmmacros (3.8 KB)

Keyboard Maestro Export

Thank you; that made it very easy to see what the issues were. The following macro works with a simple test Word document I created, so it should work with yours:

Download Macro(s): Fill in Word form.kmmacros (4.1 KB)

Macro screenshot

Macro notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System information
  • macOS 14.4.1
  • Keyboard Maestro v11.0.2

What you were missing in your macro is that the first thing the macro needs to do is to gather your values for the form. This may be different than how TextExpander works—maybe you fill in the details as it runs? And you could do something similar in Keyboard Maestro, but there's no need to, as there are more efficient ways of gathering input.

In this case, I added the first action in the macro, the Prompt for User Input. It collects the values you want to put into the form, and those values are assigned to variables (the Local__... bits in the Prompt for User Input action). Finally, those variables are transferred from Keyboard Maestro into AppleScript for use in your replacement script.

Because AppleScript can't directly use the variables, they're read from Keyboard Maestro and assigned to actual AppleScript variables at the beginning of the script. Those variables, in turn, are what's used in the AppleScript steps that actually search and replace in the Word document. Here's how that flow looks graphically:

Flow of variables from Keyboard Maestro to AppleScript

The other thing you probably want to do is to change your trigger: You don't need to use ?asTitle to launch the macro. In the version I've attached, I set it to ##FORM. But it can be anything that you wouldn't normally type in ordinary text—^#_lM would work as would ggg, as you wouldn't normally type three Gs in a row. Make it easy to type and memorable, but not something you'd otherwise normally type.

Hope this helps—and in the future when asking for help, please include the full macro as part of the original post. It'll save everyone a lot of time trying to figure out what the problems might be.

-rob.

1 Like

Ok my bad. I went back and read. I didn't realize I had to turn the macro on.

There's absolutely no need to highlight the text in the first place -- it's happening because that was in your original AppleScript and @griffman's based his on that.

Instead of using "Select All" you can explicitly set the text to be searched, as I did in my demo macro. This is the AppleScript, updated to include the footer search and replace -- you'll have to change the Keyboard Maestro variable names if you are using ones different to mine.

set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set theTitle to getvariable "Local_Title" instance inst
	set theDate to getvariable "Local_Date" instance inst
	set theSeries to getvariable "Local_Series" instance inst
end tell

tell application "Microsoft Word"
	set myRange to text object of active document
	execute find (find object of myRange) find text "?Title" replace with theTitle replace replace all
	execute find (find object of myRange) find text "?sdat" replace with theDate replace replace all
	execute find (find object of myRange) find text "?series" replace with theSeries replace replace all
	set myRange to text object of (get footer of section 1 of active document index header footer primary)
	execute find (find object of myRange) find text "?Title" replace with theTitle replace replace all
end tell