Creating a new item in the dock using applescript...

Hello,

I am trying to programatically add my application icon to the dock when the install occurs.

Does anyone know how to do this?

I am new to MAC’s and am stuck.

:slight_smile:

Applescript can’t do this for now, but I think this is a planned feature for AS engineers…

Does anyone know if this item has been implemented in AS yet? If so, can you point me at some documentation?

Thanks alot

Mike

It seems to me that there is a way using the “defaults” command to modify the com.apple.dock.plist file. The following code will read the relevant section of the file but I’m not sure how to create the appropriate new record using the “defaults write…” command.

Jon

I found the perl script to add an application’s icon to Dock.

When I saw the installer package of iLife 04, I found it can add icon to Dock.
This “postflight” perl script requires “PlistBuddy” command which is sometimes contained in Apple’s installer package (iLife, iPhoto, iChat AV 2.1 beta, iChat 2.1, Beta version of OS updater and so on).

I don’t know we can re-distribute “PlistBuddy” program within each our products.
But this may be a little help to understand how to add App icon to Dock.

-----------postflight file (perl) for installer package ( AS IS )

#!/usr/bin/perl

my $packagedir = $ARGV[0];
my $target = $ARGV[2];

########################################################################################
system(“/usr/bin/touch”, “/tmp/com.apple.pkg.Registration.cookie”);
unlink(“/tmp/com.apple.mpkg.iLife”);

sleep(1);

########################################################################################
if (opendir(USERHANDLE, “/Users”)) {
while (defined(my $USER = readdir(USERHANDLE))) {
next if $USER =~ /^¥./;
next if $USER eq “Shared”;

    my $USERDOCK    = "/Users/" . $USER . "/Library/Preferences/com.apple.dock.plist";
    
    if (!-e $USERDOCK) {	
    } else {
        TryToAddDockItem("GarageBand",  "GarageBandApp",    $USERDOCK);
        TryToAddDockItem("iTunes",      "iTunes4",          $USERDOCK);
        TryToAddDockItem("iPhoto",      "iPhoto",           $USERDOCK);
        TryToAddDockItem("iMovie",      "iMovie",           $USERDOCK);
        TryToAddDockItem("iDVD",        "iDVD",             $USERDOCK);
    }
}
    
closedir(USERHANDLE);

unlink("/tmp/com.apple.pkg.GarageBand");
unlink("/tmp/com.apple.pkg.iTunes");
unlink("/tmp/com.apple.pkg.iPhoto");
unlink("/tmp/com.apple.pkg.iMovie");
unlink("/tmp/com.apple.pkg.iDVD");

unlink("/tmp/com.apple.mpkg.iLife.GarageBandApp");
unlink("/tmp/com.apple.mpkg.iLife.iTunes4");
unlink("/tmp/com.apple.mpkg.iLife.iPhoto");
unlink("/tmp/com.apple.mpkg.iLife.iMovie");
unlink("/tmp/com.apple.mpkg.iLife.iDVD");

############################    
my $IS_RUNNING = 0;
my $APP = "Dock";
my $PID = 0;
my $rest;

open(PSOUT, "/bin/ps -awwx |");
while( <PSOUT> ) {
    if( /$APP¥.app/ ) {
        ($PID, $rest) = split(' ');
        $IS_RUNNING = 1;
    }
}
close(PSOUT);

if(1 eq $IS_RUNNING) {
    open(ASOUT, "/usr/bin/osascript -e ¥'tell application ¥"$APP¥"¥' -e ¥'try¥' -e ¥'quit¥' -e ¥'on error¥' -e ¥'return (¥"force quit¥")¥' -e ¥'end try¥' -e ¥'end tell¥' |" );
    while(<ASOUT> ) {
        if( /force quit/ ) {
                system("kill -9 $PID");
        }
    }
}

}

#####################################################################
exit 0;

#####################################################################
#####################################################################
#####################################################################
#####################################################################

sub TryToAddDockItem
{
my $appname = $[0];
my $pkgname = $
[1];
my $userdock = $_[2];

my $packagefile     = "/tmp/com.apple.mpkg.iLife." . $pkgname;
my $infoplist       = "/Library/Receipts/" . $pkgname . ".pkg/Contents/Info.plist";

my $firstmoddate    = 0;
my $currentmoddate  = 0;
my $itemlabel       = $appname;
my $itemurl         = "/Applications/" . $appname . ".app";

if (-e $packagefile) {
    open (PACKAGEFILE, $packagefile);
    chomp($firstmoddate = <PACKAGEFILE>);
    close (PACKAGEFILE);
}

if (-e $infoplist) {
    $currentmoddate = (stat($infoplist))[9];
} else {

print $infoplist . " does not exist. No dock item for you!¥n";

    return 0;
};
    
unless ($currentmoddate > $firstmoddate) {
    return 0;
}

if ($appname eq "iPhoto") {
    my $iphotocookie = "/tmp/com.apple.pkg.iPhoto";
    if (open (PACKAGEFILE, $iphotocookie)) {
        chomp($itemlabel = <PACKAGEFILE>);
        chomp($itemurl = <PACKAGEFILE>);
        close (PACKAGEFILE);
    }
}

unless (-e $itemurl) {
    return 0;
}

unless (open(DOCKFILE, $userdock)) {
    return 0;
}
while ( <DOCKFILE> ) {
    if ( /$itemlabel/ ) {
        close (DOCKFILE);
        return 0;
    }
}
close(DOCKFILE);

my $err = AddDockAppToUser($itemurl, $itemlabel, $userdock);
return $err;

}

#####################################################################

sub AddDockAppToUser
{
my $itemurl = $[0];
my $itemlabel = $
[1];
my $dockprefs = $_[2];

my $newitem =       "<?xml version=¥"1.0¥" encoding=¥"UTF-8¥"?>" .
                    "<!DOCTYPE plist PUBLIC ¥"-//Apple Computer//DTD PLIST 1.0//EN¥" ¥"http://www.apple.com/DTDs/PropertyList-1.0.dtd¥">" .
                    "<plist version=¥"1.0¥">" .
                    "<dict>" .
                    "<key>tile-data</key>" .
                    "<dict>" .
                            "<key>file-data</key>" .
                            "<dict>" .
                                    "<key>_CFURLString</key>" .
                                    "<string>" . $itemurl . "</string>" .
                                    "<key>_CFURLStringType</key>" .
                                    "<integer>0</integer>" .
                            "</dict>" .
                            "<key>file-label</key>" .
                            "<string>" . $itemlabel . "</string>" .
                    "</dict>" .
                    "<key>tile-type</key>" .
                    "<string>file-tile</string>" .
                "</dict>" .
                "</plist>";

use Fcntl;
my $tempfile = "/tmp/com.apple.mpkg.iLife.tempfile1";
        
sysopen(TEMP, $tempfile, O_WRONLY | O_TRUNC | O_CREAT);  
print TEMP $newitem . "¥n";
close TEMP;

system( $packagedir . "/Contents/Resources/PlistBuddy",
        "-c", "add persistent-apps:0 dict",
        "-c", "merge " . $tempfile . " persistent-apps:0",
        "-c", "copy persistent-apps:0 persistent-apps:",
        "-c", "delete persistent-apps:0",
        $dockprefs 
        );
        
unlink $tempfile;
return 0;

}