Manipulate KM’s Display Text Window

With a little AppleScript you can do things with KM’s Display Text window. (This is the window that is created by the Display Text action.)

# tell application "Keyboard Maestro Engine" to activate # Only needed when another app is frontmost
tell application "System Events"
  tell application process "Keyboard Maestro Engine"
    tell window "Keyboard Maestro - Display Text"
      set position to {20, 45}
      set size to {500, 309}
      tell button "OK" to set focused to true
    end tell
  end tell
end tell

[See the post below for the script as plug-in action.]
‌‌

The script …

  • moves the window towards the upper left corner of the screen
  • sets it to a fixed size
  • gives focus to the OK button, so that it responds to a single press of the Return key

Set the desired values for size and position in the script. If you don’t need any of the three features, comment out the corresponding line.

Put the script into an Execute AppleScript action after the Display Text action.

If there is something that pushes away the window from the foreground (e.g. another action), then you have to uncomment the first line of the script to make the window frontmost again.


Here is an example macro with the script:

[example] Manipulate KM’s Display Text Window.kmmacros (5.1 KB)
↓ See the post further below for an improved version (plug-in action) ↓

9 Likes

Thanks, Tom. Great macro/script! :+1:

Just added to Best Macro List.

I have built a plug-in action from the script:

[Moderator] @Tom has updated his plugin [in the below post](https://forum.keyboardmaestro.com/t/manipulate-km-s-display-text-window/6969/12?u=jmichaeltx).

Manipulate a Display Text Window.zip (11.7 KB)



The plug-in action does exactly the same as the script above, but it is more convenient to set the parameters for the window.

To install the action…

  1. drop the zip file onto the KM icon in the dock,
  2. restart KM Engine and Editor.
  • The action should now be in your action inventory. (Folder “Third-Party Plug-ins”)

To use the action place it anywhere in your macro after a “Display text in a window” action.

For example like this:

You can repeat this for every “Display text in a window” action, so you can nicely distribute multiple windows over your screen.

Position: The X and Y screen coordinates separated by a comma, the space is optional. “0, 0” is the top left corner of the screen. “0” is the same as “0, 0” and “0,0”, “300” is the same as “300, 0”, etc. Leave the field blank to allow the standard behavior of the window (i.e. it remembers its previous position).

Size: Width and height of the window. Leave the field blank to allow the standard behavior of the window (i.e. it remembers its previous size).

So you can for example set a fixed Position while leaving the Size field empty to allow the window to restore its previous size, or vice versa.

Focus OK button: gives focus to the OK button, so that it responds to a single press of the Return key.

Make window active: Use this if something pushes away the window from the foreground; for example if you have certain other actions between the “Display text in a window” action and the “Manipulate Most Recent…” action.

4 Likes

Very useful, thank you!

Do you think it would it be possible to make a similar plug-in action to manipulate KM’s various palettes?

If you can script them with AppleScript or AppleScript UI scripting, then it is certainly possible.

Interesting. I don’t know, does anybody else know if the palettes can be controlled by AppleScript or AppleScript UI?

Why don’t you just try it? You have the script in the first post.

To answer your question: yes, it seems to work also for palettes. This for example repositions my Global KM palette:

tell application "System Events"
  tell application process "Keyboard Maestro Engine"
    tell window "Keyboard Maestro"
      set position to {20, 45}
    end tell
  end tell
end tell

Replace the window name with the name of the palette you are targeting.

LIke @Tom says, just try it.
@ccstone has a script published in this forum that provides detailed window info.

Here's a simple version:

tell application "System Events"
  tell application process "Keyboard Maestro Engine"
    
    set winList to windows
    set winNameList to name of every window
    set AppleScript's text item delimiters to linefeed
    
    winNameList as text
    
  end tell
end tell


When I had a palette with the name of " [WIN Palette]" open:

####Results

[WIN Palette]
Keyboard Maestro

Thank you very much for good advice! I’m an AppleScript/KM novice, but based on your input I managed to put the below script together. It seems to work fine for moving single palettes that are not transient (it does unfortunately not work for palettes “for one action” or conflict palettes because they disappear when activating this script), although I have made only very limited testing. Note that it moves the palette to the center of the screen, which is what I prefer, but this is easy to change. The script is not generalized to be able to handle multiple palettes being open simultaneously. If you do not have SwitchResX you can comment out that part and try to use the SPDisplaysDataType shell function instead; however, this did not work well for me because it reported the maximal screen resolution instead of the current resolution. The bounds function may also be an alternative.

tell application "SwitchResX Daemon"
	set {screenWidth, screenHeight} to get {width, height} of (current mode of main display)
end tell
tell application "System Events"
	-- set screenWidth to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $2}'")
	-- set screenHeight to (do shell script "system_profiler SPDisplaysDataType | awk '/Resolution/{print $4}'")
	tell application process "Keyboard Maestro Engine"
		set paletteWin to first window
		set sizePal to the size of paletteWin
		set widthPal to item 1 of sizePal
		set heightPal to item 2 of sizePal
		set winList to windows
		set winNameList to name of every window
		set AppleScript's text item delimiters to linefeed
		set palWinName to item 1 of winNameList as text
		set adjXpos to widthPal / 2
		set adjYpos to heightPal / 2
		tell window palWinName
			set position to {(screenWidth / 2) - adjXpos, (screenHeight / 2) - adjYpos}
		end tell
	end tell
