macos

Add a macOS-style drop shadow to your "partial" screen captures or plain-bordered images

By default, and like it or hate it, the macOS keystrokes to capture full app screen (⇧- ⌘- 5) will add a subtle drop shadow around your saved image.  This can make it more visually appealing when adding a screen shot or image to a document.  The convert app, part of the ImageMagick distribution, has a nice command line function to add this same drop shadow effect to any of your images (e.g., a partial screen capture using .⇧- ⌘- 4)

Once installed, you can use the following command line to update your image with a drop-shadow:
convert "Screen Shot 2021-07-07 at 09.47.13.png" \( +clone -background black -shadow 80x20+0+15 \) +swap -background transparent -layers merge +repage "Screen Shot 2021-07-07 at 09.47.13.png"


which will turn this:
sample_clean.png 143 KB



into this:
sample_shadow.png 132.01 KB


Pro-tip: You can create a pseudo-function in your BASH or ZSH environment - e.g., in your ~/.bashrc file, add this line:
shadow () { convert "$@" \( +clone -background black -shadow 80x20+0+15 \) +swap -background transparent -layers merge +repage "$@"; }
and then source your ~/.bashrc file (first time) to activate it:
source ~/.bashrc

Now, from your command line, you can add a drop shadow to any image file by simply typing:
shadow "Screen Shot 2021-07-07 at 09.47.13.png"
(remember to add double-quotes around your file if it contains spaces)

Done.
1.09 Thousand
Jeff

Convert SVG to PNG

On MacOS, install inkscape (with brew) and use it like this:
$ inkscape --export-type=png --export-dpi=200 --export-background-opacity=0 file.svg
This will create a file.png with transparent background.
1.07 Thousand
Const

Reset Postgres table sequence after DELETE

When deleting recent records from a table, you can reset it's sequence counter if auto-increment field continuity is important to you. Consider actions on the following table, users
 
select max(id) from users;

+-----+ 
| max | 
+-----+ 
| 896 | 
+-----+ 
1 rows in set (0.03 sec)

Then, some delete action against most recent rows: 
delete from users where created_at > timestamp 'yesterday';
96 rows in set (0.15 sec) 

The next auto-increment value for id would be 897 on the next insert. Use the following command to reduce the sequence value for the users table by 96.
select setval('users_id_seq',(select max(id) from users));

+--------+
| setval |
+--------+
| 800    |
+--------+
1 rows in set (0.04 sec)

Why Reset the Sequence?


Resetting a sequence in Postgres is necessary when you want to reinitialize the sequence to a new starting value, allowing for a new contiguous sequence to be generated. This is often required when you need to merge data from multiple databases, change the structure of your database, or recover from data corruption. By resetting the sequence, you can ensure that your primary key columns continue to generate unique and contiguous integers.

Understanding the Primary Key Sequence


A primary key sequence is a database object used to generate unique integer values for primary key columns in a table. It is a crucial component of a relational database management system, as it ensures that each row in a table has a unique identifier. The primary key sequence is typically created when a table is created, and it is used to generate values for the primary key column. Understanding how the primary key sequence works is essential for managing your database effectively.

How to Reset the Sequence


To reset a sequence in Postgres, you can use the ALTER SEQUENCE command. This command allows you to change the definition of a sequence generator, including its starting value, increment, and maximum value. To reset the sequence, you need to specify the name of the sequence, the new starting value, and the increment. For example, to reset a sequence called “my_sequence” to start from 100 with an increment of 1, you would use the following command: ALTER SEQUENCE my_sequence RESTART WITH 100 INCREMENT BY 1; Alternatively, you can use the SETVAL function to reset the sequence to a specific value. This function sets the current value of the sequence to the specified value, and it can be used to reset the sequence to a new starting value. For example: SELECT setval('my_sequence', 100);

Precautions and Considerations


