带有获取每个文件循环的Applescript失败


0

我想在具有多个嵌套文件夹层次结构的磁盘上的所有具有两个特定扩展名的文件上运行脚本。当我选择一个带有较少子文件夹的文件夹时,我的脚本运行良好,但我希望在具有上千个文件夹的整个档案中运行它。AppleScript永久占用get所有文件,然后退出而不返回任何内容,并且在获取文件后不执行任何操作。没有超时消息,没有错误消息。并且Finder变得无响应(并且在脚本退出后仍然停留)。

set myFolder to choose folder with prompt "Choose a folder:"
    tell application "Finder"
        try
            set eafFiles to (every file in entire contents of myFolder whose name ends with ".eaf") as alias list
        on error
            try
                set eafFiles to ((every file in entire contents of myFolder whose name ends with ".eaf") as alias) as list
            on error
                set eafFiles to {}
            end try
        end try
        try
            set pfsxFiles to (every file in entire contents of myFolder whose name ends with ".pfsx") as alias list
        on error
            try
                set pfsxFiles to ((every file in entire contents of myFolder whose name ends with ".pfsx") as alias) as list
            on error
                set pfsxFiles to {}
            end try
        end try
        set myFiles to eafFiles & pfsxFiles
    end tell

    repeat with CurrentFile in myFiles
        set CurrentFile to CurrentFile as string

        do shell script "perl /path/to/perl/script.pl " & quoted form of CurrentFile
    end repeat

1
您要如何处理找到的文件?一的bash脚本可能是去一个更有效的方式。
user3439894 '16

我通过以下方式将它们传递给perl脚本do shell script "perl " & quoted form of perlScript & " " & quoted form of CurrentFile:我敢肯定有一种更好的方法,但是我的脚本编写技能几乎仅限于applescript。

Answers:


1

Perl的File :: Find非常适合遍历文件夹中的所有文件。这种方法一次处理一个文件,速度快,内存效率高。

使用AppleScript呈现文件夹选择,然后将选定的文件夹路径传递给您的perl脚本:

set myFolder to choose folder with prompt "Choose a folder:"
do shell script "perl /path/to/perl/script.pl " & quoted form of POSIX path of myFolder

然后,perl脚本可以处理遍历文件。下面是遍历文件和过滤以只样本perl脚本eafpfsx后缀:

#!/usr/bin/env perl

use strict;
use warnings;

use File::Find;

# Get the first argument passed to the script
my $argument_path = shift;
die("ERROR: an argument must be provided\n") unless $argument_path;
die("ERROR: the argument must be a folder: $argument_path\n") unless -d $argument_path;

# Iterate over every file and folder within the $argument_path
find sub {

    my $filename = $_; # relative to current working directory

    # Skip all but .eaf and .pfsx file types
    return unless $filename =~ /\.eaf$/;
    return unless $filename =~ /\.pfsx$/;

    print $filename."\n";

    # ... deal with the file or folder here

}, $argument_path;

0

尝试使用系统事件而不是Finder。我试图在文件夹中获取特定扩展名类型的每个文件,并不断收到脚本错误。

tell application "System Events"

继续其余的代码。

希望这可以帮助!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.