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.
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:
- 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.)
- Being built in, Preview is much easier to automate.
tell application "Preview" to close window 1
should do what you want.
- 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".
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.