Page 1 of 2

Identify if output goes to the terminal or is being redirected, in Golang May 16 2021

Good command-line tools are a pleasure to work with. A feature I’m always grateful for is when the developer took the time to provide an output that is human readable and an output that is easy to pass to another tool for additional processing. A neat trick to differentiate between these two cases is by having the application identify if the output is being “piped” to another program or not. If the process’s output is not being piped or redirected, we can assume that the user is looking at the results via a terminal. If that is not the case, our application could behave differently and show output that is easier to parse by another program. In this post, I’ll show you how to determine if the stdout of a program is being redirected or piped using the Go programming language.

Read More...

Managing UTI and URL schemes via Launch Services' API from Swift Nov 27 2019

Apple provides the Launch Services API so we can interact with different applications from our current process. We can define URL schemes for our apps, and when that URL is opened, our app gets launched. We can also specify which application to open when a file associated with a specific Uniform Type Identifier (UTI) is being opened. This is registered in the Launch Services database. In this post, I’ll show you how to interact with the Launch Services API to register URL schemes and UTIs.

Read More...

Making a C library available in Swift using the Swift Package Manager Nov 7 2019

System libraries are typically defined using C, that means we need a way to make them available to Swift. In this post, we are going to explore how to use the Swift Package Manager to give us access to C libraries, be it system or user-defined.

Let’s start by talking about Swift modules.

*NOTE: check the following GitHub repositories for the code:

#Swift modules

Read More...

Multithreading with pthreads in Swift Oct 29 2019

Long gone are the days when single process single thread was the norm. We now take multithreading for granted, we expect all our applications not to lockup when we interact with them. We expect them to handle multiple users at the same time, etcetera. In this post, I’ll explain what multithreading is and how to use threads (using pthreads) in Swift.

Before someone tells me that in 2009 GCD (Grand Central Dispatch) was introduced, I know. This post is aimed at anyone that wants to understand how multithreading works using threads. To use threads directly in Swift, we need to interoperate with C code. We need to go down to using pthreads directly. We’ll go from the basics of threads and then write a Server simulator using threads.

Read More...

Creating a Launch Agent that provides an XPC service on macOS using Swift Oct 21 2019

Last week we discussed how to build XPC Services(the .xpc bundles) inside your macOS applications. This week we are going to explore how to provide XPC services that can be used from other applications or tools.

To make the XPC service available to other processes, we are going to create a launch agent. So let’s start by understanding how Launch Agents work.

* You can find the code for the Launch Agent in this GitHub repository

Read More...

XPC Services on macOS apps using Swift Oct 17 2019

We have access to many Inter-Process-Communication(IPC) mechanisms in macOS. One of the newest is XPC1. Before XPC a common way to use IPC, and provide services between processes, was through Sockets or Mach Messages (Using Mach Ports).

Nowadays, we are encouraged by Apple to use XPC in our applications and frameworks that provide services. Also, Apple, in their code, has updated many of its frameworks, daemons and applications to use XPC.

Read More...

Using Kernel Queues (kqueue) notifications in Swift Oct 9 2019

Many components of macOS trace their roots back to BSD. One key aspect inherited from BSD is the Kernel Event Notification mechanism know as Kernel Queues (kqueues for short).

In the past, before kqueue(2)1/ kevent(2)2, when we wanted to track events related to a socket, or if a file descriptor changed, we used a polling mechanism to test for readiness/events (maybe using select(2) or poll(2)). This polling techniques were inefficient and got replaced by the more efficient kqueue API. Instead of continually polling, we can now be notified about events by the kernel. Also, mechanisms like select(2) and poll(2) were restricted to work with file descriptors. kqueue increases the range of operating systems events to monitor not only for file descriptors but also elements like signals, timers, network devices, etc.

Read More...

Understanding the RunLoop model by creating a basic shell Oct 1 2019

When we find ourselves listening for events, reacting to those events, and then going back to listening for more events, we have ourselves an Event-Loop. Having an event-loop is a common scenario. So common that Apple decided to provide us with a model to handle event-loops consistently. In this post, we are going to explore how RunLoops work and use them to build basic shell.

Let’s get started by creating a program that makes use of an event-loop.

Read More...

Using BSD Sockets in Swift Sep 24 2019

Apple provides many useful network frameworks. Network.framework (you can check my previous article on Network.framework if you want to see an example) is the latest. But sometimes we need to go deeper, and the abstractions might get in the way. In this post, we are going to see how to use BSD sockets directly in Swift.

Using BSD sockets means interfacing with C from Swift. Let’s first have a look at some concepts and tips that will help us with using sockets.

Read More...

Understanding Objective-C and Swift interoperability Sep 16 2019

There are many reasons to use more than one programming language in a project. In some cases, a lot of work has gone into building a framework (years maybe). Rewriting the whole framework to have the codebase in the same programming language, might not be the best use of resources. You can see this in macOS between the two main programming languages, Swift and Objective-C (three counting C). In this post, I’ll show how to use Swift code in Objective-C, and how to use Objective-C code in Swift. We are going to explore how the interoperability occurs by building the code manually to get a better understanding.

Read More...