Runar Ovesen Hjerpbakk

Science-based software development

OS X

Make the macOS firewall to permanently allow iOS apps running in the simulator

macOS has an awesome firewall, however the default setting is very harsh for us iOS developers.

Read More

Open folder in Visual Studio Code from the Finder

When working on many simultaneous projects with Visual Studio Code (VS Code), it is convenient having a fast way of opening the project folders. With the open from the terminal shell extension, and the macOS service below, your project folders can be opened from both the Terminal and the Finder.

Read More

WiFi login on OS X

Lately I've been having a first world problem with my 13" Retina MacBook Pro: Safari refuses to load the login page of WiFi Hotspots it has been previously connected to. Quite a hassle in airports and coffee shops.

Safari shows this success-dialog when connecting, but there is no Internet. Success dialog without success

The solution was to abandon Safari and go to a random URL with another browser. Using Firefox makes this dialog appear at my local airport. Firefox with login page

Read More

New features in FermiContainer 1.1

FermiContainer gains features, but while doing so becomes even more simple. How is this possible? Let me tell you:

Automatic resolving of constructor arguments

Through constructor injection, the dependencies known to the container are automatically resolved. No attributes or XML configuration are needed.

[Test]
public void Register_EvenMoreComplexClass_CanBeResolved() {
  m_fermiContainer.Register<IEvenMoreComplex, EvenMoreComplex>();
  m_fermiContainer.Register<ICalculator, Calculator>();
  m_fermiContainer.Register<IComplex, ComplexClass>();
  m_fermiContainer.Register<ClassWithoutInterface>();

  var complexInstance = m_fermiContainer.Resolve<IEvenMoreComplex>();

  IsInstanceOf<IEvenMoreComplex>(complexInstance);
}

public interface ICalculator {}

public class Calculator : ICalculator {}

public interface IComplex {}

public class ComplexClass : IComplex {
  public ComplexClass(ICalculator calculator) {}
}

public interface IEvenMoreComplex {}

public class EvenMoreComplex : IEvenMoreComplex {
  public EvenMoreComplex(
    IComplex complex, I
    Calculator calculator, 
    ClassWithoutInterface classWithoutInterface) {}
}

public class ClassWithoutInterface {}>

Default container instance

var instance = FermiContainer.DefaultInstance;

IsInstanceOf<IFermiContainer>(instance);
Assert.AreSame(instance, FermiContainer.DefaultInstance);

Easier to extend

The Services dictionary is now protected so FermiContainer is easily extendable.

Better performance

C# expressions makes FermiContainer very performant.

Available as source through NuGet

PM> Install-Package FermiContainer.Sources

Read More

Introducing FermiContainer - an IoC container for the rest of us

What the world needs most is more IoC containers in the .Net space.

So I created FermiContianer, the simples IoC container imaginable.

It supports registering implementations of interface using either a default constructor or a factory method.

IFermiContainer fermiContainer = new FermiContainer();

fermiContainer.Register<ICalculator, Calculator>();
fermiContainer.Register<ClassWithOutAnInterface>();

Resolve gives you a new instance each time.

var calculator = fermiContainer.Resolve<ICalculator>();

Assert.IsInstanceOf<Calculator>(calculator);

var calculator2 = fermiContainer.Resolve<ICalculator>();

Assert.AreNotSame(calculator, calculator2);

Singleton will return the same instance.

var calculator = fermiContainer.Singleton<ICalculator>();

var calculator2 = fermiContainer.Singleton<ICalculator>();

Assert.AreSame(calculator, calculator2);

That's it! Available through NuGet for for .Net 4.0 or later, Xamarin.iOS, Xamarin.Mac, Xamarin.Android, Windows 8, Windows Phone 8, Windows Store apps and Silverlight 5. The source lives on GitHub.

PM> Install-Package FermiContainer

If FermiContainer ever becomes too simple for your needs, I recommend LightInject.

Read More

Xamarin.Forms and iOS Simulator Scrolling

Here are a couple of Xamarin quick tips i have found useful the last couple of days.

iOS simulator refuses to scroll

If your iOS simulator refuses to scroll your ListView or TableView, try restarting it. The simulator sometimes locks up and disables scrolling for some reason. This has only happened during my Xamarin.Forms development, but I have no clue as of the source.

Open another instance of Xamarin Studio

To open another instance of Xamarin Studio on OS X, use the following command in the Terminal.app:

open -n "/Applications/Xamarin Studio.app"

To open another instance of Xamarin Studio on OS X, use the following command in the Terminal.app:

open -n "/Applications/Xamarin Studio.app"

Given that Xamarin Studio is in its default location. Sometimes Unix can be useful, who knew? 😬

Read More

OS X script for fetching App Store icons

Via Brett Terpstra:

I wrote a quick script to allow me to search for an iOS app by name and instantly write its 1024px version (or the highest resolution available) to the current directory in Terminal. There’s also an OS X application version at the end of this post, so you can perform this trick without needing the command line at all.

I came across this nice OS X script for fetching the logo from your favorite app in App Store. Works great!

iOS Simulator scaling on a retina display

tl;dr

If the iOS Simulator scaling is set to 75% or 100% on a non-retina display, scale it to 50% before moving it to a retina display.

Symptom

One of the biggest advantages of a Retina MacBook Pro is that the iOS Simulator can run at 100% resolution and you get a good feel for what your app will look like on the real device. Running the simulator on an external display, you often need to scale down the window to 75% or 50% in order to view the whole thing.

100 scaling quite large

