2014-05-14 48 views
-1

所以我對AppleScript很新穎,但我正在嘗試製作一個腳本來對我的視頻文件進行排序。AppleScript:Finder Sorting

我看起來似乎無法解決這個問題。什麼都沒發生。任何人都有線索?

一個可能的解釋可能是文件夾是我的家庭服務器,而不是在硬盤上?

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 
+0

有太多的「噪音」,這使得它很難理解的問題;嘗試縮小問題範圍並描述特定症狀。 – mklement0

回答

0

我看了一下你的腳本。我不熟悉文件夾操作腳本,所以這是教育。以下是我理解你想從你的代碼來實現 - 請糾正我,如果我錯了:

  1. 你的文件被命名爲這樣的:「{顯示名稱} {賽季XX}等」
  2. 進程名稱以列表名稱開頭的文件忽略其餘部分。
  3. 根據文件名確定季節並創建名爲「Season XX」的文件夾。
  4. 最後它將文件移動到這些文件夾。

所以我開始從一開始就一行一行地修好,就我所知,什麼是不工作。正如你在下面看到的,我已經離開了所有的調試語句。您可以通過運行以下命令看他們,

$ 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