Here’s a basic example of how you could pass a variable from Keyboard Maestro to a script to be executed on another machine over SSH (assuming you are using password-less access with SSH keys).
In this example I am taking some text input in Keyboard Maestro (defaults to ls -al $HOME
- a command to give a detailed directory listing of my home directory). This is utilised int he next step, not as stdin
, but by direct insertion as a variable. To make things easier to follow, I’ve actually broken it down into a function and the script is passing the variable as a parameter to that function.
The script exectutes an SSH connection to a machine on my local network (called styx
, with my user account specified for the connection. The content of strCommand
is passed as a command to the SSH session. That variable simply specifies to output some starting text, the passed in instruction (default is the home directory listing noted above), and then an ending string.
The output is displayed in a Keyboard Maestro window - it is an example after all.
When run, the default input results in a directory listing for the remote machine.
For your particular use case, you can swap in your own instructions, including the wget
for the example strCommand
content, and the input from the Keyboard Maestro variable, you want to get set as the URL you want to grab the content of.
As you noted, you could do this by copying the URL - then passing in the clipboard content to the shell script. You could do that from Keyboard Maestro, or from pbpaste
.
It might also be possible to use Automator to create a Quick Action/Service to process the URL from with a similar approach. I’m not sure if it’s easy for Automator to get the link details or not. As a result, I’d probably go with the Keyboard Maestro + URL on clipboard approach. However, you can also automate the copying to the clipboard with Keyboard Maestro.
In this macro (created for use in Safari (15.1)), I right click at the current pointer position, wait for a moment for the context menu to appear, then press “c” to select the “Copy Link” item and “return” to execute it. There’s a pause for the clipboard to update, and then the final step, just for illustration purposes, displays the URL on the clipboard.
I tied this to a hot key so when the mouse pointer is over a link I could trigger it. For your use, once happy with how it works, you would drop the final step and replace it with your script step taking in the clipboard. Using the suggested simplification from @Tjluoma, and some appropriate use of additional quotation marks, I think that you would have something of this format.
#!/bin/zsh
ssh stephen@styx.local 'wget -P "public_html/path/to/media/" "'$(pbpaste)'"'
The combined result I think should give you a keyboard shortcut triggered (or Stream Deck, etc. as you wish) Keyboard Maestro macro that would trigger the copy of the URL to the clipboard and pass that off to an SSH-key enabled SSH connection tot he remote machine, and initiate the process there.
You may also wish to consider options such as running triggering the command and disconnecting (a la nohup
), or having the process self contained with, for example, a monitored iCloud folder (Automator folder action, Keyboard Maestro Folder changed trigger, Hazel (the uber tool for such things)), and a download file that is used to specify the download and save to locations. That might give you a single consistent process regardless of trigger location or action.
Hope that helps.