Setting position of new case entries added with AppleScript

I'm starting to use AppleScript to control KM itself—very powerful!

I've run into a complication when I try to add a case entry. I have a Switch/Case action with a lot of cases, and then a final "otherwise" case. I'm adding new case entries with AppleScript like this:

	tell myAction
	    make new case entry with properties {xml:"<dict><key>Actions</key><array/> ... </dict>"}
	end tell

This creates the case entry I've defined, but it adds it to the end of the Switch/Case action—after the "otherwise" case entry. As a result the new case entry is never triggered, because the "otherwise" condition always gets triggered first.

Is there a way to insert the new case entry in the penultimate position, or the top position, rather than at the end?

Hey Gabriel,

If you'll provide a working test macro and a working AppleScript I'll have a look.

I can think of a couple of possible avenues, but I need something to test with.

-Chris

Thanks Chris. Try this:

(1) Load up this macro:
AppleScript Case Entry Tester.kmmacros (2.7 KB)

(2) Run this AppleScript:

tell application id "com.stairways.keyboardmaestro.editor"
    set m to macro "AppleScript Case Entry Tester"
    tell m
        tell action "AppleScript Case Entry Tester Switch/Case Action"
            make new case entry with properties {xml:"
<dict>
    <key>Actions</key>
    <array>
        <dict>
            <key>IsDisclosed</key>
            <false/>
            <key>MacroActionType</key>
            <string>Notification</string>
            <key>Title</key>
            <string>contains h</string>
        </dict>
    </array>
    <key>ConditionType</key>
    <string>Contains</string>
    <key>TestValue</key>
    <string>h</string>
</dict>
            "}
        end tell
    end tell
end tell

When I do this, it adds a case entry at the end of the switch/case action, after the "otherwise" case entry.

You can try this, @gabrielroth.

tell application id "com.stairways.keyboardmaestro.editor"
    set m to macro "AppleScript Case Entry Tester"
    tell m
        tell action "AppleScript Case Entry Tester Switch/Case Action"
            make new case entry with properties {xml:"
<dict>
    <key>Actions</key>
    <array>
        <dict>
            <key>IsDisclosed</key>
            <false/>
            <key>MacroActionType</key>
            <string>Notification</string>
            <key>Title</key>
            <string>contains h</string>
        </dict>
    </array>
    <key>ConditionType</key>
    <string>Contains</string>
    <key>TestValue</key>
    <string>h</string>
</dict>
            "} at beginning of every case entry of it
        end tell
    end tell
end tell

Hey Gabriel,

This works.

You can say before or after your case entry #.

And you can use at beginning or at end of case entries.

-Chris

tell application id "com.stairways.keyboardmaestro.editor"
   set m to macro "AppleScript Case Entry Tester"
   tell m
      tell action "AppleScript Case Entry Tester Switch/Case Action"
         make new case entry at before last case entry with properties {xml:"
<dict>
    <key>Actions</key>
    <array>
        <dict>
            <key>IsDisclosed</key>
            <false/>
            <key>MacroActionType</key>
            <string>Notification</string>
            <key>Title</key>
            <string>contains h</string>
        </dict>
    </array>
    <key>ConditionType</key>
    <string>Contains</string>
    <key>TestValue</key>
    <string>h</string>
</dict>
            "}
      end tell
   end tell
end tell
1 Like

@peternlewis this is seriously spiffy!  :sunglasses:

-Chris

1 Like

That works! Thanks guys. AppleScript is a funny language.

2 Likes

Chris, thanks for giving us these options, but it is not obvious (at least to me) how you would actually use it from your example.

Your Example:
make new case entry at before last case entry with properties {xml:"

So, would "at beginning" be like this:
make new case entry at beginning of case entries with properties {xml:"

and "at end" be:
make new case entry at end of case entries with properties {xml:"

Would this work to position new entry "after" a certain entry?
make new case entry after second case entry with properties {xml:"

Hey JM,

Experimentation is the name of the game. Try it and see...

But – these all work:

make new case entry at before first case entry with properties {xml: …
make new case entry at after first case entry with properties {xml: …

make new case entry at before last case entry with properties {xml: …
make new case entry at after last case entry with properties {xml: …

make new case entry at before case entry 2 with properties {xml: …
make new case entry at after case entry 2 with properties {xml: …

-Chris

4 Likes

I'm glad all that work I put in to the AppleScript interface is paying off!

4 Likes

YES! Thank you very much Peter. The Ver 8+ AppleScript features have been very useful.