AppleScript:Finder排序

时间:2014-05-14 21:02:36

标签: sorting applescript

所以我在AppleScript上很陌生,但我正在尝试制作一个脚本来对我的视频文件进行排序。

我似乎无法解决这个问题。什么都没发生。有人知道吗?

可能的解释可能是文件夹是我的家庭服务器而不是硬盘驱动器?

property nameList : {"Arrow"} -- the start of the fileName to check
property destFoldersPath : {"/Volumes/media/Videos/Serier/Arrow"} -- drag/drop the dest folder in each item of this list

on adding folder items to masterFolder after receiving these_items
    repeat with tFile in these_items
        set destFolder to my check_start_of_Name(tFile as string)
        if destFolder is not "" then -- this file name is in the first list
            tell application "Finder" to move tFile to destFolder with replacing --move this file
        end if
    end repeat
end adding folder items to

on check_start_of_Name(t)
    set tc to count nameList
    repeat with i from 1 to tc
        if t starts with (item i of nameList) then
            set seasonPath to my check_season_of_Name(t)
            set outputPath to (POSIX file (item i of destFoldersPath))
            set completePath to outputPath & seasonPath as alias
            if FinderItemNotExists(completePath) then
                tell application "Finder"
                    make new folder at outputPath with properties {name:seasonPath}
                end tell
            end if
            log outputPath
            return outputPath as alias -- return the dest folder
        end if
    end repeat
    return "" -- this name is not in the list
end check_start_of_Name

on check_season_of_Name(t)
    repeat with s from 1 to 30
        set SW to s
        if s is less than 10 then set SW to "0" & s
        set seasonName to "Season " & SW
        log seasonName
        if seasonName is in t then
            return seasonName
        end if
    end repeat
end check_season_of_Name

on FinderItemNotExists(thePath)
    try
        set thePath to thePath as alias
    on error
        return true
    end try
    return false
end FinderItemNotExists

1 个答案:

答案 0 :(得分:0)

我看了一下你的剧本。我对Folder Action脚本不熟悉,所以这很有教育意义。以下是我理解您要从您的代码中实现的内容 - 如果我错了,请纠正我:

  1. 您的文件名称如下:" {显示名称} {季节XX}等"
  2. 处理名称以列表名开头的文件,忽略其余文件。
  3. 根据文件名确定季节,并创建名为'第XX季'。
  4. 的文件夹。
  5. 最后,它将文件移动到这些文件夹。
  6. 所以我从头开始逐行修复,据我所知,什么是无效的。如下所示,我已经离开了所有的调试语句。您可以通过运行以下命令来查看它们,

    $ tail -f /var/log/system.log | grep 'Debug:'
    

    我根据您似乎使用的惯例创建了一个虚拟文件来测试它,并在我密切关注调试语句的日志时简单地进行复制/粘贴。

    PS:如果删除on adding folder items行并在顶部插入以下内容

    set these_items to (choose file) as list
    

    您可以在Applescript编辑器中运行和调试代码。使用log进行调试非常繁琐......

    所以这是代码:

    property nameList : {"Arrow"} -- the start of the fileName to check
    property destFoldersPath : {"/Volumes/media/Videos/Serier/Arrow"} -- drag/drop the dest folder in each item of this list
    
    on adding folder items to masterFolder after receiving these_items
    
        log "Debug: New file detected!"
    
        tell application "Finder"
            activate
            repeat with tFile in these_items
                set fName to (get name of tFile)
                #log "Debug: Item name: " & fName
    
                set destFolder to my check_start_of_Name(fName)
                log "Debug: Destiantion Folder: " & destFolder
    
                if destFolder is not "" then -- this file name is in the first list
                     move tFile to destFolder with replacing --move this file
                end if
            end repeat
        end tell
    end adding folder items to
    
    on check_start_of_Name(t)
        set tc to count nameList
        #log "Debug: List count is: " & tc
        log "Debug: Item name: " & t
    
        repeat with i from 1 to tc
            if t starts with (item i of nameList) then
                log "Debug: Checking for TV Series: " & item i of nameList
    
                set seasonPath to my check_season_of_Name(t)
                #log "Debug: Season Path: " & seasonPath
    
                set outputPath to (POSIX file (item i of destFoldersPath))
                log "Debug: Output path is: " & outputPath
    
                #set completePath to outputPath & seasonPath as alias
                # Can't create alias, a dymanic object, of something that may not exist... so...
    
                set completePath to (outputPath as text) & ":" & seasonPath & ":"
                log "Debug: Complete path is: " & completePath
    
                # Lets use the POSIX path to test for the existance of the folder...
                set posixPath to (POSIX path of (completePath as text))
                log "Debug: Posix path is: " & posixPath
    
                if FinderItemNotExists(posixPath) then
                    log "Debug: Seacon folder not found, creating..."
                    tell application "Finder"
                        make new folder at outputPath with properties {name:seasonPath}
                    end tell
                else
                    log "Debug: Folder exists."
                end if
    
                # Shouldn't we return the new folder we just created or deduced?
                #return outputPath as alias -- return the dest folder
    
                # So I think you mean...
                return completePath as alias
            end if
        end repeat
        log "Debug: Name not found in list!"
        return "" -- this name is not in the list
    end check_start_of_Name
    
    on check_season_of_Name(t)
        log "Debug: Checking season in filename: " & t
        repeat with s from 1 to 30
            set SW to s
            if s is less than 10 then set SW to "0" & s
            set seasonName to "Season " & SW
            #log seasonName
            if seasonName is in t then
                log "Debug: Found season: " & seasonName
                return seasonName
            end if
        end repeat
        log "Debug: Season name not found!"
    end check_season_of_Name
    
    on FinderItemNotExists(thePath)
        try
            do shell script "test -d '" & thePath & "'"
            return false
        on error
            return true
        end try
    end FinderItemNotExists