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

发布于 2024-09-27 12:18:22 字数 1730 浏览 3 评论 0原文

我正在尝试改进 Applescript 来设置 Finder 窗口视图 - 递归 - 与任何给定文件夹中可能存在的深度一样多。我可以达到预期的结果,但只能在一个窗口/文件夹中;尽管经过数小时的搜索和实验,我仍然无法理解 Applescript 看似简单的语法(相对于变量,尤其是循环)。顺便说一句,我的预期用途是作为留在 Dock 中的 Droplet,或者创建一些变体并将它们添加到 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 相对于循环、变量的简单语法感到困惑。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-10-04 12:18:22

听起来您可能已经阅读了 AppleScript:初学者教程

既然你还没有收到任何回复,我会尽力回答。我对 AppleScript 不太熟悉,而且此时无法使用 Mac。我曾经经常使用 HyperTalk(在旧的 HyperCard 中,在 System 7 上),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 可以在不打开每个文件夹的情况下设置视图属性。如果我要更改此设置以打开每个文件夹,我可能希望确保脚本打开的每个窗口在下一个窗口打开之前关闭。不过,我认为对于大量文件夹来说,它仍然会陷入困境。

也许对深嵌套或长列表进行错误检查是明智的,然后在开始之前提示用户进行确认。包含一种中止脚本的好方法也很重要,最好是通过进度对话框中的取消按钮。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文