end tell

@Tom, I had to remove your plugin because the KM Editor.log was reporting an error:

2017-09-25 20:04:50 Invalid File for Plug In Action Manipulate a Display Text Window

Sorry, but I didn't try to diagnose or reinstall, since:

  • I have your equivalent macro
  • The only macro I was using it was a macro by @ccstone:
    Search Designated Text File and Display Results

In case anyone else needs to uninstall a plugin:

How to Uninstall a Plug In

  1. In the Finder, go to this folder (use ⇧⌘G )
    ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions
  2. Select the Folder for the Plug In, and move to Mac Trash

To be safe, I stopped both the KM Editor and the KM Engine before the uninstall.

Thanks for the note. I will update it ASAP.

I'm unable to reproduce that error. It works fine here with KM 8.0.1 on macOS 10.13.

I don't know what "Invalid File" exactly means. Maybe try to reinstall the plugin?

After reinstalling action plugins — if the action already is inserted in a macro — you should reload KM and KM engine:

tell application "Keyboard Maestro" to reload
tell application "Keyboard Maestro Engine" to reload

or simply relaunch them.

Edit:

The issue has been resolved; see posts below.


Update for KM7 (26 Sep 2017, 21:20 Z)

Manipulate a Display Text Window (KM7).zip (7.7 KB)

…and…

Update for KM8 (26 Sep 2017, 21:22 Z)


Manipulate a Display Text Window (KM8).zip (7.5 KB)

I have removed the "Focus OK button" checkbox, since this has become the default with KM8. So, download this version only if you are using KM8+.

2 Likes

It means it is complaining about the Icon or Script key data.

Either it cannot read it, or the contents of the key does not look like a file name (regex match ^[A-Za-z0-9_]+(?:[.][A-Za-z0-9_]+)?$.

Note that this error happens before it looks at the file system, so it has nothing to do with whether the file exists or not, only the value of the key in the plist.

It's this:

	<key>Name</key>
	<string>Manipulate a Display Text Window</string>
	<key>Script</key>
	<string>Action.scpt</string>
	<key>Icon</key>
	<string></string>

Its simply complaining about the Icon. It’s not a significant error, probably it should not even be reported.

Well, on my machine it isn't complaining at all.

The only log entry after running a macro with that action is:

2017-09-26 12:32:30 Execute macro “[plugin test] Manipulate a Display Text Window” from trigger Editor

The Icon should be optional, so I’ve stopped it reporting an issue if there is no Icon key, but it would still complain about the Icon in that example.

What exactly do you mean with "complain"?

Running the action I don't get any notification nor any error in the log.

It should appear in the Editor log file and the Console log when you do something that would require the icon, which would mostly only happen if you display the action in the editor unless the action was the only action in a macro, then the macro might take on the icon from the action.

Ah, sorry I was looking in the Engine log all the time. Stupid me.

The message is in the Editor log, indeed.

So, what is the correct string for the default icon?

Edit / PS:

Or just leaving it as it is (=empty) and ignoring the message in the Editor log?