Including Value in Renamed Action Title

Many actions display the value that is being set within the default Action name, and most or all actions can also be renamed.

But when you rename the action, you lose the display of the value.

For example, I can set a pause of 0.5 seconds and the default Action Name will be "Pause for 0.5 Seconds". If I use the Rename option, I can add text to the end of that so that it says, "Pause for 0.5 Seconds to allow app message window to open". But now the "0.5" is just text, it won't change if I change the value of the pause.

Is there a token that I can use in the Action Title that will be updated when I change the value of the action setting so that my new title will be updated when I change the value?

2 Likes

That would be a nice-to-have feature.

Nyet.

I'd be a bit surprised if Peter would want to mess with that, but let's ask him.

@peternlewis?

It is possible to change an action's XML with AppleScript, so you can customize things to your liking to a degree.

No, there is no token you can use in the rename to include values from the action. It's not really practical, since dynamic action names have a large degree of variability, including special cases for plurals, multiple arguments, different names, different components.

2 Likes

How about a token that would simply produce whatever the original dynamic action name would be? If that was all that was provided as the renamed action, it would be exactly the same as the default, but it would also allow us to add text before and/or after to clarify the actions context, state, or purpose?

E.g., In the Action Title, I insert the token "%==%" and it gets replaced by the defualt, "Pause for 0.5 Seconds". So if I change the title to "%==% to allow app message window to open", what I will see in the action list is, "Pause for 0.5 Seconds to allow app message window to open". And if I change the length of the pause to 1.5 seconds, it will show up as "Pause for 1.5 Seconds to allow app message window to open".

Whatever is used to generate the default title, ""Pause for 0.5 Seconds", does not need to change, it's monolithic. And it allows us to have our cake and eat it too.

It's possible, but the use case is so limited that I can't see it being worth the effort.

Meanwhile I just want the timeout integer to appear in the action by default. Cus otherwise I can never remember what I set the timeout to. :sweat_smile:

1 Like

Granted. It's a bit of "polish". I can do much the same with an adjacent Comment. Cases like Chris' should definitely be higher priority.

You mean in the action name or somewhere else?

I think it unlikely that Peter will implement such a thing, but as long as you're willing to add a step to your process you can do it yourself.

Of course it doesn't automagically update itself if you change the timeout...

I have a general error-handler in this script, but I'm not specifically trapping for TimeOutPeriod exists, so if you want a more descriptive error name you'll have to adjust the code a bit.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/11/14 03:43
# dMod: 2022/11/14 03:43 
# Appl: Keyboard Maestro Editor
# Task: Add Timeout of Selected Action to its Name
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Keyboard_Maestro, , @Add, @Timeout, @Action, @Name
--------------------------------------------------------
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
--------------------------------------------------------

try
   
   -----------------------------------------------------
   
   tell application "Keyboard Maestro"
      set theAction to (get the selection)'s item 1
      if class of theAction is action then
         set actionXML to theAction's xml
         set actionName to theAction's name
      else
         error "The Selected Item Is NOT An Action!"
      end if
   end tell
   
   -----------------------------------------------------
   
   set actionXMLRecord to its convertPlistToAS:actionXML isPath:false
   set actionTimeoutPeriod to (TimeOutPeriod of actionXMLRecord) as integer
   
   tell application "Keyboard Maestro"
      set newActionName to theAction's name & " ⇢ Timout: " & actionTimeoutPeriod
      set theAction's name to newActionName
   end tell
   
on error errMsg number errNum
   set errMsg to errMsg & linefeed & linefeed & "Num: " & errNum
   if errNum ≠ -128 then
      try
         tell application (path to frontmost application as text) ¬
            to set ddButton to button returned of ¬
            (display dialog errMsg with title ¬
               "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
               default button "OK" giving up after 30)
         if ddButton = "Copy Error Message" then set the clipboard to errMsg
      end try
   end if
