Applescript:获取文件>中项目的绝对路径打开窗户

时间:2013-06-25 19:36:07

标签: applescript posix absolute finder

我正在尝试自动化不完全可编写脚本的JPEGmini - 控制它的唯一方法是通过“打开”对话框。

每张图片反复调用一次非常慢。

相反,我试图在“打开”对话框中搜索.jpg或.jpeg文件,以便在该结果集中我可以选择我要处理的一批文件并在单个文件中打开它们通过。

(代码示例后的更多细节)

-- later within the search results list I get, I want to select these
set filesToSelect to {"/Users/me/Desktop/photo.jpg", "/Users/me/Documents/image.jpeg"}

-- the actual app is JPEGmini but the same principle applies to Preview
tell application "Preview"

  -- start the app
  activate

  -- let it boot up
  delay 3

  -- ensure it still has focus
  activate

end tell

tell application "System Events"

  tell process "Preview"
    -- spawn "Open" window
    keystroke "o" using {command down}
    delay 1
    -- spawn "Go to" window
    keystroke "g" using {command down, shift down}
    delay 1
    -- Go to root of hard drive
    keystroke "/"
    keystroke return
    delay 1
    -- Perform search
    keystroke "f" using {command down}
    delay 1
    keystroke ".jpg OR .jpeg"
    keystroke return
  end tell

end tell

完成上述操作后,如何访问搜索结果列表,重复每个项目并获取其绝对文件路径?

我尝试了一些变化,但我无处可去。

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

您可以使用shell命令行和applescript之间的交叉来简化这一过程。

set jpegSearch to paragraphs of (do shell script "mdfind -onlyin / 'kMDItemContentType == \"public.jpeg\" '")

mdfind 命令

  

查询中央元数据存储并返回列表        与给定元数据查询匹配的文件。查询可以是字符串        或查询表达式。

AFAIK这是聚光灯使用的。

mdfind文档将让您了解此命令的工作原理。但该脚本仅在“ / ”中搜索,并查找 public.jpeg

内容类型属性

回报是文字。匹配文件的POSIX路径列表。这个文本就像一个文本文档,在新行上的每个路径,但就Applescript而言实际上是一个项目。

他们需要在Applecript列表中分解。所以我通过询问结果的段落来做到这一点。

另外,如果你想打开一个新的取景器窗口:

尝试类似:

tell application "Finder"
    activate

    set myWindow to make new Finder window to startup disk
end tell

回答为什么在尝试获取文件目标的POSIX路径时收到错误的原因。

如果查看搜索窗口中返回的其中一个文件的属性。

tell application "Finder" to set fileList to properties of first file of  front window

您将看到文件属性具有名为原始项

的属性

如果你真的想按照你的方式去做,那么一种方法是获得原始项目。将结果强制转换为别名形式,然后获取posix路径。

tell application "Finder" to set aFile to POSIX path of (original item of first file of front window as alias)

在普通的取景器窗口中,您可以使用。

tell application "Finder" to set fileList to POSIX path of (first file  of front window as alias)

这些只是向您展示最新情况的例子。

查找程序窗口结果类型的不同之处在于,在搜索窗口中显示的是原始文件的别名文件(类:别名文件),因此是原始项属性。

更新2。

要查看列表中的项目并将其与其他列表进行核对很简单。

Apple有一些工具可以帮助您使用代码。

在你的脚本中。

crtl +鼠标单击将jpg结果保存为列表的变量。

这将为您提供包含帮助程序代码的上下文菜单。 转到菜单中的重复例程文件夹。

然后到'处理每个项目

enter image description here

这将为您的代码添加重复例程。

enter image description here

然后,您可以使用它来检查每个项目与您的其他列表。

set yourOtherList to {"/Library/Desktop Pictures/Abstract/Abstract 2.jpg", "/Library/Desktop Pictures/Abstract/Abstract 1.jpg"}

    set jpegSearch to paragraphs of (do shell script "mdfind -onlyin / 'kMDItemContentType == \"public.jpeg\" '")
    repeat with i from 1 to number of items in jpegSearch
        set this_item to item i of jpegSearch
        if this_item is in yourOtherList then

            -- do something
            log this_item
        end if
    end repeat

重复例程就像这样。

它遍历给定列表中的每个项目

它将从第1项迭代到列表中项目的计数。

i 变量保存循环迭代次数。即它在第一个循环中为1,在第300个循环上为300。

所以在第一个循环中将this_item设置为jpegSearch的项目i 相当于将设置this_item设置为jpegSearch的第1项

但苹果可以省去你必须用重复我来编写每个项目的每个数字。

变量 i 可以是您选择的任何符合正常允许的变量命名语法的单词或字母。

您可以做的是从匹配的项目中构建新列表。通过将它们复制到先前声明的列表中。然后,您可以在重复循环完成后处理该列表。

此处 bigList 被声明为空列表 {}

匹配的项目将复制到 bigList 。它收到的每个新项目都会添加到列表的末尾。

set bigList to {}

    set yourOtherList to {"/Library/Desktop Pictures/Abstract/Abstract 2.jpg", "/Library/Desktop Pictures/Abstract/Abstract 1.jpg"}
    set jpegSearch to paragraphs of (do shell script "mdfind -onlyin / 'kMDItemContentType == \"public.jpeg\" '")
    repeat with i from 1 to number of items in jpegSearch
        set this_item to item i of jpegSearch
        if this_item is in yourOtherList then

            copy this_item to end of bigList
        end if
    end repeat

    bigList