Hi.
I'm writing a macro to launch an application in command line mode.
To do this, I need to send a path to the terminal.
I can't do it...
My error is probably trivial...
Here's the script I use to test the variable transmission
It gives me this result.
Obviously, the variable is not transmitted.
Thanks for your help
When you execute a shell script from other apps, like Terminal.app, the shell does not have access to the Keyboard Maestro Engine environment, and thus it does not know anything about Keyboard Maestro Variables.
at kevinb:
Thanks for your response.
But I see there:
" Using Keyboard Maestro Variables
Keyboard Maestro variables are included in the environment of the script, with the prefix KMVAR_ and spaces in the name replaced with underscores (_),
By default, all variables are included, but you can select No Variables, or specific variables as desired using the popup menu next to the script (v11.0+)."
I don't understand ...
(and what about the popup menu ? I don't see the option for the variables)
I'm feeling really stupid here, but what Action is that? It's not Execute Shell Script, and I've never seen one with the option to open a new terminal window and/or bring it to the foreground.
Thanks for clarifying, I was really confused. Do you need to see the output actually happening in Terminal? I assume so, otherwise you can just use the Execute a Shell Script Action.
For my testing phase, I need to see the output window
because I'm just a rookie ...
Your question forced me to read again the original post of Tom.
It saved me !
As you have since realised, the text I quoted was from the page for a different action, Execute a Shell Script. I deliberately gave only the URL of the source rather than the page title but the forum software "helpfully" added it back in again, and I should have emphasised that it was a different action!
You can see output with the Shell Script action if you set the results to display in a window. Alternatively, you could make a macro like this, which could pass variables:
Activate Terminal
Enter Text by Pasting: %Variable%your_KM_variable%
That's basically what Tom's plug-in does, but if you do it directly, you can just pass your variables as needed.
(I see this has been sorted between me starting and me finally hitting the post button -- left for posterity in case you do need to use Terminal for some reason, eg you need to monitor a script's or a utility's progress.)
That looks like a plug-in rather than a standard KM action -- it'll help if you tell us which plug-in you are using for this. KM variables aren't generally available in a Terminal instance so the plug-in must be doing something clever.
I wouldn't go this route myself -- I'd send the command via an "Execute an AppleScript" action. Again, you have to get the variables in somehow and the easiest way is build the command in AS first, remembering to escape any double-quotes:
Well, actually my Execute a Script in Terminal action is also just an AppleScript, no magic involved . This is the script from the action:
AppleScript content of Execute a Script in Terminal action
# Description: Part of a KM action. Passes a command or script to the Terminal.
# Author: Tom Floeren
# Version: 2.1.0
# Mod. Date: 2017-12-06
set winFront to system attribute "KMPARAM_Bring_Terminal_to_foreground"
set winNew to system attribute "KMPARAM_New_Terminal_window"
set theScript to do shell script "echo $KMPARAM_Script"
set kmVars to system attribute "KMPARAM_KM_variables"
# Get KM variables if any
if length of kmVars > 0 and kmVars does not start with "Optional." then
set inst to system attribute "KMINSTANCE"
set varDefs to ""
set c to 0
tell application id "com.stairways.keyboardmaestro.engine"
repeat with p in paragraphs of kmVars
if length of p > 0 then # Ignore empty lines
set v to getvariable p instance inst
set c to (c + 1)
set varDefs to varDefs & "km" & c & "=" & quoted form of v & ";" & space
end if
end repeat
end tell
set theScript to varDefs & theScript
end if
# Run script in Terminal
if winNew is "1" then
tell application id "com.apple.Terminal"
launch
do script theScript
if winFront is "1" then activate
end tell
else
tell application "Terminal"
launch
if winFront is "1" then activate
set windowCount to (count of the windows)
if windowCount is greater than 0 then
repeat with w from 1 to windowCount
if window 1 is busy or history of window 1 contains "[Process completed]" then
set frontmost of window 1 to false
else
do script theScript in window 1
set frontmost of window 1 to true
return
end if
end repeat
end if
tell window 1
do script theScript
set frontmost to true
end tell
end tell
end if
By the way, @Filou, the last version of the action is 2.1.0. Saying this because above you posted a link to version 2.0.0.
(Due to the forum settings it is (or was) not possible to edit a new version into the OP of a topic, as posts get locked after a while. So before you download a macro or action, it is always a good idea to slowly scroll down the topic, until the very bottom, to see if there are any updates posted later.)
I would like to run the following command line via KM:
/Applications/EasyDataTransform.app/Contents/MacOS/EasyDataTransform /Users/dpp/Documents/Admin/Bqe/iCompta/Interfaces/test.transform /Users/dpp/Downloads/MC_test.csv /Users/dpp/Documents/Admin/Bqe/iCompta/Interfaces/output.csv -cli
I've defined a Batch variable into which I copy the instruction
and added an action Execute Shell Script
I first set two vars, one for each path in your command. The shell script action is then just the command you're running, each path separately, and the command line options. Try it that way, and it should work.
(Any line breaks you see are due to window width; I didn't add any.)
Thank you for your help.
I had tried something similar but without success.
I think my mistake was that I had put the application (and its path) in a variable rather than directly in the Shell Script.
And I've just tried your suggestion, it works...
Thanks again ...