MacOS: Remove Stubborn Files

Mac trick

Getting rid of stubborn files in TWO steps:

Step 1: Launch Terminal

Type “Terminal” in Spotlight, if you’d never used Terminal

Step 2: Type commands in Terminal:

Access Trash

cd ~/.Trash

Check which files are stored in Trash

ls

Then remove the file “file_that_I_cannot_kill.dmg”

rm file_that_I_cannot_kill.dmg

DONE!

Advertisements

Setup direct access to tools in Terminal, Mac OS

Mac trick

I had just installed Android SDK I want to be able to execute adb directly from terminal without the hassle of typing all the path. That’s how to do it. Once you understand the method, you can add any other tools in the same way.

Reference: http://stackoverflow.com/questions/5526470/trying-to-add-adb-to-path-variable-osx

  1. First open Terminal app
  2. Enter the home directory
    cd ~/
  3. Create a “.bash_profile” file in the home directory
    touch .bash_profile
  4. Open “.bash_profile” file to edit it
    open -e .bash_profile
  5. Now with this file opened in a text edit, add the following line: In this example, let’s say my user name is “xxx“, I store the whole AndroidSDK bunch in “yyy” folder
    export PATH=$PATH:/Users/xxx/yyy/AndroidSDK/sdk/platform-tools/
  6. Save this file and close it
  7. Reload the .bash_profile file
    source ~/.bash_profile
  8. Now test if adb is successfully set into the path
    adb version

Zoom Safari Pages Permanently

Mac trick

Reference: Automatically zoom safari pages on launch

The result, upon opens any new page, Safari automatically zoom (or shrink) it to certain percentage which is the main reason I am stuck with Chrome.

Step 1: In a plain text editor type in:

body { zoom: 125%; }

Step 2: Save it to a css file, e.g. safari_125.css

Step 3: Open Safari Preferences

Step 4: Click the last tab: “Advanced

Step 5: in “Style sheet“, select the css file you just created.

Step 6: Restart Safari

In Mac OS Find & Replace Strings in Multiple files using Batch Script

Mac trick

This is a step-by-step instruction for people who are totally oblivious to the wonderland of programming and coding, people like me  🙂

Suitable scenario:

In Mac OS system, repetitively replace same strings in a large number of plain text files.

Advantage:

  • Reusable; execute in batch
  • Works for Western and Asian characters

Limitations:

  • Only works for plain txt files.
  • Can be quite slow when there are hundreds of txt files.
  • Some symbols can mess up the whole file.
  • Case sensitive (For me it is a disadvantage, but it doesn’t have to be bad. Perhaps there is a parameter to tweak that, but I didn’t manage to find it. If anyone knows please let me know.)
  • There might be limitations in encodings, but I didn’t do any experiment on that.

Warning

Make sure you backup all the files before run the script!!!!

Part 1: Single line to execute “Find & Replace”

Extract from: How to Quickly Find and Replace Text Across Multiple Files with One Command

The command is:

perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' PATH/*.txt

Example: Replace ‘Lucy‘ with ‘露西‘ in all txt files in a dropbox folder called ‘txtFiles‘:

perl -pi -w -e 's/Lucy/露西/g;' ~/Dropbox/txtFiles/*.txt

Explanation:

perl -pi -w -e ...
  • -p: assume ‘while (<>) { … }‘ loop around program and print each processed line too.
  • -i.bak: change the input file (filename) in place and create the file filename.bak as backup.
  • -w : use warnings
  • -e execute the following command
     's/SEARCH/REPLACE/g' ...
  • s in s/: to mark substitution
  • g: make the substitution globally..that is don’t stop after first replacement.

Summary:

So, if you just need to replace one string in batch, simply launch Terminal and type this line. That’s it!

If you need to repetitively find & replace same strings in multiple text files, then please read on.

Part 2: Execute commands in batch

The standard syntax in Terminal, end every command with a “;”

So, type in Terminal:

cmd 1;

cmd 2;

cmd n

Now you can already replace multiple strings in multiple txt files.

If you want to be able to replace the same strings again, please read on!!!

Part 3: Create a executable script file

Extract from: http://forums.macrumors.com/showthread.php?t=126280

Save all commands in the above format in a plain text file.

Save the file with no extension!!! e.g.

exampleScript

Make sure the script is executable, type in Terminal:

chmod 755 ~/Desktop/exampleScript

This command changes the file’s permission, so it is executable.

The 755 is made up of three digits, that represent the permissions for you, others in your “user group” and all others. You want yourself to be able to write to the file and execute it (7) and you want everyone else to be able to read it or execute it but not write it (5). You could also give them 7 if you want (chmod 777 curlscript), etc.

Part 4 (Almost there!): Execute the script

Simply type the script in Terminal including all path, e.g.

~/Desktop/exampleScript

Again, make sure to backup everything before executing the script!!!

The first time I tried, it was a fiasco. One symbol in the script messed up all my txt files, and I didn’t back up!!! There was simply no way back. Don’t be as silly as me.

Add scripts to TextWrangler

Mac trick

Add a script to allow copying the entire row of texts

Open AppleScript Editor (search in Spotlight)

In AppleScript Editor type in:

tell application "TextWrangler"
tell window 1
select line the (startLine of the selection)
copy (contents of the selection) as text to myText
set the contents of the selection to myText & myText
end tell
end tell

Save to (might need to create the folder):

~/Library/Application Support/TextWrangler/Scripts/

Save as “Duplicate Line

Restart TextWrangler application

In TextWrangler application, open from the top menu bar:

Window/Palettes/Scripts

Image

Image

Set Shortcut; any combination of your liking.