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

时间:2011-12-01 12:49:07

标签: applescript automator

简而言之,我需要做这样的事情:

  1. 我有一个包含大量文件的文件夹,想要处理扩展名为.epub。
  2. 的所有文件
  3. 所有文件都遵循命名方案:Lastname,Firstname - Title.epub或Lastname,Firstname - Series x - Title.epub,我需要一个解析器,用于Lastname,Firstname,Series(如果存在)和Title。
  4. 我有一个设置元数据的命令行工具:ebook-meta filename -a“Firstname Lastname”-t Title
  5. 1.)有许多小报,但是我需要输入2.)并感谢任何帮助/指针!

1 个答案:

答案 0 :(得分:1)

您可以从以下内容开始,然后进行更改以满足您的需求。它编译,虽然未经测试。

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

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