1

我有一个充满图像的文件夹,我需要使用 applescript 创建一个包含所有图像名称的文本文件。Applescript 有什么方法可以读取所有文件名,大约有 10k 个文件名,然后将其输出到文本文件?任何帮助都会很棒!谢谢阅读。

4

3 回答 3

5

为什么不直接从终端执行。

ls > pix.txt

于 2009-06-25T23:13:14.940 回答
1

The following Applescript will write the names of files within a folder to a text file:

property theFolder : "File:Path:To:theFolder:"

tell application "Finder"

    -- Create text file on desktop to write filenames to
    make new file at desktop with properties {name:"theFile.txt"}
    set theFile to the result as alias
    set openFile to open for access theFile with write permission

    -- Read file names and write to text file
    set theFiles to every item of folder theFolder
    repeat with i in theFiles
        set fileName to name of i
        write fileName & "
" to openFile starting at eof
    end repeat

    close access openFile

end tell
于 2009-07-11T05:29:52.587 回答
1

在打开文件进行访问之前,您无需创建文件。你可以做

set theFile to (theFolder & "thefile.txt")as string
set openFile to open for access theFile with write permission

当然,如果文件存在,它将覆盖它。你可以使用

set thefile to choose file name with prompt "name the output file"

“选择文件名”返回一个路径而不创建文件,如果文件存在,它会询问用户是否要覆盖。

你也可以像这样使用'return'来换行,它使代码更整洁:

write fileName & return to openFile

当然,如果您想要一种简单且更优雅的方式,那么命令就是您需要的地方。

ls>thefile.txt

在此示例中,“>”将 ls(列表目录)命令的输出写入文件。你可以从一个applescript中运行它

set thePosixDrectory to posix path of file thedirectory of app "Finder"
set theposixResults to posix path of file theresultfile of app "Finder"
do shell script ("ls \"" & thePosixDrectory & "\">\"" & theposixResults & "\"")as string

posix 路径的东西是将 applescript 样式directory:paths:to your:files转换为 unix 样式/directory/paths/to\ your/files

请注意,实际运行的 shell 脚本如下所示:

ls "/some/directory/path/">"/some/file/path.txt"

引号是为了防止空格或其他时髦的字符混淆 shell 脚本。为了阻止引号被读取为苹果脚本中的引号,反斜杠用于“转义”它们。您也可以使用单引号,从而获得更易读的代码:

将 shell script ("ls '" & thePosixDrectory & "'>'" & theposixResults & "'") 作为字符串

这将出现在外壳中

 ls '/some/directory/path/'>'/some/file/path.txt'

高温高压

于 2009-07-14T04:59:50.673 回答