Before resetting a sequence, it is essential to take precautions to avoid data corruption or inconsistencies. Here are some considerations to keep in mind:

  • Make sure you have a backup of your database before making any changes to the sequence.
  • Ensure that you have the necessary permissions to modify the sequence.
  • Be careful when resetting the sequence, as it can affect the integrity of your data.
  • Consider the impact of resetting the sequence on your application and users.

Alternative Methods


In addition to using the ALTER SEQUENCE command or the SETVAL function, there are alternative methods for resetting a sequence in Postgres. One approach is to use the pg_get_serial_sequence function to retrieve the name of the sequence associated with a column, and then use the ALTER SEQUENCE command to reset the sequence. For example: SELECT pg_get_serial_sequence('my_table', 'id'); ALTER SEQUENCE my_table_id_seq RESTART WITH 100; Another approach is to use the SQL function to migrate the primary key sequence to a new starting value. This can be particularly useful when you need to ensure that the sequence values are contiguous and unique across multiple tables or databases.
1.07 Thousand
Jeff

My most used slack shortcuts

⌘-K(or T) to open the channel switcher (I navigate channels solely with this shortcut and have my slack set to hide all channels without new messages)

⌘-F opens the search box. Can modify searches with from:@user and in:#channel for extra awesomeness

Up Arrow will edit you last message; perfect for when you hit Enter too early or mispell things

+:emoji: react to the last message. Have to be quick so nobody else replies before you react though or you'll react to the wrong message

/remind [someone usually myself] [to do something] [at this time in words] not really a shortcut but is great for not forgetting simple tasks

The channel switcher also supports fuzzy matching to some extent. If a channel is named foo-bar you can type fb to show it. It's doesn't work so well with multi-user-dm's though.

1.07 Thousand
Joe

Chrome tab manipulation

Sometimes I want to go back to the last page I viewed while still keeping the same one open. You can very easily achieve this with shortcuts

  1. ⌘-L to focus the omnibox
  2. ⌘-Enter to open the current URL in a new tab
  3. ⌘-⇧-[ to go back to the original tab
  4. ⌘-[ to go back to the previously viewed page in the original tab
1.07 Thousand
Joe

Add spacers to your macOS app dock to make groups

Open term (or equivalent) and paste the following one-liner:

defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'; killall Dock


When the Dock restarts, there should be a draggable blank space that you can move anywhere. Repeat to your heart's desire.

1.07 Thousand
Jeff

Change system names on macOS

From term (or equivalent), copy and paste:

To change your public Host Name (i.e., jeff.oozou.com): 
sudo scutil --set HostName "New Name" 

To change your local Host Name (i.e., jeff.local): 
sudo scutil --set LocalHostName "New Name"

To change your Finder Computer Name (i.e., Jeff's MBP): 
sudo scutil --set ComputerName "New Name"

Finally, flush your DNS cache to force refresh: 
dscacheutil -flushcache 

1.07 Thousand
Jeff

Use FFmpeg and Apple Script to compress videos

Build an application you can drag-and-drop videos onto to compress them significantely:

Make sure you have ffmpeg installed (brew install ffmpeg).

Create a new Apple Script app: AppleScript on open filelist repeat with i in filelist tell application "Terminal" do script "ffmpeg -i " & POSIX path of i & " -vcodec h264 -acodec mp2 " & POSIX path of i & "_compressed.mp4" end tell end repeat end open

1.07 Thousand
Const

Get the password of the wifi you're on

wifi_password() {
    airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
    ssid="`$airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`"
    pwd="`security find-generic-password -D 'AirPort network password' -ga \"$ssid\" 2>&1 >/dev/null`"
    echo $pwd
}


$ wifi_password

password: "!forHackers"


1.07 Thousand
Ali

Cleanup stale formulas on brew

Use brew cleanup to get some spaces back!

Use brew cleanup -n to list what's gonna get cleanup

Use brew cleanup to clean only that particular formula

<3

1.07 Thousand
Tino