Applescript 读取文件夹、解析文件名并调用其他程序

Applescript 读取文件夹、解析文件名并调用其他程序,applescript,automator,Applescript,Automator,简言之,我需要这样做: 我有一个包含大量文件的文件夹,希望处理扩展名为.epub的所有文件 所有文件都已遵循命名方案:Lastname、Firstname-Title.epub或Lastname、Firstname-Series x-Title.epub,我需要一个用于Lastname、Firstname、Series(如果存在)和Title的解析器 我有一个设置元数据的命令行工具:电子书元文件名-一个“Firstname Lastname”-t Title 1.)有很多小剪贴画,但我需要2.)

简言之,我需要这样做:

  • 我有一个包含大量文件的文件夹,希望处理扩展名为.epub的所有文件
  • 所有文件都已遵循命名方案:Lastname、Firstname-Title.epub或Lastname、Firstname-Series x-Title.epub,我需要一个用于Lastname、Firstname、Series(如果存在)和Title的解析器
  • 我有一个设置元数据的命令行工具:电子书元文件名-一个“Firstname Lastname”-t Title

  • 1.)有很多小剪贴画,但我需要2.)的输入,非常感谢您的帮助/指点

    您可以从以下内容开始,并根据需要进行更改。虽然未经测试,但它可以编译

    set p to POSIX file "/Users/kaass/Desktop/test/"
    tell application "Finder" to set filelist to name of every file of folder p
    
    repeat with filename in filelist
        set text item delimiters to ""
        if text -5 thru -1 of filename is equal to ".epub" then
            set temp to items 1 thru -6 of filename as text
    
            set text item delimiters to " - "
            set myWord to text items 1 thru -1 of temp
            set title to myWord's last item as text
    
            if myWord's length is equal to 3 then set series to myWord's second item as text
    
            set myWord to item 1 of myWord as text
            if myWord contains "," then
                set text item delimiters to ", "
            else
                set text item delimiters to " "
            end if
            set author to (text item 2 of myWord) & space & (text item 1 of myWord)
            set path_and_filename to POSIX path of file p & filename
            do shell script "echo Processing file " & quoted form of path_and_filename & ": " & author & " +++ " & title
            do shell script "/Applications/calibre.app/Contents/MacOS/ebook-meta " & quoted form of path_and_filename & " -a " & quoted form of author & " -t " & quoted form of title
        end if
    end repeat
    

    如果您需要更改某些内容,请发表评论。

    谢谢,这太好了。它需要一些更改才能运行:首先,if“if-text-5到-1的a等于“.epub”然后“然后”,其次,我必须保存并恢复原始分隔符(在if的开头和结尾),否则它只适用于列表中的第一项:“set oldlimiters to my text item delimiters”和“将我的文本项分隔符设置为oldDelimiters”。最后,我还有一个问题:如果只有一个名称,例如“国家地理”,但没有一个,“?除了电子书元的召唤外,它的作用就像一个符咒。这似乎是编码/转义的问题。我做了一些谷歌研究并添加了引用表单,如果我将行更改为“do shell script”echo/Applications/calibre.app/Contents/MacOS/ebook-meta\”&文件名引用表单&“\”-a\”&作者引用表单&“\”-t\”&标题引用表单&“\”,我会得到输出“/Applications/calibre.app/Contents/MacOS/ebook-meta'Adler-Olsen,Jussi-Carl Mørck 1-Erbarmen.mobi'-a'Jussi-Adler-Olsen'-t'Carl Mørck 1-Erbarmen'。如果我将其粘贴到命令行,它将正确执行。如果没有回音,则由脚本直接执行,“没有这样的文件或目录:u“'Adler-Olsen,Jussi-Carl M\xf8rck 1-Erbarmen.mobi'“那么它现在可以与您的更改配合使用了?或者我可以对do shell脚本行进行更好的转义。问题是当前的工作目录不同。如果您查看以下屏幕截图,它将当前工作目录显示为
    /
    ,而文件位于
    /test/
    目录中。