Open a Folder or File in a new VS Code window on Mac OS

Problem: When opening a new folder in VS Code (Mac OS), I want it to open a new VSCode window, and not add the folder to the already open window. I’m used to the Atom IDE behaviour from years back.

Fix: There are two settings that help here:
window.openFoldersInNewWindow controls whether a folder should be opened in the same window or a new one.

window.openFilesInNewWindow does the same for Files, i.e. when you open just a single file.

These settings can be found in the ‘Code > Preferences > Settings’ menu item. OR easier still, with the shortcut  ⌘, (or ‘Command’ plus ‘comma’ keys, if you can’t see the shortcut to the left here)

Once in Settings, start typing window.openF and you should see both options there. Set one or both to on for the desired behaviour. Can’t remember if you have to restart VS Code or not after changing this. Try it!

Please let me know if this works for you Nope, not helpful...Yep, more useful than not! (No Ratings Yet)
Loading...

Splitting many files into numbered folders with leading zeros

Problem: I had a few thousand files in a directory and needed them to be in a series of numbered folders – specifically with leading zeros – with no more than 100 files in each. Don’t ask why… just some weird old batch program that needs files presented that way. Obviously manually creating directories and moving the files was going to take a while. Also, this needed to be done via a cron job, so I needed something scriptable.

Solution: Thankfully all sorts of magic can be done on the *nix command line, and we have access to very useful formatting functions like printf and for loops.

The following command achieves what I needed:

i=0; for f in *; do d=dirname_$(printf %03d $((i/100+1))); mkdir -p $d; mv "$f" $d; let i++; done

Continue reading “Splitting many files into numbered folders with leading zeros”

stream_socket_client(): unable to connect to smtp.mailtrap.io:2525 (Permission denied) – CentOS 7 / Mailtrap / Laravel

Problem:
After rebuilding a CentOS 7 system with a Laravel app on it, every time the app attempted to mail a notification using MailTrap, the above error got fired.

Solution:
SELinux. Again. It’s always flipping SE LINUX! The SELinux policy can block in and outbound ports and all sorts of things. It’s a git. A clever useful git.

Running `sudo sealert -a /var/log/audit/audit.log` showed the problem. If sealert isn’t possible, then tailing the audit log with `sudo tail -f /var/log/audit/audit.log` goes some way to finding the problem.

In this case a solution (being Linux there are probably 74 equally effective and largely contested solutions) was to allow Apache to use port 2525 like so:

sudo semanage port -a -t http_port_t -p tcp 2525

Any good? If this helped please let me know: Nope, not helpful...Yep, more useful than not! (No Ratings Yet)
Loading...

Get terminal / bash prompt to show useful info

Problem: Well it’s more of an irritation. My new hosting provider does allow SSH access, but when logged in the bash prompt looks like this, regardless of which account  I’m logged-in as, or where I am:

-bash-4.2$

What I want is for it to show something like:

username@server:/current/path$

Solution: The bash prompt can be customised via a profile script, ideally anything that loads when you log in like ‘.bash_profile’, ‘.bashrc’ or ‘.profile’. If one of these files doesn’t already exist in your home directory (check with ls -la ~ to list the contents) then create one. Here is an example of creating, editing and loading a login script to show a better prompt.

Continue reading “Get terminal / bash prompt to show useful info”

Edit crontab with NANO on centos (crontab -e)

Problem: I don’t know where my root users crontab file is, and this doesn’t usually matter because using the command ‘crontab -e’ opens it for editing anyway… but it opens in VI and I am too stupid / lazy / in a rush to use VI.

[Edit – mid 2018: Earlier this year I went through about 3 weeks of IDE/Editor hell and taught myself Vim. It’s… wonderful]

Solution: The -e switch makes the file open in whatever the default editor for the environment is. To override this, pass the EDITOR environment var to the command when it’s run:

sudo env EDITOR=nano crontab -e

Please let me know if this nano tip was useful, just one click!
Nope, not helpful...Yep, more useful than not! - +91 thumb, 117 overall.
Loading...

Using Mac Image Capture with Nikon D70 (Direct Cable connection)

Problem: Recently I dug out my old Nikon D70 to take some pics on holiday. When I connected it to my Mac (OS X v10.8.4) via the USB cable, Image Capture fired-up as expected, but when I tired to Import them I got an error stating that Image Capture was unable to import the RAW images.

Solution: It turns out that the USB mode needs to be ‘M’ now, (Mass Storage Device Mode). To set this:

  1. Press ‘Menu’ on the D70 (after disconnecting it from the Mac.
  2. On the far left of the menu, select the spanner icon to enter the ‘Settings’ sub-menu.
  3. In the Settings menu, scroll up/down to get to the ‘USB’ setting.
  4. Enter the USB setting, and set it to ‘M’ rather than ‘P’ (Picture Transfer Protocol).
  5. Reconnect the USB cable, and the images should now import ok.

Launch Google Chrome Incognito from the terminal or a shortcut in OSX

Problem: Ok not really a problem, but I want to be able to launch Google Chrome straight to Incognito mode from a shortcut. This is useful when logging into multiple bank accounts, Google Apps accounts, or testing session based websites. etc etc etc.

Fix: Thankfully the Google Chrome.app can be launched with the –incognito switch to do just that. The terminal command to do this, assuming the browser is sitting in /Applications/ is this:

open -a /Applications/Google Chrome.app --args --incognito

Note: –args has to be passed to satisfy the ‘open’ command’s arguments first.

Launching it from a shortcut: If you want to create a shortcut to do this, open AppleScript Editor and enter the following:

tell application "Terminal"
	activate
	do script "open -a /Applications/Google\ Chrome.app --args --incognito;"
	delay 1
	quit
end tell

Note: The space in the Google Chrome.app name must be double-escaped with two backslashes like that to work. Also, without the delay I found that the script exits too quickly or something like that, and it doesn’t work.

Save the above script as an Application, and call it something like ‘Incognito’. Running this app will launch Terminal, Chrome Incognito, then exit Terminal. Bingo!

Giving your Incognito app an Icon: I’ve added the app to my Dock, and given it a special icon (see below for a downloadable PNG icon). To do this:

  • Open the image you want to use as the icon – it should be a 512×512 24bit PNG if possible – and copy the image to the clipboard. If you are using Preview to view the image, do cmd+a to select all, then cmd+c to copy it… this works for most other graphics packages also.
  • Locate the app you created above in Finder, then press cmd+i to bring up the info window (alternatively right-click the app then select ‘get info’).
  • In the resulting pop-up, click on the icon at the top to highlight it like so (notice the blue halo around it):
    Original Script Icon
  • Then use cmd+v to paste the new icon from the clopboard into the icon area and it should look like this:
    New Incognito icon
  • Close the info window, and it’s done. You should now have a nice looking shortcut that opens Chrome Incognito with one or two clicks!

Feel free to use this icon. It’s just the standard one with some ‘colour replace’ work to make it blue. You could paste some tacky sun glasses over it if you wished 🙂

Batch rename file extensions from .jpg to .png in terminal

Problem: A client sent me 164 images with incorrect extensions on them. I needed to change them all from .jpg to .png.

Fix: Open up terminal, navigate to the directory containing all the images and type:

ls *.jpg | awk '{print("mv "$1" "$1)}' | sed 's/jpg/png/2' | /bin/sh

..that should do it! This one-liner makes use of sed and awk unix utilities.