Shortcuts

Option (ALT) key is ⌥ Command cmd key is ⌘ Shift key is ⇧ Return key is ↵ Control (CTRL) key is ^

Since on mac keyboard fn key is at left edge, I use it as ctrl . Remap with System preferences -> Keyboard -> Modifier Keys -> Function key as Ctrl Also tilda and backtick are not on top left corner, so I used different Keyboard -> Input sources -> English British so at least backtick is on top left key.

Another solution to remap keys is to use hidutil https://developer.apple.com/library/content/technotes/tn2450/_index.html You can find key codes in a table. Install Key Codes app https://itunes.apple.com/us/app/key-codes/id414568915?mt=12 to find key codes.

To switch switch keys 0xa (§) and 0x32 (backtick tilde)

hidutil property --set '{"UserKeyMapping":
  [
    {"HIDKeyboardModifierMappingSrc":0x7000000a,
    "HIDKeyboardModifierMappingDst":0x700000032},
    {"HIDKeyboardModifierMappingSrc":0x700000032,
    "HIDKeyboardModifierMappingDst":0x70000000a}
  ]
}'
# to see what is already mapped
hidutil property --get 'UserKeyMapping'
# clear mappings
hidutil property --set '{"UserKeyMapping":[]}'

From terminal toolbar:

  • ⌘ N new window
  • ⌘ T new tab

From System Preferences -> Keyboard -> Shortcuts

  • -> Spotlight
    • ⌘ space spotlight search
    • ⌥ ⌘ space finder : inside finder you can open files with ⌘ o (enter just renames the file)
  • -> Mission control
    • ^ up mission control
    • ^ down application windows in all workspaces, use arrow to focus, use tab to select another application and view it’s windows

You can disable any special key for any app. From System Preferences -> Keyboard -> Shortcuts -> App Chortcuts -> + than select the app and write exact name and add shortcut uncluding ⌥ key:

  • I disable Minimize since I do not need it, so add Minimize and random keyboard combination like ⌥ ⌘ M.
  • for iTerm I do not need Clear Buffer so now it is ⌘ ⌥ M
  • Mission control -> Move left space I remaped to ^ ⌘ h (also right space ^ ⌘ l)

From Mac keyboard shortcuts from support

  • ⌘ H hide front app
  • ⌘ ⌥ H to show only front app
  • Fn-delete is forward del since that key does not exists on macbook air

Click on link by holding ⌘ or ⌥ or ctrl will open new tab (with shift it will focus that new tab), download and open context menu (ctrl click also works for selected text).

⌘ shift 3 and ⌘ shift 4 to create screenshots for entire and selected area. Press space after that to select window. Screen shots will be on desktop.

To use apple keyboard on ubuntu I tried to remap Fn key, but that is not possible https://askubuntu.com/questions/370944/remap-fn-key-to-insert-key-on-apple-aluminium-keyboard

To show home folder in Finder, go to Finder Preferences -> Sidebar and enable home folder in sidebar. Inside Finder Go menu you can hold ⌥ key to show Library folder. Some app data can be ~/Library/Containers/com.mydomain.myaoo/Data/Library/Application%20Support/myapp/

I added to .bash_profile:

  • alias ls='ls -Gp' so I can see difference between files and folders

To enable ssh server you need to go “System Preferences -> Sharing -> enable Remote login”

Position windows using spectacle video. Comparison of all os-x-windoiw-manager. I found interesting hammerspoon blog

Karabiner Elements to change keys

Since I like Alt + ` to switch to next window inside same app. You can try to choose English PC in System Preferences->Keyboard->Input Sources. This way keys (§ and `) were replaced, but also single quote key, so better solution is to use karabiner. For Sierra 10.12.5 there is new version https://github.com/tekezo/Karabiner-Elements

My .config/karabiner/karabiner.json file looks:

{
  "profiles": [
    {
      "simple_modifications": {
        "caps_lock": "left_control",
        "non_us_backslash": "grave_accent_and_tilde"
      }
    }
  ]
}

