0

Logically this is possible, but I'm not sure it is technically possible on a Mac. And I have no idea where to start, but here it is: I have a workflow right now where I (1) close the only tab in VS Code I have open, (2) close the only tab in my PDF reader I have open, (3) (most importantly) repeat the last command in the terminal (which opens a new tab/file in each of the two mentioned programs).

Here is the thing: I don't use my Spotlight key. Is there any way I could map the spotlight key to do step 1, 2 and 3 in one single click?

(If step 1 and 2 are not possible, automating step 3 would still be the most valuable part. If so, the important thing is I don't want to change window to the terminal - since that already constitutes 2 of the total 4 keystrokes I currently do to repeat the last terminal command.)

2
  • “Repeat the last command in Terminal”. Does the up arrow key to get the last command from history not work?
    – Allan
    Commented Apr 2, 2023 at 8:31
  • Yes the up arrow key works, but I wanted to do it without having to move to the terminal window, because I have to repeat the process so many times.
    – Fom
    Commented Apr 2, 2023 at 12:17

1 Answer 1

1

First the easy part. You can change (or remove) the spotlight keyboard shortcut in System Settings -> Keyboard -> Keyboard Shortcuts -> Spotlight then unclick one or both of the options. Once you've freed up the shortcut you can reassign it to anything you'd like.

Screenshot of Spotlight keyboard shortcut menu

As for the automation, have you built any of it yet? Shortcuts alone can do a lot, but Visual Studio Code doesn't offer itself to Shortcuts so you'll need a bit of AppleScript too. And since we're scripting, it's easiest to do it all there. If you're not familiar with AppleScript, it may seem daunting at first, but the commands are surprisingly easy to read. Taking your steps one by one:

  1. Just as it isn't Shortcuts-friendly VS Code isn't very amenable to AppleScript either, so options are limited. I didn't see a way to close the open tab, but you could tell it to exit with "tell application "Visual Studio Code" to quit". (A few more minutes of STFWing may turn up a better answer.)
  2. Being built in, Preview is much easier to automate. tell application "Preview" to close window 1 should do what you want.
  3. In the Terminal "!!" will rerun the last command.

So, putting all of those together:

tell application "Preview" to close window 1
tell application "Visual Studio Code" to quit
tell application "Terminal" to do script "!!"

That will run your last command in a new Terminal window, but you can also have it run in an existing Terminal window with a few more lines:

 tell application "Preview" to close window 1
 tell application "Visual Studio Code" to quit
 tell application "System Events"
      tell application process "Terminal"
           set frontmost to true
           keystroke "!!"
           keystroke return
      end tell
 end tell

Shortcuts can run that directly and tie it to a hotkey. Open the shortcuts app, then in the right-hand search box type "AppleScript" and chose "Run AppleScript".

shortcuts editor, selecting run AppleScript

Next replace "(* Your script goes here *)" with the AppleScript code above, and give it a name. To tie it to a keyboard shortcut, click the circled "i" in the upper right, click "Add Keyboard Shortcut" and give it the keystrokes. Once that's done, your shortcut is ready to go.

If that doesn't quite work, or I misunderstood what you're looking for, let me know and I'll be happy to post an update.

shortcuts editor, ready to deploy

1
  • Thank you very much Jason! You are a hero! I have not used AppleScript before, but I will try implementing this now. Yes, the only part I have prepared is a shell script that reads the next files to open in the PDF reader and in VS Code, and then opens them, and increments the filenames to read the following time. So with your AppleScript help I think it all will work perfectly. :)
    – Fom
    Commented Apr 2, 2023 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .