I have an AppleScript that creates a KM macro and I would like to expand it to get the URL trigger of the created macro into the clipboard.
Anyone knows how to achieve that?
I have an AppleScript that creates a KM macro and I would like to expand it to get the URL trigger of the created macro into the clipboard.
Anyone knows how to achieve that?
The URL trigger is just the "id" of the macro prefixed with kmtrigger://macro=
and with an optional parameter postfix. So:
tell application "Keyboard Maestro"
tell macro group "Tests"
set theMacro to make new macro with properties {name:"Automated Test"}
end tell
set theID to id of theMacro
end tell
set the clipboard to ("kmtrigger://macro=" & theID)
Thank you for the swift help.
Or if you want to use native KM actions:
Very nice, Neil. thank you.
Do remember that when you create a macro with AppleScript it won't be the current macro in KM (unless you then switch to it). So it'll depend on your workflow as to whether you grab the trigger as part of the AppleScripted creation, or you later open the macro in KM and use Neil's native actions.
Thx for the hint.
I am using hook and markdown to switch to the macro created with AppleScript. Do you happen to know another way?
Just add select theMacro
, after you've created it, to the script:
tell application "Keyboard Maestro"
tell macro group "Tests"
set theMacro to make new macro with properties {name:"Automated Test"}
end tell
set theID to id of theMacro
select theMacro
end tell
set the clipboard to ("kmtrigger://macro=" & theID)
Hey Nige,
That looks about right.
One convention I tend to use is new instead of the for new objects (newMacro).
Here's a alternative way to write your script just for kicks:
tell application "Keyboard Maestro"
tell macro group "Test Group.ccstone" to ¬
set newMacro to make new macro with properties {name:"Automated Test"}
tell newMacro
set theID to its id
select it
end tell
end tell
set the clipboard to ("kmtrigger://macro=" & theID)
There are apps where select it would not work from within the tell newMacro block, and there's no simple way to learn this in advance of testing.
-Chris
coolest forum evva. you guys are soo helpful. thx again.
greets from greece
And for more kicks...
tell application "Keyboard Maestro" to tell macro group "Tests" to select (make new macro with properties {name:"Automated Test"})
tell application "Keyboard Maestro" to set the clipboard to ("kmtrigger://macro=" & id of item 1 of (get the selection))
While there's probably a way to both select the new macro and put its id on the clipboard in only one line of AS, I'm not seeing it. Anyone care to put me out of my misery?
Hi Guys,
Since I see so much concentrated KM expertise here, I would like to humbly ask someone with some time at their hands to please have a look at this script and maybe give me a hint why the last part with the AppleScript macro action is not working.
# Run this script when you have selected a message in mail app
# Get info of the selected message in mail app
tell application "Mail"
activate
set selectedMessage to selection
set thisMessage to item 1 of selectedMessage
set thisSender to sender of thisMessage
set thisEmail to extract address from thisSender
set thisName to extract name from thisSender
set thisEmailLabel to "Work"
set theMacroGroupName to "Trial Automations Macro Group"
set theTestMailbox to "TestVariable"
end tell
# Close Mail Application if open
tell application "Mail"
quit
end tell
# Create new contact in contact app
tell application "Contacts"
set thePerson to make new person with properties {first name:thisName, last name:""}
tell thePerson
make new email at end of emails with properties {label:thisEmailLabel, value:thisEmail}
end tell
save
end tell
delay 2
# Create new mailbox with person full name
tell application "Mail"
activate
end tell
delay 0.6
tell application "System Events" to click UI element "Mail" of list 1 of application process "Dock"
tell application "System Events" to set frontmost of process "Mail" to true
delay 1
tell application "Mail"
activate
tell account "🍎Apple iCloud"
set fullName to thisName
make new mailbox with properties {name:"Contacts🙋🏼♀️/subFolder"}
set the name of mailbox named "Contacts🙋🏼♀️/subFolder" to fullName
end tell
end tell
# Create new rule with person full name
tell application "Mail"
set newRule to make new rule at end of rules with properties {name:thisName, enabled:true, should move message:true}
set contactString to "Contacts🙋🏼♀️/"
set mailboxPath to contactString & thisName
tell newRule
make new rule condition at end of rule conditions with properties {rule type:from header, expression:thisEmail, qualifier:does contain value}
set move message to (mailbox mailboxPath of account "🍎Apple iCloud" of application "Mail")
end tell
end tell
# Reveal the created contact
tell application "Contacts"
activate
end tell
delay 0.6
tell application "System Events" to click UI element "Contacts" of list 1 of application process "Dock"
tell application "System Events" to set frontmost of process "Contacts" to true
delay 1
set selection to thePerson
delay 2
# Reveal the created mailbox
tell application "Mail"
activate
end tell
delay 0.6
tell application "System Events" to click UI element "Mail" of list 1 of application process "Dock"
tell application "System Events" to set frontmost of process "Mail" to true
delay 0.5
tell application "Mail"
activate
set theMailbox to mailbox "Contacts🙋🏼♀️/theName" of account "🍎Apple iCloud"
set selected mailboxes of front message viewer to theMailbox
end tell
delay 2
# Reveal the mail preferences
tell application "System Events"
keystroke "," using command down
end tell
set theMacroGroupName to "Trial Automations Macro Group"
set theMailbox to "TestVariable"
# Creates Macro that opens the new Mailbox when triggered
tell application id "com.stairways.keyboardmaestro.editor"
if macro group theMacroGroupName exists then
set theMacroGroup to macro group theMacroGroupName
else
# Will create a New Macro Group
set theMacroGroup to make new macro group with properties {name:theMacroGroupName, available application xml:"<dict>
<key>Targeting</key>
<string>Included</string>
<key>TargetingApps</key>
<array>
<dict>
<key>BundleIdentifier</key>
<string>com.apple.mail</string>
<key>Name</key>
<string>Mail</string>
<key>NewFile</key>
<string>/System/Applications/Mail.app</string>
</dict>
</array>
</dict>"}
end if
# Creates new Macro with Ctrl N Trigger
set m to make new macro with properties {name:"AthmanMacro Trial"}
tell m
make new trigger with properties {xml:"<dict>
<key>FireType</key>
<string>Pressed</string>
<key>KeyCode</key>
<integer>8</integer>
<key>MacroTriggerType</key>
<string>HotKey</string>
<key>Modifiers</key>
<integer>4096</integer>
</dict>"}
# Creates Applescript Macro Action to open the Mailbox created further up (not working!!!!)
make new action with properties {xml:"<dict>
<key>DisplayKind</key>
<string>None</string>
<key>HonourFailureSettings</key>
<true/>
<key>IncludeStdErr</key>
<false/>
<key>MacroActionType</key>
<string>ExecuteAppleScript</string>
<key>Path</key>
<string></string>
<key>Text</key>
<string>tell application \"Mail\"
activate
set theTestMailbox to mailboxPath of account \"🍎Apple iCloud\"
set selected mailboxes of front message viewer to theMailbox
end tell</string>
<key>TimeOutAbortsMacro</key>
<true/>
<key>TrimResults</key>
<true/>
<key>TrimResultsNew</key>
<true/>
<key>UseText</key>
<true/>
</dict>"}
end tell
end tell
It's a bit difficult to tell with no formatting, so try editing your post so that the AppleScript is in a code block -- put three back-ticks on the line before your code starts and three more on the line after it finishes:
```
Your code here
```
That said...
These lines inside your XML string probably need to have their quotes escaped:
tell application \"Mail\"
activate
set theTestMailbox to mailboxPath of account \":apple:Apple iCloud\"
#Run this script when you have selected a message in mail app
#Get info of the selected message in mail app
tell application "Mail"
activate
set selectedMessage to selection
set thisMessage to item 1 of selectedMessage
set thisSender to sender of thisMessage
set thisEmail to extract address from thisSender
set thisName to extract name from thisSender
set thisEmailLabel to "Work"
set theMacroGroupName to "Trial Automations Macro Group"
set theTestMailbox to "TestVariable"
end tell
#Close Mail Application if open
tell application "Mail"
quit
end tell
# Create new contact in contact app
tell application "Contacts"
set thePerson to make new person with properties {first name:thisName, last name:""}
tell thePerson
make new email at end of emails with properties {label:thisEmailLabel, value:thisEmail}
end tell
save
end tell
delay 2
#Create new mailbox with person full name
tell application "Mail"
activate
end tell
delay 0.6
tell application "System Events" to click UI element "Mail" of list 1 of application process "Dock"
tell application "System Events" to set frontmost of process "Mail" to true
delay 1
tell application "Mail"
activate
tell account "🍎Apple iCloud"
set fullName to thisName
make new mailbox with properties {name:"Contacts🙋🏼♀️/subFolder"}
set the name of mailbox named "Contacts🙋🏼♀️/subFolder" to fullName
end tell
end tell
#Create new rule with person full name
tell application "Mail"
set newRule to make new rule at end of rules with properties {name:thisName, enabled:true, should move message:true}
set contactString to "Contacts🙋🏼♀️/"
set mailboxPath to contactString & thisName
tell newRule
make new rule condition at end of rule conditions with properties {rule type:from header, expression:thisEmail, qualifier:does contain value}
set move message to (mailbox mailboxPath of account "🍎Apple iCloud" of application "Mail")
end tell
end tell
#Reveal the created contact
tell application "Contacts"
activate
end tell
delay 0.6
tell application "System Events" to click UI element "Contacts" of list 1 of application process "Dock"
tell application "System Events" to set frontmost of process "Contacts" to true
delay 1
set selection to thePerson
delay 2
#Reveal the created mailbox
tell application "Mail"
activate
end tell
delay 0.6
tell application "System Events" to click UI element "Mail" of list 1 of application process "Dock"
tell application "System Events" to set frontmost of process "Mail" to true
delay 0.5
tell application "Mail"
activate
set theMailbox to mailbox "Contacts🙋🏼♀️/theName" of account "🍎Apple iCloud"
set selected mailboxes of front message viewer to theMailbox
end tell
delay 2
#reveal the mail preferences
tell application "System Events"
keystroke "," using command down
end tell
set theMacroGroupName to "Trial Automations Macro Group"
set theMailbox to "TestVariable"
--Creates Macro that opens the new Mailbox when triggered
tell application id "com.stairways.keyboardmaestro.editor"
if macro group theMacroGroupName exists then
set theMacroGroup to macro group theMacroGroupName
else
-- Will create a New Macro Group
set theMacroGroup to make new macro group with properties {name:theMacroGroupName, available application xml:"<dict>
<key>Targeting</key>
<string>Included</string>
<key>TargetingApps</key>
<array>
<dict>
<key>BundleIdentifier</key>
<string>com.apple.mail</string>
<key>Name</key>
<string>Mail</string>
<key>NewFile</key>
<string>/System/Applications/Mail.app</string>
</dict>
</array>
</dict>"}
end if
--Creates new Macro with Ctrl N Trigger
set m to make new macro with properties {name:"AthmanMacro Trial"}
tell m
make new trigger with properties {xml:"<dict>
<key>FireType</key>
<string>Pressed</string>
<key>KeyCode</key>
<integer>8</integer>
<key>MacroTriggerType</key>
<string>HotKey</string>
<key>Modifiers</key>
<integer>4096</integer>
</dict>"}
--Creates Applescript Macro Action to open the Mailbox created further up (not working!!!!)
make new action with properties {xml:"<dict>
<key>DisplayKind</key>
<string>None</string>
<key>HonourFailureSettings</key>
<true/>
<key>IncludeStdErr</key>
<false/>
<key>MacroActionType</key>
<string>ExecuteAppleScript</string>
<key>Path</key>
<string></string>
<key>Text</key>
<string>tell application \"Mail\"
activate
set theTestMailbox to mailboxPath of account \"🍎Apple iCloud\"
set selected mailboxes of front message viewer to theMailbox
end tell</string>
<key>TimeOutAbortsMacro</key>
<true/>
<key>TrimResults</key>
<true/>
<key>TrimResultsNew</key>
<true/>
<key>UseText</key>
<true/>
</dict>"}
end tell
end tell
quotes escaped. i don't even know what that means.
did i mention that i am a script beginner in general, and applescript beginner in particular?
In AppleScript you wrap literal text -- strings -- in quotes: "I am a text string"
. So you can't put quotes in the string because it will confuse things. You have to "escape" them so AppleScript knows to treat them as text rather than as the end of the string: "I am a string with \"quotes\" in it"
.
But your properly-escaped quotes may have got mashed in your first post -- your latest looks much better.
So -- you say the last part doesn't work. Doesn't work in what way? What is it you'd expect to happen that doesn't, or what does happen that you don't expect? Any error messages?
the applescript in question should be the macro action that should tell the mail app to open the mailbox that has been created a little further up in the script.
So to be clear -- the AppleScript does everything correctly, including creating the KM macro, but when you run the KM macro it doesn't select the right folder in Mail?
If so... Line 90 is
set theMailbox to mailbox "Contacts🙋🏼♀️/theName" of account "🍎Apple iCloud"
...setting the theMailbox
variable to the Mail subfolder that was created earlier. But line 103
set theMailbox to "TestVariable"
...set the theMailbox
variable back to a string. Line 161 then tries to use the theMailbox
variable to select a mail folder, but that won't be the one you created earlier because you've now set it to "TestVariable".
Try commenting out line 103 by putting two dashes at the start:
--set theMailbox to "TestVariable"
...and see if that helps.
Remember, nobody here will have Mail, Contacts, etc set up the way you do so your issue is really difficult to replicate. You've got other strange stuff going on -- quitting Mail and relaunching it for seemingly no reason; activating apps and clicking on their Dock icon and making them frontmost; odd emojis (I think that's what they are) in strings -- which will also make people hesitant to run the script. So you'll need to explicitly explain the problem you are having, to help people help you.
Does anyone know if there's a way to get all the url triggers preferably with all the corresponding macro titles? I want to import all of them into Anybox's Any Dock feature.
These two actions will get all your macro names and all their UIDs. It's up to you to turn them into triggers (as per post #2 above).
Keyboard Maestro Actions.kmactions (1.7 KB)