8

I would like my application to be automatically added to the dock after the package (DMG) is installed?

Does anyone know how to do this?

nbrooks
  • 18,126
  • 5
  • 54
  • 66
andrewmore
  • 215
  • 2
  • 8

2 Answers2

6

There's a couple ways to do this, Andrew, and a lot of this depends on how you're doing your application installing.

If you are using PackageMaker to install your app, you can run a "postflight" script which adds your app's icon to the "defaults" (i.e. the preferences) of the dock. This older MacRumors thread shows how to do that.

If you are not using PackageMaker, then you might have to run an Applescript from within your app that does the same "defaults" writing trick. Here's a thread on AskDifferent that shows how.

In both cases you need to kill the dock (or Finder?) and restart it in order to get the change to pick up and show.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Michael, I don't want to run an applescript within my app. I want to add my app into the dock bar as soon as copying it to Application folder. Is that possible? – andrewmore Nov 20 '12 at 04:41
  • 1
    See why I commented "this depends on how you're doing your application installing"? If this is a drag & drop install, there's no "trigger" you can launch automatically from the DMG to make this happen. You *must* do the dock icon Applescript thing from within your app, or you need to use an installer and then the postflight script trick. – Michael Dautermann Nov 20 '12 at 04:57
1

I would suggest you run the following AppleScript code, replacing myapp with the app you want to add to de dock, including its path.

In the example below, I am adding the system app "System Preferences", but you can do the same with your own path, just assign the path of your app to the myapp variable.

 on run
   set myapp to "/Applications/System Preferences.app"
   try
     tell application "Dock" to quit
   end try
   do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & myapp & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
   try
     tell application "Dock" to activate
   end try
 end run

This follows the suggestion given by Michael, but it should be more gentle as it just quits the Dock rather thank killing it.

If you prefer bash, you could run the following code, again assigning the path of your own app to the myapp variable.

Note: in the bash case, you have to use double slashes when specifying your the path, as you can see in the example.

#!/bin/bash
myapp="//Applications//System Preferences.app"
defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>$myapp</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
osascript -e 'tell application "Dock" to quit'
osascript -e 'tell application "Dock" to activate'
Kubuntuer82
  • 1,487
  • 3
  • 18
  • 40