Apple Notes

Downloads for Apple Developers

https://developer.apple.com/download/more/

Keyboard Shortcuts

There are many useful keyboard shortcuts that I've come across over time, and I try to include them here so they don't get forgotten.

Open Spotlight

COMMAND + SPACE

Maximize window

COMMAND + TAB to highlight the minimized window you want to maximize.

Before releasing the COMMAND button, hit the OPTION button (ALT on a windows keyboard).

Resource: https://superuser.com/questions/196141/keyboard-shortcut-to-unhide-or-unminimize-a-window-in-os-x

Put mac to sleep

OPTION + COMMAND + POWER

Put display to sleep

CTRL + SHIFT + POWER

Resource: https://support.apple.com/en-us/HT201236

Jump to URL bar in Chrome

COMMAND + L

Resource:
https://apple.stackexchange.com/questions/11573/chrome-keyboard-shortcut-to-go-to-address-bar

Show hidden files and folders in Finder

CMD + SHIFT + .

Resource:
https://ianlunn.co.uk/articles/quickly-showhide-hidden-files-mac-os-x-mavericks/

Go to folder in Finder

CMD + SHIFT + G

Resource:
https://blog.bejarano.io/fixing-cron-jobs-in-mojave.html

Lock Screen

CTRL + CMD + Q

Mirror display (or stop mirroring)

CMD + F1

OSX Terminal Stuff

Highlight text

Hold OPTION + COMMAND and select the text you want with your mouse.

SSH

Check ssh service status

sudo systemsetup -getremotelogin

Enable SSH

sudo systemsetup -setremotelogin on

Disable SSH

sudo systemsetup -setremotelogin off

Resource: http://osxdaily.com/2016/08/16/enable-ssh-mac-command-line/

Disable screensharing

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.screensharing.plist

Create account

sudo dscl . -create /Users/niceuser
sudo dscl . -create /Users/niceuser UserShell /bin/bash
sudo dscl . -create /Users/niceuser RealName "Super Nice Guy"   
sudo dscl . -create /Users/niceuser UniqueID *unique id*
sudo dscl . -create /Users/niceuser PrimaryGroupID 80 # Make user part of Admin group
sudo dscl . -passwd /Users/niceuser
sudo dscl . -append /Groups/admin GroupMembership niceuser

You can create a home directory for the user if you'd like as well:
sudo mkdir /Users/niceuser

Resources:
https://smallbusiness.chron.com/add-user-terminal-mac-os-x-screen-sharing-31846.html
https://discussions.apple.com/thread/8248721

Remove account

sudo dscl . -delete /Users/niceuser

Delete the home directory if you created it:
sudo rm -rf /Users/niceuser

Resource: https://apple.stackexchange.com/questions/310308/delete-a-standard-user-from-mac-os

List users

sudo dscl . list /Users | grep -v "^_"

Take screenshot

Output to /var/tmp/.tmp

screencapture -x /var/tmp/.tmp

Get version of OSX Running

sw_vers -productVersion

Resource: https://www.tecmint.com/send-a-message-to-logged-users-in-linux-terminal/

Mount NFS share

sudo mkdir /mnt/name_of_system_folder
sudo mount -t nfs -o rsize=65536,wsize=65536,intr,hard,tcp,locallocks,rdirplus,readahead=128 system.com:<path to volume mount> /mnt/name_of_system_folder

Resources:
https://care.qumulo.com/hc/en-us/articles/115008111268-Recommended-NFS-Mount-Options
https://willhaley.com/blog/mount-nfs-share-on-a-mac/

Unmount NFS share

sudo umount -Af -t nfs,smbfs

Resources:
https://askubuntu.com/questions/292043/how-to-unmount-nfs-when-server-is-gone
https://superuser.com/questions/249611/how-to-forceably-unmount-stuck-network-share-in-mac-os-x

Automount an NFS share in /Volumes

https://gist.github.com/L422Y/8697518

SSHFS

Install:

brew cask install osxfuse
brew install sshfs

Mount remote system:

mkdir /mnt/remote_system 
cd /mnt/remote_system
sudo sshfs -o allow_other,defer_permissions user@remote_system.com:/ /mnt/remote_system

Resources:
https://www.digitalocean.com/community/tutorials/how-to-use-sshfs-to-mount-remote-file-systems-over-ssh
https://howchoo.com/g/ymmxmzlmndb/how-to-install-sshfs

Creating plist files

Plist files can be used as an alternative to cron jobs. This specific example will run even if there are no users logged in.

This particular one will run a python script, not_evil.py every day at 3 am.

Create /Library/LaunchDaemons/job.plist with this content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

    <key>Label</key>
        <string>not.an.evil.job</string>
    
    <key>Program</key>
        <string>/Users/notabackdooruser/not_evil.py</string>
    
    <key>WorkingDirectory</key>
        <string>/Users/notabackdooruser/</string>
    
    <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
                <integer>3</integer>
            <key>Minute</key>
                <integer>0</integer>
        </dict>    

    <key>EnvironmentVariables</key>
        <dict>
	    <key>http_proxy</key>
	        <string>http://companyproxy.com:80</string>
        </dict>
</dict>
</plist>

