Applescript:获取文件中项目的绝对路径>打开窗户 [英] Applescript: Get absolute path of item in a File > Open window

查看:23
本文介绍了Applescript:获取文件中项目的绝对路径>打开窗户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to automate JPEGmini which is not entirely scriptable - the only way to control it is via the "Open" dialog.

对每个图像重复调用一次非常慢.

Repeatedly calling it once per image is extremely slow.

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

Instead I'm trying to to perform a search for .jpg or .jpeg files in the "Open" dialog, so that within that result set I can select the batch of files I want to process and open them in a single pass.

(代码示例后更详细)

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

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

Having done the above, how do I access the list of search results, repeat over each item and get it's absolute file path?

我尝试了一些变体,但一无所获.

I've tried a few variations but am getting nowhere.

感谢您的时间.

推荐答案

你可以通过在 shell 命令行和 applescript 之间使用交叉来简化这个.

You could simplify this by using cross between the shell command line and applescript.

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

mdfind 命令

The mdfind command

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

consults the central metadata store and returns a list of files that match the given metadata query. The query can be a string or a query expression.

AFAIK 这就是聚光灯的用途.

AFAIK this is what spotlight uses.

mdfind文档将使您了解此命令的工作原理.但该脚本仅在/"中搜索并查找 内容类型 属性,即 public.jpeg

mdfind documents will give you an idea of how this command works. But ths script searches only in "/" and looks for content type attribute that is public.jpeg

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

The return is text. A list of POSIX Paths of the matching files. This text is like a text document each path on a new line but in effect one item as far as Applescript is concerned.

它们需要在一个applescript 列表中分解.所以我通过询问结果的段落来做到这一点.

They need to be broken up in a applescript list. So I do this by asking for the paragraphs of the result.

另外,如果您想打开一个新的查找器窗口:

Also if you want to open a new finder window:

尝试以下操作:

tell application "Finder"
    activate

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

回答为什么您在尝试获取文件目标的 POSIX 路径时遇到错误.

To answer why you are getting your error in trying to get the POSIX paths of the target of the files.

如果您查看搜索窗口中返回的文件之一的属性.

If you look at the properties of one of the files returned in your Search Window.

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

您将看到文件属性有一个名为原始项目

You will see the file properties have a property named original item

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

If you really want to do it the way you are doing it then one way is to get the original item. Coerce the result into alias form then get the posix path.

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

您可以在普通的查找器窗口中使用.

In a normal finder window you can use.

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

这些只是向您展示正在发生的事情的示例.

These are just examples to show you whats going on.

查找窗口结果类型的不同是因为在搜索窗口中显示的是原始文件的别名文件(类:别名文件),因此是原始项目属性.

The difference in the type of finder window results is because in a Search window what is being displayed is alias files (class:alias file) to the original files, hence the original item property.

更新 2.

浏览列表中的项目并根据另一个列表检查它们很简单.

To go through your items in your list and check them against another list is simple.

Apple 有一些工具可以帮助您处理代码.

Apple have some tools that will help you with the code.

在您的脚本中时.

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

crtl + Mouse Click the Variable that will hold the jpg result as a list.

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

This will give you a contextual menu that contains helper code. Go to the Repeat routines folder in the menu.

然后到它的处理每个项目"

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

This will add a repeat routine to your code.

然后您可以使用它来根据您的其他列表检查每个项目.

And from there you can use it to check each item against your other list.

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

重复例程是这样工作的.

The repeat routine works like this.

遍历给定列表中的每一项

It loops over each item in the given list

它将从项目 1 迭代到列表中项目的数量.

It will iterate from item 1 to the count of the items in the list.

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

The i variable holds the loop iteration number. I.e it will be 1 on the first loop and 300 on the 300th loop.

所以在第一个循环set this_item到jpegSearch的item i等价于写set this_item到jpegSearch的item 1

so on the first loop set this_item to item i of jpegSearch is equivalent to writing set this_item to item 1 of jpegSearch

但是苹果让你不必用用我重复..

But apple saves you having to write each number of each item with the Repeat with i..

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

The variable i can be any word or letter you choose that conforms to the normal allowed Variable naming syntax.

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

Something you can do is build a new list from the matched items. by copying them into a previously declared list. You can then work on that list after the repeat loop has completed.

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

Here bigList is declared as a empty list {}

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

The matched items are copied to the bigList. Each new item it receives is added to the end of the list.

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

这篇关于Applescript:获取文件中项目的绝对路径>打开窗户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