If the simulator scaling is set to 100% or 75% on a non-retina display, it will retain its seemingly large size when moved to the retina display.

Double retina, not exactly useful

Solution

Before moving the simulator to a retina display, set the scaling to 50%. The simulator will detect the retina display and set its scaling to 100%.

Scale

Exactly as we want.

Perfect!

Cause

The iOS Simulator only seems to auto scale if its scaling is set to 50% on a non-retina display.

Read More

Remove duplicated entries for same application in Finder context menu

Symptom

Multiple duplicated entries for the same application in the OS X Finder Open With context menu. I first noticed this after upgrading to Mountain Lion (10.8).

Duplicate entries pre

Solution

  1. Open Terminal.app
  2. Run the following command:
/System/Library/Frameworks/CoreServices.framework//Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user

The lsregister command is used to query and reset the Launch Service database. This is the database used to determine the default application and it controls the contents of the Open With context menu.

  • -kill resets the Launch Services database before doing anything else
  • -r performs a recursive directory scan, but does not recurse into packages or invisible directories
  • -domain local -domain user specifies the domains in which the command should be run. Local and User should suffice.

The result can be seen in the screenshot below. All the duplicated entries from before are now gone.

Duplicate entries post

For more information, read lsregister: How Files Are Handled in Mac OS X.

Cause

Still unknown, but the problem has yet to reappear. This reminds of the Everything's broken and nobody's upset post by Scott Hanselman.

The answer? Uninstall, reinstall, stop, start, restart.

Read More

Optimize SVG images for retina displays

As high DPI screens are becoming more and more common, it is necessary to improve the images around the web to look great on these new screens and not just the low DPI screens of the past. Guides like this can be used when dealing with ordinary raster images.

VectorBitmapExample

Another solution is to create vector graphics, for example in the SVG format. Vector images can look equally great in any resolution and at any DPI. The caveat is that the image depicted must be usable even when rendered using few pixels on low DPI displays. Thus the format is best used for icons and images without a lot of stuff going on. In the example to the right you see that the upper text on the bottle is unreadable when zoomed out.

After you have created your SVG image, you should make sure that it has the smallest file size possible. Just as we have done with raster images since forever by using tools like ImageOptim and its like.

To minimize the SVG's file size, I use the python script Scour - an SVG scrubber.

Scour is an open-source Python script that aggressively cleans SVG files, removing a lot of 'cruft' that certain tools or authors embed into their documents

This will decrease the file size with as much as 50%. Since I use the script on all of my SVG images before uploading them to my website, I've created an OS X service to run Scour from the Finder.

for f in "$@"
do
    if [ ${f: -4} == ".svg" -o ${f: -4} == ".SVG" ]
    then			
    python /Applications/Utilities/scour/scour.py --enable-comment-stripping --create-groups --enable-id-stripping --shorten-ids --set-precision=5 --quiet --indent=none -i "$f" -o "$f 2"       
    mv "$f 2" "$f"
    fi
done

The service contains a shell script that checks if the image file selected really is an SVG image and runs the Scour with a couple of optional parameters. Then it replaces the original image with the optimized version. If you want to keep the original image, this step can be deleted.

Optimize SVG Automator Service

To use the service as is, Download the OS X service and Scour. The Scour script should be copied to /Applications/Utilities/scour/, before opening the service to install it. After the service is installed, you can right click on any SVG image in the Finder and choose Optimize SVG.

Optimize SVG Automator Service

Read More

Install Java in Mountain Lion

The new version of OS X (10.8), Mountain Lion, does not ship with Java preinstalled. Luckily, installing Java is easier than ever.

  1. Open Terminal (Applications > Utilities > Terminal).
  2. Type in the Command Java and press Enter. This should give the prompt:

No Java runtime present, requesting install.

  1. Software Update will launch and Java will be installed. After the installation is completed, verify the installation by running the command java -version, which will show the currently installed version of Java. The whole process is shown in the screenshot below.

Installing Java in Mountain Lion

Easiest installation ever, next to shipping with Java preinstalled of course…

Read More

Disable OS X Lion Resume per application

I sometimes divide applications into two groups: one group contains applications which creates content while the other contains viewers. Examples in the first group could be Word or your favorite IDE. The second group contains applications for viewing PDFs, images or videos such as Preview or QuickTime.

OS X Lions Resume feature enables applications to be opened in the same state as when they were closed. This saves time for programs where you wish to continue working on the same stuff, but can be annoying for most viewer applications. Using Preview to view an image from the Finder will open the image together with the images opened the last time the Preview was run. Not always what you want.

Luckily there are (at least) three ways to tweak the Resume behavior:

  1. In System Preferences this setting can be disabled system wide by disabling General > Restore windows when quitting and re-opening apps.
  2. Pressing Option  when quitting an application from the menu or using Cmd+Q will disable Resume for the last session. Cmd+Q = quit and resume later, Opt+Cmd+Q = quit and forget the open windows.
  3. From the Terminal the Resume functionality can be disabled on a per application basis by using the command: defaults write [application identifier] NSQuitAlwaysKeepsWindows -bool false. For example defaults write com.apple.QuickTimePlayerX NSQuitAlwaysKeepsWindows -bool false will disable Resume for QuickTime after a restart. Issuing the command again using true instead of false, will re-enable Resume for the application.

Finding the application identifier

In order to find the application identifier to use in the command above, Activity Monitor can be used.

Open Activity Monitor and search for the relevant application (TextEdit in this example). Press Sample Process and the following window will appear. The identifier can be copied from this window.

Input the command in Terminal using the identifier from step two.