I’ve had Keyboard Maestro for a few years and I’m trying to make better use of it but I suppose this is a neophyte question.
I’d like to know how to trigger a macro using the contextual menu, perhaps via OS X’s Services. I tend to use the KM’s status menu to trigger macros but having access to them under the right mouse button would be more convenient.
Is this possible? It seems like it should be but I haven’t been able to discover how.
If you create a service Automation
with a Run Applescript
(or in Yosemite, Run Javascript
) action, checking Output replaces selected text
,
you can:
- capture any selected text (
item 1 of input
) and place it in a KM variable which is read and updated by one of your macros,
- call that KM macro with
do script macroName
- return the new value of the KM macro as a replacement for the selection.
![](https://forum.keyboardmaestro.com/uploads/default/original/2X/d/dce1fd22f0fb59d464d6661591df394962750969.png)
on run {input, parameters}
tell application "Keyboard Maestro Engine"
-- FIRST SET THE VALUE OF A KM VARIABLE
set value of (variable "charsToEncode") to item 1 of input
-- THEN CALL A KM MACRO WHICH USES THAT VARIABLE
do script "Encode entities"
-- AND OPTIONALLY GET AND RETURN AN UPDATED VALUE
set strText to value of (variable "charsToEncode")
return strText
end tell
end run
UPDATE
Or FWIW, using a Yosemite Javascript for Applications action in Automator (rather than Applescript):
![](https://forum.keyboardmaestro.com/uploads/default/original/2X/2/2c304f9eca04dc2582f85add601bc70ad6f4bf88.png)
function run(lstInput) {
return (function (strSeln, strMacroName, strVarName) {
var appKM = Application("Keyboard Maestro Engine"),
kmVar = appKM.variables[strVarName];
kmVar.value = strSeln; // read
appKM.doScript(strMacroName); // transform
return kmVar.value(); // return result
})(lstInput[0], "Encode entities", "charsToEncode");
}
1 Like
Wow, what a comprehensive response. Thank you.
The macro I’m working on takes selected text on a web page, grabs the URL and page title and puts it all together with quotes and links on another web forum page. This macro works perfectly when I select the text and invoke the macro from the status menu.
I’ve created a service which indeed appears in the services menu but when I select text and invoke the service “Share web content” nothing happens.
Clearly I’m missing something somewhere, which doesn’t surprise me because this is well beyond my comfort zone. I probably need to learn a little more about AppleScripts, services… and KM.
Any suggestions welcome.
Here’s my workflow:
on run {input, parameters}
tell application "Keyboard Maestro Engine"
-- FIRST SET THE VALUE OF A KM VARIABLE
set value of (variable "Page Content") to item 1 of input
-- THEN CALL A KM MACRO WHICH USES THAT VARIABLE
do script "Share web content"
-- AND OPTIONALLY GET AND RETURN AN UPDATED VALUE
set strText to value of (variable "Page Content")
return strText
end tell
end run
It resides in the Services folder although it has the extension “.workflow” not “.service”. Is that correct?
And the macro is below. I’ve tried it with an without the two steps, “Type the ⌘C Keystroke”; “Set Variable ‘Page Content’ to Text %CurrentClipboard%” without success.
**Share web content**
No triggers specified.
**Will execute the following actions:**
Comment ‘Get web page content’
Set Variable ‘Page Title’ to Text
%SafariTitle%
Set Variable ‘URL’ to Text
%SafariURL%
Type the ⌘C Keystroke
Set Variable ‘Page Content’ to Text
%CurrentClipboard%
Comment ‘Trim post title’
Copies first 64 characters from the source page title into Post Title and asks user to edit
Get Substring of Variable ‘Page Title’
Subrange from character 1 length 64.
Store result to Variable ‘Post Title’.
Action iconPrompt for User Input ‘Adjust title’
64 character maximum.
Input the following variables:
Post Title (default to ‘%Variable%Post Title%’)
Finish with the following buttons:
OK
Cancel (cancel macro)
Store button pressed in variable ‘Result Button’.
Comment ‘Set author and select destination page’
Prompt for User Input ‘Adjust source author’
Enter the author of this content
Input the following variables:
Source (default to ‘/?)
Link (default to ‘Link’)
Finish with the following buttons:
OK
Cancel (cancel macro)
Store button pressed in variable ‘Result Button’.
Comment ‘Enter info into forum post’
Set Safari Field ‘document.forms["postform"]["subject"]’ to Text
%Variable%Post Title%
Set Safari Field ‘document.forms["postform"]["message"]’ to Text
[quote="%Variable%Source%"]%Variable%Page Title%
%Variable%Page Content%[/quote]
[url=%Variable%URL%]Source[/url]
Comment ‘Delete clipboard entry’
Delete Past Clipboard 0
Would you like to post your draft macro here ?
( That would probably make it easier to get a sense of what the issue might be )
UPDATE
Would you like to post your draft macro here ?
→
"draft macro and Automator service", I meant …
I approached this again this morning with fresh eyes and it works beautifully now. Thanks for your help.
This is the Automator workflow (placed in the Services folder):
Share web content.workflow.zip (68.1 KB)
And this is the macro it activates:
Share web content.kmmacros (9.2 KB)
This macro takes selected text on a web page, grabs the URL and page title, and prepares the content for entry in a BBCode forum post with attribution and source links.
One thing I'd like to add would be the option to prompt the user for a choice of several source options instead of requiring manual entry of a string but that's a topic for another thread.
1 Like
I thought I'd need to use subroutines but I've worked out how to use Prompt for User Input and If All Conditions Met to select from three common sources (web sites I seem to quote often) and also give the option of entering a new site name.
Share web content.kmmacros (11.2 KB)
I know this is pretty basic stuff but it's exciting and new to me. ![:smile: :smile:](https://forum.keyboardmaestro.com/images/emoji/apple/smile.png?v=12)
2 Likes