I'm trying to advance an Applescript to set Finder window views - recursively - as many levels deep as there may be in any given folder. I can achieve the desired results but开发者_如何学C only in one window/folder; despite hours of searching and experimenting I'm still unable to wrap my noob head around Applescript's seemingly simple syntax (vis a vis variables and especially loops). By the way, my intended usage for this is as a droplet to leave in the Dock, or perhaps to create some variations and add them to the Finder window Toolbar. Also, even if this ultimately requires opening every folder and subfolder, that would be acceptable to me since I wouldn't be running it at too high in the file hierarchy too often. Many thanks in advance for any help with this.
This works fine for the front Window:
tell application "Finder"
activate
set toolbar visible of front window to false
set bounds of front window to {0, 43, 899, 855}
set current view of front window to list view
set visible of column id name column of list view options of front window to true
set sort column of list view options of front window to name column
set visible of column id version column of list view options of front window to false
set visible of column id label column of list view options of front window to false
set width of column id name column of list view options of front window to 300
set width of column id modification date column of list view options of front window to 111
set width of column id creation date column of list view options of front window to 111
set width of column id size column of list view options of front window to 90
set width of column id kind column of list view options of front window to 180
set uses relative dates of the list view options of front window to false
set calculates folder sizes of the list view options of front window to true
end tell
... and here's just my latest attempt to wrap the above in a repeat loop ... but I'm a noob and increasingly confused by Applescript's otherwise simple syntax vis a vis loops, variables.
tell application "Finder"
activate
set parentFolder to the front Finder window
repeat with entire contents in parentFolder
[ same 'set' commands here ]
end repeat
end tell
It sounds like you may have been reading AppleScript: Beginner's Tutorial.
Since you haven't received any replies yet, I'll try to answer. I'm not very familiar with AppleScript and don't have access to a Mac at this hour. I used to use HyperTalk (in ye olde HyperCard, on System 7) a lot, which AppleScript was based on.
I've tried to account for the possibilities of installing in the Finder window toolbar or dropping one or more files or folders on the script icon.
-- Adopted from a script found on http://snippets.dzone.com/posts/show/1037
-- which in turn was based on a script in the comments of this article:
-- http://www.macosxhints.com/article.php?story=20050924210643297
-- Instructions for use:
-- Paste this script into Script Editor and save as an application to
-- ~/Library/Scripts/Applications/Finder/open in Finder
-- Run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
-- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
-- Activate it by clicking on it or dragging a file or folder onto it.
-- Another nice touch is to give the saved script an icon.
-- To do this, in the Finder, use Get info (Command-I) on both another file and this saved script.
-- (The other file's icon should be the one you want to use on this script.)
-- In the Get Info window of the other file, click the icon (it will highlight blue) and copy it by pressing Comand-C.
-- Then, in the Get Info window of this file click on this script's icon and paste by pressing Command-V.
-- If opened by clicking the toolbar
on run
tell application "Finder"
try
set theCurrentWindow to (folder of front window as string)
useMyViewPreferences(theCurrentWindow)
setContainedFolderViews(theCurrentWindow)
on error errorMessage number errorNumber
showLocalError(errorMessage, errorNumber, "setting the view of the front window")
end try
end tell
end run
-- If dropping a file or folder on the icon
on open (droppedItems)
tell application "Finder"
try
repeat with itemPath in droppedItems
set itemPath to itemPath as string
if not (itemPath ends with ":") then
set x to the offset of ":" in (the reverse of every character of itemPath) as string
set itemPath to (characters 1 thru -(x) of itemPath) as string
end if
useMyViewPreferences(itemPath)
setContainedFolderViews(itemPath)
end repeat
on error errorMessage number errorNumber
showLocalError(errorMessage, errorNumber, "working with the file or files you supplied")
end try
end tell
end open
-- starting with the topmost folder (current window, or dropped on the icon)
on setContainedFolderViews(parentFolder)
try
repeat with eachFolder in (get every folder of parentFolder)
useMyViewPreferences(eachFolder)
end repeat
on error errorMessage nummber errorNumber
showLocalError(errorMessage, errorNumber, "working with the list of folders in the front window")
end try
end setContainedFolderViews
-- Try to set view of selected folder to my preferred view
on useMyViewPreferences(currentFolder)
-- Since this handler is called from inside a loop,
-- the error messages in could potentially get very annoying.
try
set toolbar visible of currentFolder to false
set bounds of currentFolder to {0, 43, 899, 855}
set current view of currentFolder to list view
set visible of column id name column of list view options of currentFolder to true
set sort column of list view options of currentFolder to name column
set visible of column id version column of list view options of currentFolder to false
set visible of column id label column of list view options of currentFolder to false
set width of column id name column of list view options of currentFolder to 300
set width of column id modification date column of list view options of currentFolder to 111
set width of column id creation date column of list view options of currentFolder to 111
set width of column id size column of list view options of currentFolder to 90
set width of column id kind column of list view options of currentFolder to 180
set uses relative dates of the list view options of currentFolder to false
set calculates folder sizes of the list view options of currentFolder to true
on error errorMessage number errorNumber
showLocalError(errorMessage, errorNumber, "setting the view of the current folder")
end try
end useMyViewPreferences
-- Simple-minded error dialog
on showLocalError(errorMessage, errorNumber, doingStuff)
display dialog ¬
"Sorry, an AppleScript error of type " & errorNumber as text & ¬
" (" & errorMessage & ") occured in " & doingStuff & ", because" & ¬
" the programmer tried to use vague memories of HyperTalk to write this script."
end showLocalError
EDIT: I've assumed the Finder makes it possible to set view attributes without opening each folder. If I were to change this to open each folder, I would probably want to ensure that each window the script opened was closed before the next one opened. I think it would still bog down for large numbers of folders, though.
Perhaps an error-check for deep nests or long lists would be wise, which would then prompt the user for confirmation before starting. Would also be important to include a nice way abort the script, preferably via a cancel button in a progress dialog.
精彩评论