Become a MacRumors Supporter for $25/year with no ads, private forums, and more!

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
Is there a command in Terminal where I can simulate the ENTER keypress after a command?
I'd like to automate, using Keyboard Maestro, the launching of Terminal then the entering of text:
defaults write com.apple.systempreferences AttentionPrefBundleIDs 0 && killall Dock
followed by ENTER but I can't find a way to enter ENTER.
 

Mikael H

macrumors 6502a
Sep 3, 2014
793
441
Is there a command in Terminal where I can simulate the ENTER keypress after a command?
I'd like to automate, using Keyboard Maestro, the launching of Terminal then the entering of text:
defaults write com.apple.systempreferences AttentionPrefBundleIDs 0 && killall Dock
followed by ENTER but I can't find a way to enter ENTER.
Can't you just run that as a regular script? Or does the shell session need to keep running after executing that command for some reason?

Create a file "myscript" with the following contents:
Code:
#!/bin/sh
defaults write com.apple.systempreferences AttentionPrefBundleIDs 0 && killall Dock
From a terminal run the following command to make the script executable:
Code:
chmod +x myscript
To make the script executable only by you, instead run:
Code:
chmod u+x myscript
 
  • Like
Reactions: revmacian

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
Hi Mikael,
Thanks for replying! I'm a complete newb at the Terminal so please excuse my ignorance.
I used an app called SubEthaEdit to create a file "myscript" with the text:

#!/bin/sh
defaults write com.apple.systempreferences AttentionPrefBundleIDs 0 && killall Dock

Then I saved it to the desktop in file format myscript.sh with file name myscript.
When I run chmod +x myscript in Terminal it returns the following:

chmod: myscript: No such file or directory

What did I miss?
 
Last edited:

revmacian

macrumors 68000
Oct 20, 2018
1,745
1,461
USA
Hi Mikael,
Thanks for replying! I'm a complete newb at the Terminal so please excuse my ignorance.
I used an app called SubEthaEdit to create a file "myscript" with the text:

#!/bin/sh
defaults write com.apple.systempreferences AttentionPrefBundleIDs 0 && killall Dock

then saved it to the desktop in file format myscript.sh with file name myscript.
When entered into Terminal it returns chmod: myscript: No such file or directory when I run chmod +x myscript
What did I miss?
You may have missed the location. I think it may be a good idea for you to find a recent command line tutorial for Mac.

In the terminal you can see where you are in the system with the pwd command.
Then, you can change directories using the cd command. I don’t have a Mac in front of me, but you need to change to the Desktop directory with: cd ~/Desktop
Once you’re in the Desktop folder (find out with pwd), you can list files to see if your script is there: ls
Once you’ve found your script then you can continue with the chmod commands that Mikael H mentioned.

There is a single command to do all of this, but I thought it would be best to teach you a few commands while getting the job done. Just remember, path (location) is important.

Wowsers, this makes me want to build another Linux box.
 
Last edited:
  • Like
Reactions: Mikael H

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
Hi revmacian,
As you surmised I was in the wrong directory. Entering cd ~/Desktop brought me to the right directory. I double checked using pwd and now show the directory: iMac:Desktop colegno$
Entering the list command ls shows the file myscript.sh

Using either of the chmod commands that Mikael suggested unfortunately generates the same response as before:

-iMac:Desktop colegno$ chmod u+x myscript
chmod: myscript: No such file or directory

I know, if the command line was easy everybody would be doing it!:D:D
Dang emoticons!
 
Last edited:
  • Like
Reactions: revmacian

revmacian

macrumors 68000
Oct 20, 2018
1,745
1,461
USA
Hi revmacian,
As you surmised I was in the wrong directory. Entering cd ~/Desktop brought me to the right directory. I double checked using pwd and now show the directory: iMac:Desktop colegno$
Entering the list command ls shows the file myscript.sh

Using either of the chmod commands that Mikael suggested unfortunately generates the same response as before:

-iMac:Desktop colegno$ chmod u+x myscript
chmod: myscript: No such file or directory

I know, if the command line was easy everybody would be doing it :)
Is the file “myscript” or “myscript.sh”? The computer is telling you that “myscript” doesn’t exist, and that is true. The file “myscript.sh”, however, does exist (notice the file extension) Computers, regardless of their power, are still kinda dumb, lol. The file name and extension are part of the path, so you have to enter the file name and extension. You need to work with files exactly as they appear in the ls output.