Update packages

Install homebrew Update bash: brew install bash link

brew install coreutils findutils gnu-tar gnu-sed gawk gnutls gnu-indent gnu-getopt
brew info coreutils

brew install bash-completion

Install meld for mac

On high sierra you need to run https://github.com/yousseb/meld/issues/50

unlink /Applications/Meld.app/Contents/Frameworks/libz.1.dylib

Install java JRE from jre-9.0.4_osx-x64_bin.dmg (not tar.gz) http://www.oracle.com/technetwork/java/javase/downloads/index.html Double click will install. You can check if installed using https://java.com/en/download/installed.jsp on safari.

Install vim

brew install vim --override-system-vi
# if you receive error
# xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
xcode-select --install

Instal pip

sudo easy_install-3.6 pip
pip3 install awscli  --user --upgrade

iTerm2

You can maps pageup in iterm. Do ⌘ , to open Preferences -> Keys -> Key mappings and add ⌘ [ and ⌘ ] to map scroll page down and up. But that does not work well for vim since it show previous page in terminal buffer (not in vim) so for vim use ^b and ^f (^d and ^u for half down up)

Tips

  • new disks like sd card will be mounted under /Volumes instead of /media or /mnt

Work spaces

With fn F3 (or swipe up with three fingers) you can create new spaces (any number of workspaces). You can switch between them with left/right swipe with three fingers (you can see more animations on System Preferences -> Trackpad.

Full screen apps open their own space.

AppleScript

You can run and edit scripts in Script Editor. Open Dictionary in File->Open Dictionary or drag application icon to Script Editor application icon.

# comments
-- this is comment

# continuation character Option l
dialog "This is long line" ¬
buttons { "Great", "OK" }

# Boolean
true, false
# Text
"Hi"
# List
{ 1, "Duke" }
# Record
{name: "Duke", age: 35}

# Operators
"a" & "b"

# variables
set myName to "John"
copy 33 to myAge

Statements can include all above. Simple statement is single line, multiline is compound statements

tell application "Finder"
  -- by position index, first/last or front/back, name
  get the name of window 2
  -- same as get the name of second window
  -- same as get the name of 2nd window
  -- same as get the name of the window after the front window
  -- same as get the name of window "Trash"

  -- name can not be updated, but other (like index) can be updated
  set the index of the last window to 1
  set toolbar visible of the front Finder window to true
  set the sidebar width of  the second Finder window to 240
  set the position of the front Finder window to {94, 134}
  set the bounds of the front Finder window to {24, 96, 524, 396}
  set the target of the front Finder window to home

  open window "Trash"
  -- same as select window "Trash"
  tell the front window
    set the current view to flow view
    set the bounds to {528, 116, 1016, 674}
  end tell

  close window "Trash"
  set savedName to name of front window
  set windowRef to a reference to window 1
  set windowId to theWindow's id
  -- same as set windowId to id of the first window

end tell

If conditional with type casting

if (myVar as string) is equal to "asd" then
end if

If you want to check the type of variable

if class of myVar is string then
end if

If you got exception than wrap with try to do exceptions handling

try
    set a to a as number
    display dialog "Yes! It's a number!"
end try

-- or

try
  set windowId to do shell script "defaults read com.duleorlovic.windowShortCuts " & key
on error
  display dialog "Please create window shortcut for key "
end try

Get user input

set theResponse to display dialog "Key (h, j, k, l, semicolon, m, comma, dot, slash, u, i) ?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
if button returned of theResponse is equal to "Cancel" then
 error number -128
end if
set key to text returned of theResponse

Command is series of words that request an action. Command is directed to a target. Choose file

    set xfile to choose file
    set xpath to POSIX path of xfile

Debugging with display dialog "message". Use double quotes, not single quote.

Array list iterate loop

set theList {1, 2, 3}
set first as item 1 of theList
set first to item 1 of theList
on getPositionOfItemInList(theItem, theList)
    repeat with a from 1 to count of theList
        if item a of theList is theItem then return a
    end repeat
    return 0
end getPositionOfItemInList

set theList to {"Sal", "Ben", "David", "Chris", "Jen", "Lizzie", "Maddie", "Lillie"}
getPositionOfItemInList("Maddie", theList)

String manipulation https://developer.apple.com/library/content/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html

You can run scripts inside bash with osascript -e 'display dialog "Hi"' or for shell scripts (you need to chmod +x filename.txt

#!/usr/bin/osascript
display dialog "Hi"

You can also call another scpt file

osascript ~/myscript.scpt

TODO: https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW1

Activate window

iTerm supports hotkeys https://www.iterm2.com/documentation-hotkey.html but it is not generic. I rather write window id and hotkey combination in user

do shell script "defaults write com.myname.myapp foo bar"
set myValue to do shell script "defaults read com.myname.myapp foo"

To assign keyboard shortcuts you need to create a service in Automator and create shortcut in System Preferences -> Keyboard -> Shortcuts -> Services

Create service for each key, for example activateWindowL (/home/orlovic/Library/Services/activateWindowL.workflow).

on run {input, parameters}
    -- note that mac path is using colons instead of slash
    run script file "Macintosh HD:Users:dule:config:bashrc:mac_scripts:mac_activate_window.scpt" with parameters {"l"}
    return input
end run

To overwrite existing shortcuts in specific application you need to remap that same menu item to something else, for example in chrome cmd + j is jump to selection so in System Preferences -> Keyboard -> Shortcuts -> App Shortcuts

all Jump to Selection cmd+ald+j

iTerm Clear Buffer cmd+alt+k
iTem Use Transparency cmd+alt+u

scriptEditr Underline cmd+alt+u

To get foremost window

https://apple.stackexchange.com/questions/117421/how-do-i-focus-a-specific-window-with-applescript-without-doing-an-activate-and https://stackoverflow.com/questions/10366003/applescript-google-chrome-activate-a-certain-window/34375804#34375804 http://tom.scogland.com/blog/2013/06/08/mac-raise-window-by-title/ https://macosxautomation.com/applescript/firsttutorial/index.html chap 3 name oroperty

# open last window
do shell script "open -a Google\\ Chrome"
tell application "iTerm"
       activate
       set theWindow to the first item of ¬
               (get the windows whose name is "2. bash")
       if index of theWindow is not 1 then
               set index to 1
               set visible to false
               set visible to true
       end if
end tell
tell application "System Events" to tell process "iTerm"
       perform action "AXRaise" of (first window whose name contains "2.")
end tell

Rails

brew unlink imagemagick
brew install imagemagick@6 && brew link imagemagick@6 --force

gem install puma -v 2.11.3 -- --with-cppflags=-I/usr/local/opt/openssl/include --with-ldflags=-L/usr/local/opt/openssl/lib

brew install qt

Mysql

brew install mysql will will use /tmp/mysql.sock so you can create link to ubuntu folder of mysql socket file

sudo mkdir -p /var/run/mysqld
sudo ln -s /tmp/mysql.sock /var/run/mysqld/mysqld.sock

To permanently change socket file location you can update

sudo vi /usr/local/etc/my.cnf
[client]
socket=/var/run/mysqld/mysqld.sock

Android USB Thethering

Download http://www.joshuawise.com/horndis driver (right click open with) to enable usb tethering from android phone (wireless thethering will consume batery). I needed to restart mac to find my phone in network preferences.

Defaults

Mac user defaults are preferences for user system configurations

defaults read com.mydomain.myapp
defaults read com.mydomain.myapp "MyKey"
defaults write com.mydomain.myapp "MyKey" myvalue
defaults delete com.mydomain.myapp

Logs

You can see logs using console application. You can attach iPhone and see logs for it. Filter in Search, follow last logs with Now button.