Login Items in macOS 10.11 and newer

If you've ever written or tried to write a macOS application that needs to launch itself at login, you've probably spent some time fighting with SMLoginItemSetEnabled.

There are a couple of good tutorials online that explain how to setup a help application and embed it in your main application to launch it.

However, after doing everything right, you may be banging your head against the wall because your application won't launch when you login.

That's what happened to me, anyway.

In my case (and maybe yours), the problem turned out to be copies of my app scattered around my hard drive that were confusing LaunchServices.  Once I removed all but one copy, it worked as expected.

The tricky part is, Xcode puts copies of your app in all sorts of temporary folders, and maybe you've dragged them to the trash.  LaunchServices can get confused by any one of these.

After some mucking around, I found a way to locate all the copies that LaunchServices knows about, and to reset its database once you've cleaned them out.

You use a utility called lsregister, it's probably not in your path, so you'll need the full path to run it.

Find all copies of your app that LaunchServices knows about:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump|grep .*path.*<YourAppName.

After deleting the extra copies, reset the LaunchServices database:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Also, now that Apple has deprecated several LaunchServices APIs, it's not easy to determine if your app is currently set to start on launch.  It seems most apps just don't check anymore, which is probably fine, but I suspect you could call lsregister -dump and parse the output to find out if your app is set to launch or not.

Quick and dirty script to make Icons for macOS

Sometimes you just need a quick icon for your app.  You don't need anything fancy.  You cobble together something quick, then realize you need to save 10 copies of if and remember the syntax to create a .icns file, or drag each one into Xcode separately.  Or maybe find one of those online services that do it for free but sign you up their mailing list.

If none of that appeals to you, here is a quick (and extremely dirty) bash script that will do it for you in a second or two.  It's dead-simple, no fancy tricks and you should have the requirements pre-installed if you have a Mac and Xcode.  

Drop the code in a .sh file and chmod a+x it... it takes one parameter, the path to a .png file.  It should be at least 1536x1536 and should be square.  No error-checking of any kind is performed.

It uses the following 3 utilities that you probably have installed: 

  • sed for a quick regex
  • sips to do the image resizing
  • iconutil to make the .icns file

I'm sure you can improve on it, but I spent more time writing this post about it than I did writing the script, and it suits me just fine!

Hope it saves you some googling and time on stack overflow ; )

 

 

#!/bin/bash
infile=$(echo "$1" | sed -E 's/\.png//g')
echo "Making icons for $infile"

mkdir icon.iconset

sips $1 -Z 1024 --out icon.iconset/icon_512x512@2x.png
sips $1 -Z 512 --out icon.iconset/icon_512x512.png
sips $1 -Z 512 --out icon.iconset/icon_256x256@2x.png
sips $1 -Z 256 --out icon.iconset/icon_256x256.png
sips $1 -Z 256 --out icon.iconset/icon_128x128@2x.png
sips $1 -Z 128 --out icon.iconset/icon_128x128.png
sips $1 -Z 64 --out icon.iconset/icon_32x32@2x.png
sips $1 -Z 32 --out icon.iconset/icon_32x32.png
sips $1 -Z 32 --out icon.iconset/icon_16x16@2x.png
sips $1 -Z 16 --out icon.iconset/icon_16x16.png

sips $1 -Z 512 --out iTunesArtwork@1x.png
sips $1 -Z 1024 --out iTunesArtwork@2x.png
sips $1 -Z 1536 --out iTunesArtwork@3x.png

iconutil -c icns icon.iconset

Tags ,