Author Archives: HEM DUTT

Inter-Process Communication on Mac – Know How part4(Distributed Notifications)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in previous blog  Distributed notifications work as follows :
1. A notification center manages the sending and receiving of notifications. It notifies all observers of notifications meeting specific criteria.
2. The notification information is encapsulated in NSNotification objects. Client objects register themselves with the notification center as observers of specific notifications posted by other objects.
3. When an event occurs, an object posts an appropriate notification to the notification center.

Posting a distributed notification is an expensive operation. The notification gets sent to a system wide server that then distributes it to all the processes that have objects registered for distributed notifications. The latency between posting the notification and the notification’s arrival in another process is unbounded. In fact, if too many notifications are being posted and the server’s queue fills up, notifications can be dropped.

Let’s look at a simple code to send and receive distributed notification. Read the rest of this entry

Inter-Process Communication on Mac – Know How part3 (Apple Events)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in the previous post Apple Events are the only IPC mechanism which is universally supported by GUI applications on Mac OS X for remote control. Operation like opening a application or telling a application to open a file or to quit etc. can be done using these.
AppleScript is a scripting language built on top of Apple Events which can be used using scripting bridge in a Mac application.

Here in this blog we will see the implementation  of IPC using apple events. Read the rest of this entry

How to detect and identify mounted and unmounted USB devices on Mac using Cocoa

There could be a situation in which we have to notify the application about mounting and un-mounting the USB and also to identify the USB i.e on which path it is mounted, what is the type of USB e.g I-Pod, I-Phone , Pen Drive etc.

First let’s see how can the application be notified of USB mounting and un-mounting.
To get the notification for mounting and un-mounting  of the USB device we can create 2 notifications:

Notification for Mountingthe USB device

[[[NSWorkspacesharedWorkspace] notificationCenter]addObserver:selfselector:@selector(deviceMounted:)  name: NSWorkspaceDidMountNotificationobject: nil];

Notification for Un-Mountingthe USB device

[[[NSWorkspacesharedWorkspace] notificationCenter]addObserver:selfselector:@selector(deviceUnmounted:)  name: NSWorkspaceDidUnmountNotificationobject: nil];

Once we get the notifications, now is the task to get the volume path on which the USB gets mounted.
This code will give an array of volume paths for each mounted USB

NSArray* devices = [[NSWorkspacesharedWorkspace] mountedRemovableMedia];

Now After getting the mounted paths, let’s identify the USBs
Every device has a VendorId, ProductId, ReleaseID and Name configured by the manufacturer. For ex. if the mounted USB is a I-Pod then it can be identified using the combination of it’s VendorId and ProductId. To get these attributes, Add IOKit framework to the project and import following headers in .m file. Read the rest of this entry

Inter-Process Communication on Mac – Know How part2 (Shared Memory)

In  Part 1 of this blog series we discussed about various techniques available for Inter-Process Communication (IPC) on Mac OS X.

As discussed in earlier blog Shared Memory is a implementation for IPC where a memory section is shared between different processes.In other words process A writes to this memory and B can read from this memory, or vice verse. This is fast and data doesn’t have to be copied around. The downside is that it’s really difficult to coordinate changes to the shared memory area.

Here in this blog we will see the implementation  of IPC using shared memory. We will create a server and client and will create a communication channel for data transfer between two processes.

First let us look at the basic implementation of Server using Shared memory Read the rest of this entry

Inter-Process Communication on Mac – Know How part1

Inter-process communication (IPC) can be defined as set of techniques used for  exchanging  data among multiple threads in one or more processes. Processes may be running on one or more computers connected by a network. IPC methods can divided into methods for message passing, synchronization, shared memory, and remote procedure calls (RPC).

Reasons for allowing two processes to communicate with each other may be different :

  • Information sharing
  • Computational speedup
  • Modularity
  • Convenience
  • Privilege separation

In this article we will discuss various techniques available on Mac to accomplish IPC. In the subsequent articles we will see the implementation part for various techniques.
Let’s look at the techniques available for IPC one by one. Read the rest of this entry

Minimum Cost String Transformation Problem with varying cost of operations


In computer science, edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another by counting the minimum number of operations required to transform one string into the other.

Edit distances find applications in natural language processing, where automatic spelling correction can determine candidate corrections for a misspelled word by selecting words from a dictionary that have a low distance to the word in question.

In bioinformatics, it can be used to quantify the similarity of macromolecules such as DNA, which can be viewed as strings of the letters A, C, G and T.

Several definitions of edit distance exist, using different sets of string operations. One of the most common variants is called Levenshtein distance, named after the Soviet Russian computer scientist Vladimir Levenshtein. In this version, the allowed operations are the removal or insertion of a single character, or the substitution of one character for another. Levenshtein distance may also simply be called “edit distance”, although several variants exist.

Levenshtein distance algorithm gives shortest path to reach to the destination string but that is not unique. So, The algorithm will give reliable minimum cost path only if all the operations i.e Insertion, Deletion and Substitution have same cost.

In this text I will analyze and provide an algorithm using decision tree and list for getting minimum cost path in a string transformation where all operations have varying cost depending on the character on which the operation is performed.

In the text I will also present source code in Objective-C language for the implementation of the algorithm.

Read the rest of this entry

How to check if a file is open or not using Cocoa/Objective-C?

First let me make it clear that we are not discussing about finding if a application is running or not, which can be found out very easily by using  [[NSWorkspace sharedWorkspace]  runningApplications].

We are going to discuss how if we can, get the information about a file that whether it is open or not through a Cocoa Application.

Before going further down in the topic let us consider following scenarios to understand the need of the discussion.

1. A Cocoa application might be concerned with performance enhancement and for that it might need to find and list out the opened files.

2. A application might need to find the port associated with a daemon.

3. A application might need to constantly monitor a document or documents in a particular directory and when user close that document after editing, it has to perform certain tasks.

Read the rest of this entry

Programmatically retrieving System Information on MAC

Let us first see how to view system information on Mac manually
To find the Mac OS version and memory information, go to the Apple menu and choose “About This Mac”.


The About This Mac window should look similar to this: Read the rest of this entry

%d bloggers like this: