Open Terminal and Run a Few Lines

I want to open Terminal, change to a directory and run a line or two in Terminal. However, I am having trouble even opening Terminal! I've got the Execute Shell Script with cd .. in there, but nothing happens.

What am I doing wrong?

The Execute Shell Script action runs a script (and optionally returns a result) but:

  1. It doesn't use or involve Terminal.app
  2. It uses a fresh vanilla instance of the shell – not the instance seen by Terminal.app
  3. Its default directory (see the result of pwd) is the top of the file system. i.e. cd .. will not immediately have a meaning. (/ has no parent directory)

you can get a richer harvest by changing to (or referring to) a fully specified path:

image

If you just want to open an instance of Terminal.app, with its working directory set to the current directory in Finder, you could try something like this:

Terminal window for working directory of Finder.kmmacros
(19.9 KB)

JS Source
(() => {
    'use strict';

    // ---------------------- MAIN -----------------------
    const main = () => {
        const fp = finderPath();
        return (
            // In a terminal window.
            Application('Terminal').doScript(
				`cd "${fp}"; clear`
			),

            // In the JavaScript interpreter context.
            // (e.g. for embedding in a notification).
            fp
        );
    };

    // --------------------- GENERIC ---------------------
    // finderPath :: () -> IO ()
    const finderPath = () =>
        decodeURI(
            Application('Finder')
            .insertionLocation()
            .url()
        ).slice(7);

    // MAIN --
    return main();
})();
2 Likes

Thanks for the replies. I didn't realise it didn't open Terminal itself. But I see there is the Activate a Specific Application, which I got from your code. That helped.

image

So I have it typing this code, but how to I enter a carriage return on there?

Use the "Insert Token" drop down like this:

KM 1 2020-12-15_16-51-11

1 Like

Ah thanks! I did look in there but didn't find Return. Didn't think of looking in the Characters category. Getting there slowly!

Edit: All working now. :grin:

1 Like

Hey Jon,

In my experience it's not a good idea to artificially type commands into the Terminal.

There are too many little things that can go wrong, and the possibilities for losing data are too great.

I recommend directly scripting the Terminal.

Terminal ⇢ CD into the Front Finder Window or Desktop v1.00.kmmacros (6.4 KB)

That said I do have text-expansion snippets in Typinator that help me out when I'm composing commands – but none of them run without my explicit say so.

-Chris

BTW – what are you doing?

Do you really need the Terminal.app?

Or can the Execute a Shell Script action do the job for you?

-Chris


Quick and Dirty Execute Shell Script Demo v1.00.kmmacros (6.4 KB)

Yes, I need Terminal. I am a programmer.

Then you'll have a very educated idea of which Terminal operations are safe and which are not for you.

I prefer to place automatically executed operations in scripts, because auto-typing/pasting can be interfered with via keyboard, mouse, or another process.

This is not likely to occur when you're at the keyboard and performing a text-substitution or other pasted/typed command, but I've seen some strange things in the better part of two decades using Keyboard Maestro and 36 years using Mac automation tools.

I prefer Typinator to Keyboard Maestro for text-substitution, because I have thousands of snippets – and Typinator being dedicated software does the job more efficiently than Keyboard Maestro.

More than a few of those are prototypes for the Terminal.

There is synergy in having both apps – particularly since Keyboard Maestro can't do text-substitutions in its own dialogs.

Making a case for Typinator

(Typinator ⇢ Ergonis Software)

-Chris

I will take a peek at that software. On my Windows laptop, I like Phrase Express as you can get a popup menu and choose the snippet. I am currently using Alfred Snippets and sometimes a (poor) Mac version of PhraseExpress.

I'm using KM since I just hit F13 and it opens Terminal, then navigates to a particular directory. A great time saver. Not sure if Typinator can do that sort of thing?

Yes, and no.

Typinator is a snippet expander utility similar to PhraseExpress and not specifically designed to run macros. (Although it can run AppleScripts and Shell Scripts.)

So your job as stated is best done via Keyboard Maestro or AppleScript or both.

I personally use FastScripts to run AppleScripts, because does this faster than Keyboard Maestro – and I like the way the scripts are organized and available for editing.

But – FastScripts is now priced at $24.95 instead of $9.95 – and that's too much for most folks.

I wish more folks besides me would fuss to Daniel Jalkut <jalkut@red-sweater.com> about this.

He doesn't get that many sales from FastScripts anyway, and I think he's now overpriced it so far that no one will buy it...

That grieves me, because I've been evangelizing the hell out of it since 2003.

FastScripts is another utility that has synergy with Keyboard Maestro.

I do run AppleScripts from Keyboard Maestro, but only when they're part of a larger macro.

In Typinator in addition to hundreds of other things I have many shell commands and prototypes for same such as:

sedr expands to:

sed -E 's!^!!'

sedrs expands to:

sed -E 's!^!!' <<< "$dataStr"

(The caret is where the cursor is left.)

cus; expands to:

curl -sL --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' ^

I also have cus aliased to the above command string in the Terminal.

I usually build shell scripts in BBEdit instead of the Terminal though.

BBEdit's shell worksheet is invaluable, and it can run shell scripts almost as easily from a text window.

Once I have my script working it gets moved to the appropriate venue – Terminal, Keyboard Maestro, FastScripts, etc.

-Chris