Parse input var into AppleScript

Hello,
Can someone help me with the following.


Trying to parse variable today into xml part of apple script. No result. What am I doing wrong?
If I insert plain text 2024-01-01 as example , it works.
Thanx in advance for the help.

Welcome to the forum.

KM's variables are prefixed with "KMVAR_" when passed to scripts. To quote from the Wiki entry

Keyboard Maestro sets the environment variables for the script to include all your variables, using a prefix of KMVAR_ and your variable name with spaces changed in to underscores (_). For example, your Keyboard Maestro “File Name” variable will be available as the environment variable KMVAR_File_Name.

So you need to update the script to refer to the variable that way. Also, notice that there's a little down arrow next to the "set" in your screenshot - click on that and make sure "Include All Variables" is selected or the variable won't get through to the script at all.

The relevant Wiki entry is here:
https://wiki.keyboardmaestro.com/action/Execute_an_AppleScript

thank you.
I forgot to mention that I already tried it with the KMVAR_ prefix. It did'nt work.
I cannot find a way to view (debug) if the variable is correctly parsed?

The current recommended method to use Keyboard Maestro variables in AppleScript is to first convert the Keyboard Maestro variables into AppleScript variables at the head of the script and then use those AppleScript variables in the actual AppleScript. Here is an example using two Keyboard Maestro Global Variables in a simple AppleScript that displays them.

EXAMPLE KM Global Variable to AppleScript Variable.kmmacros (3.4 KB)

Click to Show Image of Macro

The above method will work for you as your Keyboard Maestro variable is called "today" and is therefore a Global variable.

To use Keyboard Maestro Local variables in AppleScripts the scripting at the head would be slightly different:

EXAMPLE KM Local Variable to AppleScript Variable.kmmacros (3.5 KB)

Click to Show Image of Macro

Thank you very much. I will try this.
Regards.

Thanks for the explanation . I tried this, but no success. I cannot seem to replace this xml element in the apple script with the input variable.
Regards

Did you insert your KM variable into the AS string correctly? You can't just stick it into the string, you have to concat it together with the preceding and following sections of the string.

example

1 Like

and, as it happens you can process the date token directly from inside the AppleScript, bypassing the need to create and import a variable value:

tell application "Keyboard Maestro Engine"
	set dateString to process tokens "%ICUDateTimeMinus%1%Days%yyy-MM-dd%"
end tell

Incidentally, whereas in an Execute Applescript action, the only way to interpolate a computed value into some part of a an existing template string is (as @roosterboy explains) by concatenating "precedingString" & computedString & "followingString",
you can do something more direct in an Execute JavaScript for Automation string – interpolating directly with ${...} between a template between backticks:

`preceding things ${computed value} following things`

So, for example:

Token value interpolated into template.kmmacros (3.1 KB)


Expand disclosure triangle to view JS source
(() => {
    "use strict";

    const
        kme = Application("Keyboard Maestro Engine"),

        dateString = kme.processTokens(
            "%ICUDateTimeMinus%1%Days%yyy-MM-dd%"
        );

    const myQueryString = `<MedoraQuery>
        <labellndex>0</labellndex>
        <numParams>2</numParams>
        <searchSpotlight>0</searchSpotlight>
        <append>0</append>
        <isAND>l</isAND>
        <source>all</source>
        <row>
        <kind>ModificationDate</kind>
        <operator>is</operator>
        <valueClass>date</valueClass>
        <value>33691800</value>
        <value2>${dateString} 02:00:00</value2>
        </row>
        <row>
        < kind > Kind </kind>
        <operator>is</operator>
        <valueClass>kindlist</valueClass>
        <value>photo</value>
        </row>
        </MedoraQuery>`;

    Application("NeoFinder").runQuery(myQueryString);

    return myQueryString;
})();

Yess!!! That works. Thank you so much!
Regards
Nick Franken
PS I used the method Roosterboy suggested. :wink:

and FWIW you can do something very similar to your first draft, skipping concatenation, if you wrap a template string (containing one or more Keyboard Maestro tokens) in the Keyboard Maestro Engine process tokens method:

Process KM Tokens in an XML template.kmmacros (2.8 KB)


Expand disclosure triangle to view AppleScript
tell application "Keyboard Maestro Engine"
	
	set myQueryString to process tokens "<MedoraQuery>
	<labellndex>0</labellndex>
	<numParams>2</numParams>
	<searchSpotlight>0</searchSpotlight>
	<append>0</append>
	<isAND>1</isAND>
	<source>all</source>
	<row>
	<kind>ModificationDate</kind>
	<operator>is</operator>
	<valueClass>date</valueClass>
	<value>33691800</value>
	<value2>%ICUDateTimeMinus%1%Days%yyy-MM-dd% 02:00:00</value2>
	</row>
	<row>
	< kind >Kind</kind>
	<operator>is</operator>
	<valueClass>kindlist</valueClass>
	<value>photo</value>
	</row>
	</MedoraQuery>"
	
end tell


tell application "NeoFinder"
	run query myQueryString
end tell
1 Like