Make sure the file is owned by root (sudo chown root job.plist) and that wheel is set for the group (sudo chgrp wheel job.plist).

To load the file to the running daemons, run:

sudo launchctl load job.plist

To remove the file from the running daemons, run:

sudo launchctl unload job.plist

Another example

This will run the a bash script in the context of a specific user every minute. It will output the results to /tmp/test.out or /tmp/test.err if there is an error in running the command.

To start, navigate to ~/Library/LaunchAgents and put this in a file called com.yourusername.crontabtest.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>EnvironmentVariables</key>
        <dict>
            <key>PATH</key>
            <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
        </dict>

        <key>Label</key>
        <string>net.yourusername.task</string>

        <key>ProgramArguments</key>
        <array>
            <string>bash</string>
            <string>-c</string>
             <string>/Users/yourusername/Library/LaunchAgents/bashscript.sh</string>
        </array>

        <key>StartInterval</key>
        <integer>60</integer>

        <key>StandardOutPath</key>
        <string>/Users/yourusername/brew-output.txt</string>

        <key>StandardErrorPath</key>
        <string>/Users/yourusername/brew-errors.txt</string>
    </dict>
</plist>

Follow this by adding the bash script to /Users/yourusername/Library/LaunchAgents/bashscript.sh and running chmod +x on it.

With the plist and script in place, you can enable it with:

launchctl load com.yourusername.crontabtest.plist

and disable it with:

launchctl unload com.yourusername.crontabtest.plist

Resources:
https://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs
https://medium.com/@chetcorcos/a-simple-launchd-tutorial-9fecfcf2dbb3

View logs

If you're having trouble getting your plist to work, run this to see the logs:

tail -f /var/log/system.log

Then in another tab or window, unload and load the plist:

launchctl unload -w ~/Library/LaunchAgents/com.yourusername.bashscript.plist
launchctl load -w ~/Library/LaunchAgents/com.yourusername.bashscript.plist

Resource: https://stackoverflow.com/questions/1370901/very-simple-launchd-plist-not-running-my-script

Run bash script on startup

~/Library/LaunchAgents/com.yourusername.bashscript.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
    </dict>

    <key>Label</key>
    <string>com.yourusername.bashscript</string>

    <key>Program</key>
    <string>/Users/yourusername/Library/LaunchAgents/bashscript.sh</string>

    <key>RunAtLoad</key>
    <true/>

  </dict>
</plist>

Resources: https://webcache.googleusercontent.com/search?q=cache:5gISXy0YwUMJ:https://devops.itsltns.io/2019/05/01/file-mounting+&cd=1&hl=en&ct=clnk&gl=us

Run every 6 hours

<key>StartInterval</key> 
<integer>21600</integer>

Resource: https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/

Screen sharing via finder

  1. Open Finder
  2. Ctrl-k
  3. vnc://target_ip

Resource: http://hints.macworld.com/article.php?story=20100927085636535

Permission denied for creating cronjobs

  1. Give Full Disk Access to iterm (if you're using it) or terminal via these instructions
  2. Relaunch the terminal and run this command to test if the issue has been resolved: ls ~/Library/Messages

Resources:
https://serverfault.com/questions/954586/osx-mojave-crontab-tmp-tmp-x-operation-not-permitted
http://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/

You may need to do this bit of horribleness as well (obviously not ideal from a security standpoint):
https://blog.bejarano.io/fixing-cron-jobs-in-mojave.html

Store SSH Key in Keychain

ssh-add -K ~/.ssh/[your-private-key]

Resource:
https://apple.stackexchange.com/questions/48502/how-can-i-permanently-add-my-ssh-private-key-to-keychain-so-it-is-automatically/250572#250572?newreg=1026a7ae1a72435ea5dca92ea1dbe053

Get CPU thread count

sysctl -a | grep cpu.thread_count | egrep -o '(\d+)'

Brew Notes

Install specific version of package

You can install a specific version of a package using @. For example, to install golang 1.12:

brew install go@1.12

Update script

brew update; brew upgrade; brew cleanup

Run Software Update via CLI

Update all

softwareupdate -i -a

Resource: https://www.jamf.com/jamf-nation/discussions/30828/softwareupdate-l-no-updates-found

Brewfile

Generate Brewfile with existing packages

brew bundle dump

Resource: https://www.caseyliss.com/2019/10/8/brew-bundle

Show reason for reboot

log show --predicate 'eventMessage contains "Previous shutdown cause"' --last 24h

Resource: https://apple.stackexchange.com/questions/321541/how-can-i-find-the-reason-for-my-macs-recent-restart

Kill all chrome processes

killall Google\ Chrome 

Resource: https://apple.stackexchange.com/questions/158684/chrome-wont-quit-mac-yosemite

Capture output of running process

This would call for something like strace in linux, which is not available in OSX. Instead, we use dtrace:

capture() {
    sudo dtrace -p "$1" -qn '
        syscall::write*:entry
        /pid == $target && arg0 == 1/ {
            printf("%s", copyinstr(arg1, arg2));
        }
    '
}

Resource: https://stackoverflow.com/questions/3425340/how-can-i-capture-the-stdout-from-a-process-that-is-already-running