Hot answers tagged

50

sudo launchctl bootout gui/$(id -u <username>) or sudo launchctl bootout user/$(id -u <username>) Replace username with the target user's user name or replace the whole subshell with the user's uid. This tells launchctl to teardown the users login session (gui specifically refers to the user's temporary login session, user specifies the users ...


44

You need to set 'execute' on the file for it to allow you to run it: chmod u+x /path/to/file.command u is the owner of the file, +x adds 'execute', so u+x gives the owner of the file the ability to execute it …then you can run it in the future by double-clicking it.


40

Short answer A .command script should do the trick Step-by-step Open TextEdit and create a new file Convert it to plain text by clicking Format > Make Plain Text Add your commands, one per line. For example, you could do: #! /bin/bash cd ~/Desktop mkdir myCoolFolder cd myCoolFolder Run chmod u+x ~/Desktop/myCommandScript.command in your terminal, where ~/...


33

All processes get paused when the system goes to sleep, independent of whether the process is part of macOS, an application or some code you wrote on your own. After the system wakes up again, all processes will continue to run. PS: Processes which were waiting for network or disk data when going to sleep may run into timeouts though. But that can happen ...


31

With the benefit of several years of hindsight: user588's answer and koiyu's answer work well, but they rely on utilities (Rez, DeRez, and SetFile) that: aren't installed by default (they come with either Xcode or the developer command-line utilities) are now deprecated (Rez and DeRez, because they relate to Carbon) osxiconutils look interesting, but won't ...


31

Well, here you go with an AppleScript for that. First, create the AppleScript: Open Automator Create a Quick Action Set the input to no input Drag and Drop the Run AppleScript workflow element onto the grey space. Paste the code from below into the AppleScript Save the workflow as Create new file If you have iCloud Drive activated, make sure you are saving ...


30

To log out purely from terminal (or a remote ssh session), just kill the loginwindow process: sudo pkill loginwindow You could get fancy and specify the user if multiple users have a loginwindow process, but this is an easy one shot, no prompt way to end a user's graphical session.


30

This was easily done with scripts and now the built-in autoupdate mechanism handles this elegantly. See Tate’s excellent answer for details If the automatic update and upgrade aren’t working, here is another way for older versions. For efficiency (and cool factor), I would use a tool like Lingon to launch this script periodically using launchctl/launchd ...


26

There is no native alternative. You must acquire watch from using Homebrew (brew install watch) or MacPorts (port install watch) if you require an actual executable. You can however, emulate the function of watch. That can be accomplished in a standard bash while loop (from Stack Overflow by Daniel Pittman): You can emulate the basic functionality with ...


26

You have good old bash (or csh, or whatever). You can use it to write shell scripts.


25

Try brew install moreutils and then run the ts command. moreutils installs a bunch of things. https://rentes.github.io/unix/utilities/2015/07/27/moreutils-package/


25

Terminal runs the Unix shell, (bash, zsh, and others) which is obviously a command line scripting environment, which can execute complex scripts, particularly file processing. Python 2.7 comes bundled with MacOS, and always has done. Future?. Ruby, Perl are also included. AppleScript and its JavaScript equivalent (docs) are also there out-of-the box.


21

When passing input to Run Shell Script in Automator you need to change Pass input: to stdin to Pass input: as arguments as shown in the example workflow below.


21

Yes, terminal processes like a python script will stop execution when the machine goes into idle sleep. To prevent your machine from sleeping without changing the settings you can just use the terminal tool caffeinate. Once called it will prevent the machine from sleeping until you cancel the program using ctrl+c. You can pass arguments like the process ...


19

Try this: tell application "iTerm2" set newWindow to (create window with default profile) tell current session of newWindow write text "echo it works!" end tell end tell The write command, when not using without newline, executes the write text command because of the default added newline after the "text to send". Thus the window stays ...


19

There is a very well documented open source script that reads the software update catalogs and parses them for the major macOS installer components, prompts you which build and version to download and will then commence to download each portion to your filesystem and then build an installer application or image based on the options you choose. https://...


18

This has worked for me in the past: Log out (with confirmation) osascript -e 'tell app "System Events" to log out' Log out directly (no confirmation) osascript -e 'tell app "System Events" to «event aevtrlgo»' or osascript -e 'tell application "loginwindow" to «event aevtrlgo»' This way any running application will get noticed and can terminate in a ...


18

Homebrew has an autoupdate subcommand. So you can run: brew autoupdate start To automatically run brew update every 24 hours. The docs linked above outline a number of configuration options. Because you'd like to also run brew upgrade, you can run: brew autoupdate start --upgrade Under the hood brew autoupdate is using launchd.


17

For a more general solution to the bash environment in automator differing from your own you could simply load your personal bash profile at the first line of the automator bash script: source ~/.bash_profile This will make the path and any other environment variables you're used to using available from your automator script.


17

I'm stumped as to why cp is behaving this way if there's no alias involved. However, there is a quick-dirty-and-dangerous utility provided for this exact situation: yes. You can use it to pipe a continuous stream of affirmatives into any command that gives you confirmation prompts (you can use it to send any text, but the default is "y"). yes | cp foo bar ...


17

As patrix has pointed out in a comment, you can set up any Mac to start up or wake up, and sleep or shut down at specific times, via the System Preferences -> Energy Saver -> Schedule... settings. You could also try using AppleScript, e.g. this code (edit it with Script Editor, then save as an Application): tell application id "com.apple.systemevents" -- ...


17

It will break any script which expects BSD-style utilities (different arguments, partially different functionality) But you can install coreutils with Homebrew or Macports which will give them a g prefix (gcp etc). And then adapt your scripts to use those (depending on the platform they run).


16

There is a tool called homebrew-autoupdate which will do this for you. It can automatically run brew update in the background every 24 hours (configurable) to ensure that you always have fresh homebrew data when you go to install/upgrade packages. To install it run brew tap domt4/autoupdate and brew autoupdate --start 43200 to configure it to autoupdate ...


16

The following works: osascript -e 'display notification "Hello world!" with title "Hi!"'


16

If you run AppleScript code via osascript in Terminal, then you need to add Terminal to System Preferences > Security & Privacy > Privacy > Accessibility, to allow it assistive access. I took your code, saved it to a file named testcode and made it executable using chmod. I then ran it in Terminal and received the following error: ./testcode:157:286: ...


16

You only need to run chmod once for a script, not each time you run it. If you don‘t want to do this, you can also use bash ./run to execute it. You can also create a text file with the executable bit set once, and then use it as a template in your editor of choice to create new scripts based on it.


16

While using un-prefixed coreutils has the potential to break any script expecting BSD-style programs, I have been using un-prefixed coreutils for almost 8 years now, and I've never run into a single issue. Given the anecdotal nature of that experience report you should take it with a grain of salt, but my experience has been that the reported dangers of un-...


14

I've created an AppleScript very similar to the @YoshiBotX's one, but with some improvements. The idea is to create an Automator workflow and assigning a shortcut to it using the following steps: Open Automator and create a Service; Set the input to no input, and the application to Finder.app; Drag and Drop the Run AppleScript workflow element onto the ...


14

The following is what I use to mount Samba shares via launchd: /usr/bin/osascript -e "try" -e "mount volume \"smb://guest@${host}\"" -e "end try" Using osascript's mount means any keychain access needed is done "automagically", there's no progress indicator or Finder window, and the command waits for the mount to be available before proceeding (try it with ...


14

I would take a look at a project named shc (Shell script compiler). https://github.com/neurobin/shc It takes a shell script and compiles it into C source code. The C source code can then be compiled with a standard C compiler into a binary executable. This allows you to run the script you've made without the contents of the shell script being immediately ...


Only top voted, non community-wiki answers of a minimum length are eligible