Try: chmod u+x myscript.sh
 

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
The file name on the desktop is myscript but ls shows it as myscript.sh so it looks like that's how the computer wants to see it in terminal. When I run chmod u+x myscript.sh I'm no longer getting the 'no file or directory' message so that's progress! What's missing now is the script which isn't executing or showing up in Terminal. Here's a screenshot of the file myscript from the desktop:
 

Attachments

  • Screen Shot 2019-11-01 at 11.53.06 AM.png
    Screen Shot 2019-11-01 at 11.53.06 AM.png
    182.2 KB · Views: 179
Last edited:

revmacian

macrumors 68000
Oct 20, 2018
1,745
1,461
USA
The file name on the desktop is myscript but ls shows it as myscript.sh so it looks like that's how the computer wants to see it in terminal. When I run chmod u+x myscript.sh I'm no longer getting the 'no file or directory' message so that's progress! What's missing now is the script which isn't executing or showing up in Terminal. Here's a screenshot of the file myscript from the desktop:
I don’t know how to manage the defaults command, so I am unable to help you there. I’m guessing that macOS desktop doesn’t show file extensions by default, but that is ok. As long as you work with what the Terminal is expecting you should be ok. When it comes to the terminal, “no news means good news”, so the Terminal won’t show output unless there is something to report. The ls command shows output because there is something to report. The chmod command won’t show anything unless there is an error.

Your script won’t execute by itself unless you set up a cron job or other method to automate the process.
You can manually execute your script with this command - which should work regardless of your location:
cd ~/Desktop && ./myscript.sh

I don’t have a Mac in front of me, but I’m curious. What happens if you double-click that script with your mouse cursor?
 

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
Your cd ~/Desktop && ./myscript.sh worked, as for double clicking on the myscript file, located on the desktop, all that does is open the file in SubEthaEdit. Nothing happens in Terminal. Looks like I need to get familiar with setting up a cron job in order to get the command to self execute. Thanks revmacian, I appreciate your help.
 
  • Like
Reactions: revmacian

revmacian

macrumors 68000
Oct 20, 2018
1,745
1,461
USA
Your cd ~/Desktop && ./myscript.sh worked, as for double clicking on the myscript file, located on the desktop, all that does is open the file in SubEthaEdit. Nothing happens in Terminal. Looks like I need to get familiar with setting up a cron job in order to get the command to self execute. Thanks revmacian, I appreciate your help.
You’re very welcome. Using cron can be a bit daunting, there is a lot to learn about the syntax and environments. Search for a Crontab tutorial. Also, I’m not sure how the added security in macOS will affect your attempts to set up a cron job in the Terminal app. You may have to allow full disk access for Terminal.app.
 

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
I’m not sure how the added security in macOS will affect your attempts to set up a cron job in the Terminal app. You may have to allow full disk access for Terminal.app.
Yes, Mac OS has become much more secure over the years so it wouldn't surprise me to find I'll need to allow full disk access for Terminal.app and in doing so I might be opening my computer up to attacks that otherwise I wouldn't need to be concerned about.

Discretion is usually the better part of valor so I might be 'asking for trouble' by doing so.
 

revmacian

macrumors 68000
Oct 20, 2018
1,745
1,461
USA
Yes, Mac OS has become much more secure over the years so it wouldn't surprise me to find I'll need to allow full disk access for Terminal.app and in doing so I might be opening my computer up to attacks that otherwise I wouldn't need to be concerned about.

Discretion is usually the better part of valor so I might be 'asking for trouble' by doing so.
Nah, just stick with safe computing practices and you should be fine. I don’t think there is much danger in just allowing full disk access to the Terminal.. so long as you learn proper commands and maintain a sufficient backup scheme.

I dumped Windows in 2001 and moved to Debian GNU/Linux. I’ve been using Linux and macOS since then (haven’t touched a Microsoft product) and I haven’t experienced any catastrophes. The more you learn the better off you’ll be :cool:
 

stradify

macrumors member
Original poster
Jul 4, 2015
90
29
USA
Will give it a go! Thanks for encouragement. Now get started on putting the parts list together for that new Linux box!
 
  • Like
Reactions: revmacian
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.