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

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

问题描述

我想自动化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.

(在code示例后的更多详细信息)

(More detail after the code example)

-- 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\" '")

<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mdfind.1.html\"相对=nofollow> mdfind 命令

The mdfind command

咨询中央元数据存储,并返回一个列表
       文件,这些文件匹配给定的元数据查询。查询可以是一个字符串
       或查询前pression。

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 this is what spotlight uses.

<一个href=\"https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mdfind.1.html\"相对=nofollow> 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.

此外,如果你想打开一个新的Finder窗口:

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)

在你可以使用正常的Finder窗口。

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.

在Finder窗口的结果类型的差异是因为在正在显示什么是搜索窗口是别名文件(等级:别名文件),以原始文件,因此原来的项目属性。

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.

苹果有一些工具,将帮助您与code。

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.

这会给你一个包含帮手code上下文菜单。
进入重复程序文件夹中的菜单。

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

然后到处理每一个项目'

这将重复例行添加到您的code。

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.

变量保存在循环迭代次数。即它将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.

等第一循环的 jpegSearch的this_item设置为项目i 相当于写的设置为this_item的jpegSearch项目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..

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

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

有时候你可以做的是建立从匹配项的新列表。通过将其复制到previously申报清单。然后,您可以在名单上的工作重复循环完成后。

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的:获取的文件&gt项目的绝对路径;打开窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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