Wednesday, June 28, 2017

#1 2005-07-27 04:54:04 pm

rebelcommander
Member
Registered: 2005-07-27
Posts: 2

Add items to dock

Hey, I'm trying to create a script that'll let me add items to the dock.  A few hours of searching google haven't led me any closer to an answer so i was wondering if anyone here had an idea.  Basically i need to be able to add and remove items from the dock via a script.
Thanks

Operating System: Mac OS X (10.4)

Offline

 

#2 2005-07-27 08:47:01 pm

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Offline

 

#3 2005-07-27 11:39:11 pm

jonn8
Member
From: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: Add items to dock

The tip from MacOSXHints.com works. The only caveat is that you need to quit the Dock before writing the prefs. You could write the pref and then force quit the Dock using the "kill" command but then other changes made to the Dock by the user will be discarded. So, quit the Dock (getting it to save any changes the user may have made), write the pref and then relaunch the Dock. The Dock will relaunch itself so the last step may not be strictly necessary but it can't hurt. The following script works but I can imagine in some instances that maybe the script won't be fast enough and the Dock will re-spawn before the prefs are changed (this is probably not very probable but it's something to consider if the script fails):

Applescript:

my add_item_to_dock(choose file of type {"APPL"} with prompt "Choose an application to add to the Dock:")

on add_item_to_dock(item_path)
   try
       get item_path as alias -- you need a POSIX path so this coerces the path in case it's an HFS path, alias, file ref, etc.
       set item_path to POSIX path of item_path
   end try
   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>" & item_path & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
   try
       tell application "Dock" to activate
   end try
end add_item_to_dock

Jon


Filed under: dock

Offline

 

#4 2005-08-01 12:05:38 pm

rebelcommander
Member
Registered: 2005-07-27
Posts: 2

Re: Add items to dock

Thanks, that works great.  I have one more question, is it possible to make the script remove all of the dock items first (perhaps by emptying the contencts of the com.apple.dock.plist file).  Basically this script needs to be able to dump all dock items and add specific new ones.
-Thanks

Offline

 

#5 2005-08-01 07:38:17 pm

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Add items to dock

This script should work for you, rebelcommander. You should also able to add multiple apps/files at once.
I found that quitting the dock afterwards is fine for me (panther). I think I heard that in tiger, if you quit the dock it doesn't relaunch so I have forced the dock to open again (by using the repeat, try, and exit repeat statements).

-- Edit: added 'add folder'

Applescript:

set the_choice to (choose from list {"Add Application", "Add File", "Add Folder", "Remove All"} with prompt "Dock actions:") as string
if the_choice is "false" then error number -128
try
   if word 1 of the_choice is "Add" then
       if word 2 of the_choice is "Application" then
           set these_files to choose file of type "APPL" with prompt "Choose an application to add to the Dock:" with multiple selections allowed without invisibles
           set persistent_what to "persistent-apps"
       else if word 2 of the_choice is "File" then
           set these_files to choose file with prompt "Choose a file to add to the Dock:" with multiple selections allowed without invisibles
           set persistent_what to "persistent-others"
       else
           set these_files to choose folder with prompt "Choose a folder to add to the Dock:" with multiple selections allowed without invisibles
           set persistent_what to "persistent-others"
       end if
       
       repeat with i from 1 to the count of these_files
           set this_file to POSIX path of (item i of these_files as alias)
           do shell script "defaults write com.apple.dock " & persistent_what & " -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & this_file & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
       end repeat
   else
       set button_returned to button returned of (display dialog "Are you sure you want to remove everything from your Dock?" buttons {"Cancel", "OK"} default button "OK")
       do shell script "defaults delete com.apple.dock persistent-apps"
       do shell script "defaults delete com.apple.dock persistent-others"
   end if
   
   tell application "Dock"
       quit
       repeat
           try
               activate
               exit repeat
           end try
       end repeat
   end tell
on error error_message number error_number
   if error_number is not -128 then -- not user cancelled
       display dialog "Error: " & error_number & return & error_message
   end if
end try

You can add applications to the file section of the dock too! tongue

Last edited by Qwerty Denzel (2005-08-03 10:43:33 pm)


Filed under: dock

Offline

 

#6 2006-06-16 01:51:53 am

shane.carr
Member
From: North of the South Pole
Registered: 2006-06-14
Posts: 7

Re: Add items to dock

I like the script!  But, how do you set it so when the dock quits the minimised files will stay?  Or when the dock reopens the files return to their minimised state?

Offline

 

#7 2014-04-23 02:34:36 pm

brian_donovan
Member
Registered: 2003-11-10
Posts: 135

Re: Add items to dock

Hi! is it possible to access the dock files from within a hidden folder and just remove them?

Offline

 

#8 2015-08-24 05:49:35 am

ThomasSeidler
Member
Registered: 2015-08-21
Posts: 1

Re: Add items to dock

Great script. 10.6 Snow Leopard users beware, you need to add bolded section (at least for files): "defaults write com.apple.dock persistent-others -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>/Users/user/Filepath/</string><key>_CFURLStringType</key><integer>0</integer></dict></dict><key>tile-type</key><string>file-tile</string></dict>'; killall Dock"

The addition doesn't break it in Mavericks, so i'm inclined to keep it, but it does butcher Dock in 10.6 without it - and crash system in fact!! !wink To fix, if you have already done it, go to user/Library/Preferences/com.apple.dock and *neatly* delete the <dict></dict> you just added, end to end. Save, then killall Dock and it'll be right as rain again! wink

T


Filed under: defaults, dock, 10.6, snow leopard

Offline

 

#9 2017-03-20 05:34:22 pm

nellbern
Member
Registered: 2007-01-16
Posts: 116

Re: Add items to dock

Wonderful scripts. I'm running them in Sierra but it's allowing to select 1 application instead of multiple Applications.sad

Model: iMac
AppleScript: 2.9 (191)
Browser: Safari 602.4.8
Operating System: Mac OS X (10.10)

Offline

 

#10 2017-03-21 05:33:07 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 4312

Re: Add items to dock

Hi.

It's the same in El Capitan. If of type "APPL" (or of type {"APPL"}) is specified with 'choose file', then with multiple selections allowed is ignored for files of that type. With a mixture of types specified, you can choose either one application OR one or more files of the other type(s). With no types specified, you can select multiple application files.

Things work as expected if the 'type identifier' is used instead of the 'file type':

Applescript:

set these_files to choose file of type {"com.apple.application-bundle"} with prompt "Choose an application to add to the Dock:" with multiple selections allowed without invisibles


NG

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.064 seconds, 11 queries executed ]

RSS (new topics) RSS (active topics)