end try

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
-- pass either a POSIX path to the .plist file, or a property list string; isPath is a boolean value to tell which
on convertPlistToAS:plistStringOrPath isPath:isPath
   if isPath then -- read file as data
      set theData to current application's NSData's dataWithContentsOfFile:plistStringOrPath
   else -- it's a string, convert to data
      set aString to current application's NSString's stringWithString:plistStringOrPath
      set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
   end if
   -- convert to Cocoa object
   set {theThing, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 |format|:(missing value) |error|:(reference)
   if theThing is missing value then error (theError's localizedDescription() as text) number -10000
   -- we don't know the class of theThing for coercion, so...
   set listOfThing to current application's NSArray's arrayWithObject:theThing
   return item 1 of (listOfThing as list)
end convertPlistToAS:isPath:
--------------------------------------------------------

Edit 2022/11/16 06:18 CST

Be sure to read down the thread – @Nige_S has reminded me that 'timeout period' is directly exposed to AppleScript.

The meat of the script above does expose some properties of some actions that are not available to AppleScript, so this method of converting an action's XML to an AppleScript record should not be discounted.


3 Likes

Hi Chris, yes I meant in the action name. Thanks for sharing that script; when I have a moment this evening I’ll check it out.

Why the XML? Does that give us human-readable numbers or similar, without effort on our part?

Hey @Nige_S,

Run the script up to actionXMLRecord and see. :sunglasses:

-Chris

1 Like

...fluttered their eyelashes coquettishly from behind their code and whispered...

You tease, you...

2 Likes

So no, still in seconds -- I was wondering if going via the XML and/or ASObjC was giving you "5 minutes", rather than "300", for free.

Given that the KM Editor has to running to get a selection, what are the advantages of parsing the XML of the action rather than a straight query?

tell application "Keyboard Maestro"
	set theAction to item 1 of (get the selection)
	if class of theAction is action then
		set theTimeout to timeout period of theAction
		set theName to name of theAction
	else
		error "The Selected Item Is NOT An Action!"
	end if
end tell

return theTimeout

Or is this a more generic handler that lets you access properties not normally revealed directly?

1 Like

Ha! You've got me on this one.

I forgot to look at action properties, before I wrote my script and did things the hard way...

At least I learned a couple of things in the process.

:sunglasses:

2 Likes

I believe that is correct.

For example, using this in Chris's script

set actionTimeoutPeriod to (|Time| of actionXMLRecord)

instead of

set actionTimeoutPeriod to (TimeOutPeriod of actionXMLRecord) as integer

gets the value (as a string) of the delay period set in a Pause action.

Horses for courses I guess.

2 Likes

In studying your script using Script Debugger I learnt that TimeOutPeriod exists only if you’ve actually accessed the gear menu on the action and changed the value. Now I understand your comment about error trapping :wink: Thanks for that extra bit of knowledge!

2 Likes

Hey Guys,

Just in case it's not clear...

My AppleScript converts the selected action's XML into an AppleScript record, and this method exposes some properties of various actions that are not available directly to AppleScript.

AppleScript records are rather stupid and lack many features of other languages such as the ability to extract a their key names.

You can't directly query a record for the existence of a key name, so you have to trap for n error when the key name might not exist.

I'm thinking there's a way to get keys of an AS record with AppleScriptObjC...

Yep.

use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions

set testRecord to {a:"aaa", b:"bbb", c:"ccc"}

set objCDictionary to current application's NSDictionary's dictionaryWithDictionary:testRecord
set allKeys to objCDictionary's allKeys() as list

There may be a way to directly ask a record if a key exists as well, but I'm going to stop here for today.

-Chris

2 Likes

A way to make it so that it does automatically update the name of the Action if the timeout settings are changed is to make it so the first thing it does is open the KM timeout dialog (instead of using the gear menu).

I've played around and think I've got a good Macro working. It's actually nicer to use than the gear menu (as it has a hotkey) so it can just become my default way to set the timeout settings of an Action and to change the name of an existing Action to include its timeout settings.

I used parts of the AppleScripts by @ccstone and @Nige_S to get the info from the selected Action and then Keyboard Maestro Actions to change some of that data into my preferred formatting before using a second AppleScript to append this info to the original name of the Action.

It leaves the part of the Action name before the timeout info alone, no matter how many times the timeout settings are changed.
It also shows whether the timeout will abort the Macro and/or Notify.
It presents the timeout in seconds if it is up to 60 seconds and after that in hh:mm:ss format (using a method by @Tom)

EDIT - Macro is now updated with AppleScript from @Nige_S which simplifies it and means it can work on multiple-selected Actions.

Screen Recording 2022-11-16 at 13.20.24-Animated GIF

Click to Show Image of Macro

Append Name of Action with Timeout v2.00.kmmacros (8.6 KB)

3 Likes

That’s a great one @Zabobon - now also get it to do what @August asked for in post #1 :wink: