Need help with a macro

I have a macro I run in another automation program. Here is what it does. I have a sermon template that has some Text set up (?Title) (?sdat) (?series). I ago to a blank line and type (?asTitle) I get a fill-in file once I input a date, Title and series name. It will automatically fill that in on my new file.
I want this to work within Keyboard Maestro. I the program I currently use is very dependent on the internet. I don't have internet available where I use this macro. Any help ideas apreciated. Or where I could find a similiar macro I could modify.

Welcome to the forums; I'm sure we'll be able to help you out.

With that said, I'm not sure I fully understand what your macro does—what is a "fill-in file" and how does it relate to typing ?asTitle? Your example was ?Title, so what's the added as bit at the front, and what exactly happens when you type one of those things?

-rob

1 Like

Use the Prompt for Snippet action or the Prompt for User Input action to collect the data and the Insert Text by Pasting action to paste in the desired format, perhaps using the LongDate token or ICUDateTime token tokens for the date.

If you give us more of an example of how you input the data and how you want it output, then we can probably give more specific assistance.

I have an outline template that hast the place holder ?Title where the title will go. ?sdat for date information to be put. Then in the footer where it has Title:?Title will be replaced. The name of the applescript is ?asTitle. I type ?asTitle. The whole outline is highlighted. I’m prompted for date in @ day, month, year then a choice of ams or pms. Then prompted for a series name that will replace a ?series place holder. It will go to footer replace ?Title with Title Ive put in and un highlight.
I also need to do my naming a file name macro. The way works is. I type ?file. Then prompted to put in year the month 01-12 format then day it has me choose ams or pms. It then puts in - prompts me for title and the adds a - then lets me choose - outline, slide, notecard or doc. Once done i hit enter and it saves the file. Would look like 20240429pms_-_test title here - OUTLINE.

Yes, but what application are you doing this in?

Getting, or letting you manually input, the data you want to use is easy -- the tricky bit is "inserting" it into your template, because that bit is very application-dependent.

Im doing this in Word. Here are a couple of PDF's I made this morning via illustration.


Right now I use an app called Text Expander to trigger all this action. I want to switch it to Keyboard Maestro. Main reason is Text Expander wants to "phone iHOME" too much. I dont have internet access at the office. If I can get everything over to KBM I will be better off. The macro works fine at the house. But the office no. I have to bring the computer HOME get it online hope I don't have to reboot at the office or I lose connection to TE. I also would be able to drop a "ransom" subscription each year.

Is that an actual Word form letter form, or is it just a Word document with your specially-coded text in place? If the latter, then I think @peternlewis has the best suggestion:

Just use Prompt for User Input to collect each of the values you need, saving each in a local_theTitle (or whatever name you prefer) variable. Then you'd need to use Word's Find and Replace feature to search multiple times, once for each template value.

I took a few minutes to cobble together a functional demo:

Animated GIF of Word

word_replace

I will warn you, however, that this is very janky—Word is quite slow with the replace all stuff, and I had to insert timing delays in many places to make it work. If you'd like to try it, here's the macro:

app-Word form replacements Macros.kmmacros (11 KB)

This macro group consists of two macros: One that gathers input, and the other that interacts with Word. I wrote it this way so the Word stuff only had to be written one time. All the Word commands are in a subroutine, and it takes two things as input: The code word and its replacement value.

Here's the demo word document you can test it with:

word form demo.zip (11.4 KB)

If you try to use this yourself, you'll have to add additional subroutine calls (with the delay between each) for each code value you want to replace. But this is probably not the best way to do this, given how really horrible Word seems about wanting to do these things. In fact, it's worse than it looks in the demo, because every time you do a replacement, you have to get rid of this dialog box, too:

It was appearing outside my recorded area in the GIF, so you don't see it, but there's a keystroke in the macro just to get rid of it.

But really, I wouldn't recommend using my macro—it will probably take some timing tweaks to even make it work, and it seems quite likely to break if the speed of the computer is different at a given time when it's launched.

I think a much better solution would be to put the text itself into Keyboard Maestro, where you could then do lightning-fast find-and-replace actions. You could even format it with either Markdown code or HTML, then send that output to an editor capable of handling one of those forms, so it appeared with your formatting.

There may be other approaches along these lines that work well, but if you can take Word out of the picture somehow, I think it's possible to write a much better (and much simpler!) macro than what you see here.

-rob.

And here's a version that works with Pages instead of Word. Pages' main advantage is that the Find/Replace dialog just appears onscreen, and you can use it over and over without having to remove a dialog each time.

I still had to slow things down a bit, but this macro runs much quickly (and smoother) than does the Word version, as you can see in the GIF:

Animated GIF of Pages

Pages_replace

Pages - replacement demo Macros.kmmacros (15 KB)

And here's the sample Pages document I used:

Demo Pages.zip (51.8 KB)

I think using Pages, if you can, would work better than Word; this version seems more robust and less likely to fail. It's also much cleaner, without so much stuff going on in the window.

-rob.

I'm wondering if, in the background, TextExpander is actually using an AppleScripted S'n'R in Word. Here's a KM version:

Fill Word Template from Prompt.kmmacros (3.5 KB)

Image

@Macmike -- open up your Word document then fire up the above macro, fill in the form, and see if what you want to happen... happens.

1 Like

I'd completely forgotten that Office had re-embraced AppleScript! Much better than my solutions!

-rob.

My Text expander one uses an AppleScript. Iwill try and do a copy and put on here.

On my house computer now. Here is the apple script.

--- select all text
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 "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

-- send right arrow key to unselect text
return "%>"

If I could incorporate that into Keyboard Maestro would be good.

This is what I had set up in TE to save my files from the line where I would type the file name to save. I would use ?File to trigger it.
Oops didn't work. It is a lot of chosing from a list involved.

Year Month Sunday AM/PM File Name _-_File Type

-

-

You can use the Execute an AppleScript action to execute AppleScripts.

The AppleScript is already in the macro above, you just need to add the "-- replace footer as well" section and it should work.

Yes, Trying to get the applescript triggered from Key Maestro. Is the issue. The file is a Word Template I call asSermonOutline. As lets me know the Applescript place holders are there.

I'm confused; as @peternlewis notes above, you can run the AppleScript you currently have directly in Keyboard Maestro. The only thing you'd need to do is pass in the values, which @Nige_S' macro shows you how to do.

-rob.

1 Like

Question. Haven’t had time to get back into the macro yet. The execute AppleScript action. Do I put my script in it or in a folder that I put a call to the folder in the action?

You can do either; I can't speak for others, but I try to keep the code directly in the macro, as it's easier to edit and update that way. Look at the macro I posted today for an example:

-rob.

1 Like