使用Applescript循环递归应用视图选项

时间:2010-10-12 20:26:19

标签: loops applescript

我正在尝试推进Applescript以设置Finder窗口视图 - 递归 - 与任何给定文件夹中的深度级别相同。我可以达到预期的效果,但只能在一个窗口/文件夹中;尽管经过数小时的搜索和实验,我仍然无法将我的菜鸟头包裹在Applescript看似简单的语法(对于变量,特别是循环)。顺便说一句,我对此的预期用途是作为Droplet留在Dock中,或者可能创建一些变体并将它们添加到Finder窗口工具栏。此外,即使这最终需要打开每个文件夹和子文件夹,这对我来说是可以接受的,因为我不会经常在文件层次结构中以太高的速度运行它。非常感谢您提供任何帮助。

这适用于前窗口:

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

...这里只是我最近尝试将上述内容包装在重复循环中...但我是一个菜鸟,并且越来越多地被Applescript的简单语法与vis循环,变量混淆。

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

1 个答案:

答案 0 :(得分:2)

听起来你可能一直在阅读AppleScript: Beginner's Tutorial

由于您尚未收到任何回复,我会尽力回答。我对AppleScript不是很熟悉,而且此时无法访问Mac。我过去经常使用HyperTalk(在系统7中使用HyperCard),这是AppleScript的基础。

我试图考虑在Finder窗口工具栏中安装或在脚本图标上删除一个或多个文件或文件夹的可能性。

-- 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

编辑: 我假设Finder可以在不打开每个文件夹的情况下设置视图属性。如果我要更改它以打开每个文件夹,我可能希望确保打开脚本的每个窗口在下一个打开之前关闭。不过,我认为它仍会因大量文件夹而陷入困境。

对于深巢或长列表的错误检查可能是明智的,然后在启动之前提示用户进行确认。包括一个很好的方式中止脚本也很重要,最好通过进度对话框中的取消按钮。