2013-05-11 83 views
0

因此,我有一個applescript(波紋管),用於清理電視節目的名稱,並將該節目移至其正確的文件夾。我使用淡褐色激活與正確的文件的腳本。在蘋果腳本編輯器中,腳本似乎很好,但Hazel在第12行給了我一個錯誤。下面是腳本:嘗試移動榛色文件時出現Applescript錯誤

set theSample to theFile 
set a to RemoveListFromString(theSample, {"720p", "HDTV", "x264", "IMMERSE", "-", "E01"}) 
set b to RemoveListFromString(a, {"..."}) 
set c to RemoveListFromString(b, {".."}) 
set d to replaceString(c, "S0", "Season ") 
set e to replaceString(d, ".", " ") 
if e contains "Season" then 
    move theFile to "/Volumes/LaCie/Midia/Series/" & e 
end if 
if e does not contain "Season" then 
    move theFile to "/Volumes/LaCie/Midia/Filmes" 
end if 
on RemoveFromString(theText, CharOrString) 
    -- ljr (http://applescript.bratis-lover.net/library/string/) 
    local ASTID, theText, CharOrString, lst 
    set ASTID to AppleScript's text item delimiters 
    try 
     considering case 
      if theText does not contain CharOrString then ¬ 
       return theText 
      set AppleScript's text item delimiters to CharOrString 
      set lst to theText's text items 
     end considering 
     set AppleScript's text item delimiters to ASTID 
     return lst as text 
    on error eMsg number eNum 
     set AppleScript's text item delimiters to ASTID 
     error "Can't RemoveFromString: " & eMsg number eNum 
    end try 
end RemoveFromString 

on RemoveListFromString(theText, listOfCharsOrStrings) 
    -- ljr (http://applescript.bratis-lover.net/library/string/) 
    local ASTID, theText, CharOrString, lst 
    try 
     script k 
      property l : listOfCharsOrStrings 
     end script 
     set len to count k's l 
     repeat with i from 1 to len 
      set cur_ to k's l's item i 
      set theText to my RemoveFromString(theText, cur_) 
     end repeat 
     return theText 
    on error eMsg number eNum 
     error "Can't RemoveListFromString: " & eMsg number eNum 
    end try 
end RemoveListFromString 

-- e.g. replaceString("Hello hello", "hello", "Bye") --> "Hello Bye" 

on replaceString(theText, oldString, newString) 
    -- ljr (http://applescript.bratis-lover.net/library/string/) 
    local ASTID, theText, oldString, newString, lst 
    set ASTID to AppleScript's text item delimiters 
    try 
     considering case 
      set AppleScript's text item delimiters to oldString 
      set lst to every text item of theText 
      set AppleScript's text item delimiters to newString 
      set theText to lst as string 
     end considering 
     set AppleScript's text item delimiters to ASTID 
     return theText 
    on error eMsg number eNum 
     set AppleScript's text item delimiters to ASTID 
     error "Can't replaceString: " & eMsg number eNum 
    end try 
end replaceString 

任何想法? 謝謝!

編輯:

所以,現在我得到這樣的:

on hazelProcessFile(theFile) 
set text item delimiters to ":" 
set filename to last text item of (theFile as text) 
set text item delimiters to "." 
if filename contains "." then 
    set base to text items 1 thru -2 of filename as text 
    set extension to "." & text item -1 of filename 
else 
    set base to filename 
    set extension to "" 
end if 
set text item delimiters to {"720p", "HDTV", "x264", "IMMERSE", "-", "E01", "…", "E02", "EVOLVE"} 
set ti to text items of base 
set text item delimiters to "" 
set newbase to ti as text 
set newbase to Replace(newbase, "S0", "Season ") 
set newbase to Replace(newbase, ".", " ") 
set newbase to Replace(newbase, " ", "") 
set newbase to Replace(newbase, " ", "") 
set folderLocation to "/Volumes/LaCie/Midia/Series/" 
set folderName to newbase as text 
tell application "Finder" 
    if newbase contains "Season" then 
     if not (exists folder (POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text)) then 
      set p to path to "/Volumes/LaCie/Midia/Series/" 
      --make new folder at p with properties {name:newbase} 
      make new folder with properties {name:folderName, location:p} 
     else 
      move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" & newbase as text 
      set name of result to newbase 
     end if 
    else 
     move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/" 
    end if 
end tell 
end hazelProcessFile 

on Replace(input, x, y) 
    set text item delimiters to x 
    set ti to text items of input 
    set text item delimiters to y 
    ti as text 
end Replace 

即不工作是部分至極只有一部分使該文件夾,如果犯規存在,它...任何想法?

+0

在Ruby中編寫這個腳本會容易得多。只是說! – matt 2013-05-12 04:18:22

+0

我不知道紅寶石,所以我不能建立它...... – 2013-05-12 20:50:20

回答

2

這將是更容易使用shell腳本,但嘗試這樣的事:

on hazelProcessFile(theFile) 
    set text item delimiters to ":" 
    set filename to last text item of (theFile as text) 
    set text item delimiters to "." 
    if filename contains "." then 
     set base to text items 1 thru -2 of filename as text 
     set extension to "." & text item -1 of filename 
    else 
     set base to filename 
     set extension to "" 
    end if 
    set text item delimiters to {"720p", "HDTV", "x264", "IMMERSE", "-", "E01", "..."} 
    set ti to text items of base 
    set text item delimiters to "" 
    set newbase to ti as text 
    set newbase to replace(newbase, "S0", "Season ") 
    set newbase to replace(newbase, ".", " ") & extension 
    tell application "Finder" 
     if newbase contains "Season" then 
      move theFile to POSIX file "/Volumes/LaCie/Midia/Series/" 
      set name of result to newbase 
     else 
      move theFile to POSIX file "/Volumes/LaCie/Midia/Filmes/" 
     end if 
    end tell 
end hazelProcessFile 

on replace(input, x, y) 
    set text item delimiters to x 
    set ti to text items of input 
    set text item delimiters to y 
    ti as text 
end replace 
  • 嵌入的腳本不能包含處理器
  • 外部腳本已使用hazelProcessFile
  • theFile是一個別名,所以你必須強制它到文本
  • 你必須告訴Finder移動文件並將路徑轉換爲文件對象POSIX file
  • Restoring text item delimiters is not necessary據我所知

編輯:這裏是一個shell腳本版本:

basename=${1##*/} 
base=${basename%.*} 
[[ $basename = *.* ]] && ext=".${basename##*.}" || ext= 
for x in 720p HDTV x264 IMMERSE - E01 ...; do base=${base//"$x"}; done 
base=${base//S0/Season} 
base=${base//./ } 
if [[ $base = *Season* ]]; then 
    mv "$1" "/Volumes/LaCie/Midia/Series/$base$ext" 
else 
    mv "$1" /Volumes/LaCie/Midia/Movies/ 
fi 

編輯2:這將例如移動here.comes.honey.boo.boo.s01e04.720p-killers.mkv到名爲Here Comes Honey Boo Boo Season 1的文件夾:

d=${1##*/} 
if [[ $d =~ [Ss][0-9][0-9][Ee][0-9][0-9] ]]; then 
    d=$(sed -E 's/[ .][Ss]([0-9][0-9])[Ee][0-9][0-9].*/ Season \1/;s/ Season 0/ Season /;s/\./ /g' <<< "$d") 
    d=$(ruby -r rubygems -e 'require "titlecase"; puts ARGV[0].titlecase' "$d") 
    target="/Volumes/LaCie/Midia/Series/$d" 
    mkdir -p "$target" 
    mv "$1" "$target" 
else 
    mv "$1" "/Volumes/LaCie/Midia/Movies" 
fi 

標題寶石可以與sudo gem install titlecase一起安裝。

+0

Hazel在第一行仍然給我一個錯誤,它說「預期」結束「,但發現」上「。有任何想法嗎? – 2013-05-12 14:53:32

+0

由於處理程序的原因,它不能用作嵌入式腳本。您必須將其保存爲外部腳本。 – user495470 2013-05-12 15:12:53

+0

+1爲勞裏。在hazel中運行AppleScript操作包含隱藏處理程序中的嵌入式腳本。由於您不能在處理程序中包含處理程序,因此您需要將榛樹指向外部腳本運行。 – adayzdone 2013-05-12 20:08:40