Re: Install in Dock
Re: Install in Dock
- Subject: Re: Install in Dock
- From: André Berg <email@hidden>
- Date: Thu, 06 Nov 2008 05:02:30 +0100
Hi Luther,
Thanks for sharing your code with us. If I may, I expanded on your code
a little. Apologies for not posting with proper syntax coloring, but I
switched
from Mail to Thunderbird a few days ago. Also please excuse the old-school
continuation character stuff, it's not because I like it so much but more
because I fear the mail transfer will just butcher lines wider than 80
bytes.
Cheers,
André
---------------------------------- CODE START
-------------------------------------
(*
New Dock Folder
Version: 0.1
Created by André Berg on 2008-11-06
Copyright © 2008 Berg Media. All rights reserved.
This script adds to an idea and realisation done by
Luther Fuller from the AppleScript-Users mailinglist.
The handler down below was tweaked and modified
only slightly from the version Luther kindly attached to
the mailinglist. It shows a handler to edit the Dock's
plist (property list) in order to install a folder onto the
dock with certain properties pre-set.
Note: Test needs a folder "Test" on desktop with some
content in it
*)
-- ------------------------------------- Test
---------------------------------------------
set theFolder to alias ((path to desktop as string) & "Test:")
--newDockFolder(theFolder, "Kind", "Stack", "List")
newMenuDockFolder(theFolder)
-- ------------------------------------- Handlers
----------------------------------------
--
-- Description: Installs a new folder folderAlias to the Dock
-- with pre-specified properties.
-- sSortBy "Name", "Date Added", "Date Modified",
-- "Date Created" or "Kind"
-- sDisplayAs "Folder" or "Stack"
-- sShowCont... "Fan", "Grid", "List", or "Automatic"
--
-- Return Value: True if the folder was created false otherwise
--
on newDockFolder(folderAlias, sSortBy, sDisplayAs, sShowContentAs)
-- 0 process parameters
tell application "Finder" to ¬
if (class of item folderAlias) is not folder then return
-- Note: 0 seems to be used for 'Default Value'
-- so counting from 1 works more reliably for me
ignoring case and white space
if sSortBy is "name" then
set nSort to 1
else if sSortBy is "date added" then
set nSort to 2
else if sSortBy is "date modified" then
set nSort to 3
else if sSortBy is "date created" then
set nSort to 4
else if sSortBy is "kind" then
set nSort to 5
else
error "Wrong Parameter: sSortBy must be one of " & ¬
"\"Name\", \"Date Added\", \"Date Modified\", " & ¬
"\"Date Created\" or \"Kind\"" number -1728
end if
if sDisplayAs is "folder" then
set nDisplay to 1
else if sDisplayAs is "stack" then
set nDisplay to 2
else
error "Wrong Parameter: sDisplayAs must be \"Folder\" " & ¬
"or \"Stack\"" number -1728
end if
if sShowContentAs is "fan" then
set nShow to 1
else if sShowContentAs is "grid" then
set nShow to 2
else if sShowContentAs is "list" then
set nShow to 3
else if sShowContentAs is "automatic" then
set nShow to 4
else
error "Wrong Parameter: sShowContentAs is not one of " & ¬
"\"Fan\", \"Grid\", \"List\" or \"Automatic\"" number -1728
end if
end ignoring
try
-- 1 construct the |file-data| record
set folderPath to (POSIX path of folderAlias)
if (last character of folderPath) = "/" then
set folderPath to (text 1 thru -2 of folderPath)
end if
set newFileData to {|_CFURLString|:folderPath, |_CFURLStringType|:0}
--2 construct the |tile-data| record
tell application "Finder" to get name of folderAlias
set newTileData to ¬
{arrangement:nSort ¬
, displayas:nDisplay ¬
, |file-data|:newFileData ¬
, |file-label|:the result ¬
, |file-type|:2, showas:nShow}
-- 3 construct the newMenuFolderItem record
set newMenuFolderItem to {|tile-data|:newTileData,
|tile-type|:"directory-tile"}
-- 4 write to Dock preference file
set prefsFile to ¬
(((path to preferences from user domain) as text) & ¬
"com.apple.dock.plist") as alias
tell application "System Events"
set dockPath to (POSIX path of prefsFile)
set dockRec to (value of property list file dockPath)
(|persistent-others| of dockRec) & {newMenuFolderItem}
set |persistent-others| of dockRec to the result
set (value of property list file dockPath) to dockRec
end tell
delay 1
-- synchronizes user defaults and calculates missing data
tell application "Dock" to quit
-- wait again because we don't want to risc double plist access
-- in case we get two chronologically close calls to this handler
-- delay 0.5
return true
on error
return false
end try
end newDockFolder
--
-- Description: A convenience handler for newDockFolder set to
-- Sort by: Name, Display as: Folder and Show contents as: List
--
-- Return Value: True if the folder was created false otherwise
--
on newMenuDockFolder(folderAlias)
try
my newDockFolder(folderAlias, "Name", "Folder", "List")
return true
on error errMsg number errNum
return false
end try
end newMenuDockFolder
---------------------------------- CODE END
-------------------------------------
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden