Macos Mac用户需要一些关于applescript的文件夹帮助。SFW

Macos Mac用户需要一些关于applescript的文件夹帮助。SFW,macos,terminal,applescript,Macos,Terminal,Applescript,我有一个充满图像的文件夹,我需要用applescript创建一个包含所有图像名称的文本文件。Applescript有没有办法读入所有文件名,其中大约有10k个文件名,然后输出到文本文件中?任何帮助都会很好!谢谢阅读。为什么不直接从终端上完成呢 ls>pix.txt为什么不直接从终端执行呢 ls>pix.txt以下Applescript将文件夹中的文件名写入文本文件: property theFolder : "File:Path:To:theFolder:" tell application

我有一个充满图像的文件夹,我需要用applescript创建一个包含所有图像名称的文本文件。Applescript有没有办法读入所有文件名,其中大约有10k个文件名,然后输出到文本文件中?任何帮助都会很好!谢谢阅读。

为什么不直接从终端上完成呢


ls>pix.txt

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


ls>pix.txt

以下Applescript将文件夹中的文件名写入文本文件:

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

以下Applescript将文件夹中的文件名写入文本文件:

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

在打开文件进行访问之前,不需要创建文件。你可以这么做

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"
“choose file name”(选择文件名)返回不创建文件的路径,并询问用户是否要覆盖文件是否存在

您还可以使用“return”插入一个换行符,这样可以使代码更整洁:

write fileName & return to openFile
当然,如果您想要一种更简单、更优雅的方法,那么命令就是您需要的地方

ls>thefile.txt
在本例中,“>”将ls list directory命令的输出写入一个文件。您可以在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 path将applescript样式的directory:path:to-your:files转换为unix样式的/directory/path/to\your/files

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

ls "/some/directory/path/">"/some/file/path.txt"
引号用于阻止空格或其他时髦字符混淆shell脚本。为了防止引号在applescript中被读取为引号,使用反斜杠将其转义。您还可以使用单引号,以获得更可读的代码:

shell脚本ls'&theposixdirectory&'>'&theposixResults&'作为字符串吗

它将以壳状的形式出现

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

HTH

在打开文件进行访问之前,不需要创建文件。你可以这么做

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"
“choose file name”(选择文件名)返回不创建文件的路径,并询问用户是否要覆盖文件是否存在

您还可以使用“return”插入一个换行符,这样可以使代码更整洁:

write fileName & return to openFile
当然,如果您想要一种更简单、更优雅的方法,那么命令就是您需要的地方

ls>thefile.txt
在本例中,“>”将ls list directory命令的输出写入一个文件。您可以在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 path将applescript样式的directory:path:to-your:files转换为unix样式的/directory/path/to\your/files

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

ls "/some/directory/path/">"/some/file/path.txt"
引号用于阻止空格或其他时髦字符混淆shell脚本。为了防止引号在applescript中被读取为引号,使用反斜杠将其转义。您还可以使用单引号,以获得更可读的代码:

shell脚本ls'&theposixdirectory&'>'&theposixResults&'作为字符串吗

它将以壳状的形式出现

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

HTH

工作安全吗?我想是的…我想这取决于图像是什么…工作安全吗?我想这是…我想这取决于图像是什么。。