0

I have an applescript (below) which is used to clean the name of a tv show episode, and move the episode to its right folder based on that. I use hazel to activate the script with the right file. In applescript editor, the script seems fine, but Hazel gives me an error on line 12. Here's the script:

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

Any ideas?

EDIT:

So, now I have this:

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

The only part that doesn't work is the part which make the folder if it doesn't exists... Any ideas?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Daniel Ruhman
  • 123
  • 2
  • 13

1 Answers1

3

It would be easier to use a shell script, but try something like this:

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
  • Embedded scripts can't contain handlers
  • External scripts have to use hazelProcessFile
  • theFile is an alias so you have to coerce it to text
  • You have to tell Finder to move files and convert paths to file objects with POSIX file
  • Restoring text item delimiters is not necessary as far as I know

Edit: here is a shell script version:

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

Edit 2: this would for example move here.comes.honey.boo.boo.s01e04.720p-killers.mkv to a folder named 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

The titlecase gem can be installed with sudo gem install titlecase.

Community
  • 1
  • 1
Lri
  • 26,768
  • 8
  • 84
  • 82
  • Hazel still gives me an error on the first line, it says "Expected “end” but found “on”." Any ideas? – Daniel Ruhman May 12 '13 at 14:53
  • It doesn't work as an embedded script because of the handlers. You have to save it as an external script. – Lri May 12 '13 at 15:12
  • +1 for Lauri. The Run AppleScript action in hazel includes the embedded script within a hidden handler. Since you can't include a handler within a handler, you need to point hazel towards an external script to run. – adayzdone May 12 '13 at 20:08
  • I've tried setting it up as an external script, but sometimes it gives me an error when its trying to run the applescript, other times it doesnt give any errors, but nothing happens! – Daniel Ruhman May 12 '13 at 20:42
  • @DanielRuhman Check the log or try using `say newbase` or commenting out the lines for renaming files or something. I edited the answer to add a shell script version. – Lri May 13 '13 at 06:50
  • Thanks! @lauri, it works (the shell script), but not exactly as I wanted: I want a string with the clean name of the file (Not renaming the file), and use this string to move the file (with its original name) to the right folder (/Volumes/LaCie/Midia/Series/string). I also want the folder to be created if it doesnt exist. Any ideas? Thanks for the help! – Daniel Ruhman May 13 '13 at 16:57
  • @lauri, I update my answer with the updated script, can you please take a look? Thanks! – Daniel Ruhman May 19 '13 at 20:01