Applescript error while trying to move files with hazel

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

So,I have an applescript (bellow) wich 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:

Code: Select all
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?
Thanks!
ruhman
 
Posts: 6
Joined: Sun Nov 04, 2012 1:31 pm

Reading the help file regarding AppleScript will help. Here the problem is that you have internal functions. However, I must ask why you are using a script to do this. Scripts like this are what Hazel is designed to replace.

I suggest that you look into pattern matching - that is substantially better than the suggestion below. However, since I do not know the format of your file names, you can duplicate your script in Hazel like this:

Rule 1: Clean File Name
Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     if (any) of the following conditions are met for (the file or folder being processed):
          name contains ...
          name contains ..
          name contains 720p
          name contains HDTV
          name contains x264
          name contains IMMERSE
          name contains -
          name contains E01
     kind is movie

Do the following to the matched file or folder:
     rename file with (name)(extension)


Note: "Replace" options within name to eliminate the above from any filename containing them.

Rule 2: Move Seasons to Season
Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     name contains Season
     kind is movie

Do the following to the matched file or folder:
     move to /Volumes/LaCie/Midia/Series/


Rule 3: Move All Else to Films
Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     any file
     kind is movie

Do the following to the matched file or folder:
     move to /Volumes/LaCie/Midia/Filmes/
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Yes, I know that, I used to do this as you described, but let me explain better: The files are TV Shows episodes, and i want to clean their name from all the crap, and move them to the right folder for each different show. I used to replace the text as you mentioned it, and just sort it into a subfolder like that, the problem is that when I replace the "." with " "(spaces), there are always 3 or 2 spaces in the end of the folder name. If the number of dots changes, the show gets sorted in a different folder, with the same name as the right one (Show Name Season Number), but with a different amount of spaces in the end. So, the sort into subfolder method doesnt work, thats why I'm building the script so that it cleans the name (and the ending string contains the name of the right folder) and moves it to the right folder. Got it?

Thanks!
ruhman
 
Posts: 6
Joined: Sun Nov 04, 2012 1:31 pm

Post some example file names.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

As mentioned before, you can't define handlers within an embedded script. Use an external script instead of you absolutely need to use handlers.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Example file name: Anger.Management.S02E01.720p.HDTV.x264-IMMERSE.mkv

Mister_Noodle, I've tried using an external script, but it gives me an error...
ruhman
 
Posts: 6
Joined: Sun Nov 04, 2012 1:31 pm

So, why don't you do this?

Anger.Management.S02E01.720p.HDTV.x264-IMMERSE.mkv

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     kind is movie
     name matches .S(12).
     name matches (Series Name •).S(Season Number •)E(Episode Number •)(...)

Do the following to the matched file or folder:
     rename (Series Name •).Season (Season Number •) E(Episode Number •)
     move to Series


Where:

(Series Name •) = (...) anything
Season Number •) = (12) number
(Episode Number •) = (12) number

And "Series Name" has a text replace of "." with " "

Then a second rule:

Code: Select all
if (all) of the following conditions are met for (the file or folder being processed):
     kind is movie
     name does not match .S(12).


Do the following to the matched file or folder:
     move to Filmes
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Cause then I would have to create one rule for every tv show...
ruhman
 
Posts: 6
Joined: Sun Nov 04, 2012 1:31 pm

No... you don't. That's the point. These are tokens. Look up custom matching.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado

Why do you put something about the episode?
What is S(12)?
ruhman
 
Posts: 6
Joined: Sun Nov 04, 2012 1:31 pm

ruhman wrote:Why do you put something about the episode?

So it'll be in the renamed filename, or isn't that what you mean?

What is S(12)?

Have you read about custom matching, as a_freyer suggested?
sjk
 
Posts: 332
Joined: Thu Aug 02, 2007 5:43 pm
Location: Eugene

This is substantially copied from my tutorial on custom matching.

There have been a few folks asking how to sort files into alphabetical folders, so I thought I would post a step by step tutorial. A lot of people thing that you would have to create 26 separate rules for this, but not with Hazel. We are going to use custom tokens to help us Sort "Time Machine.pdf" into the "T" folder.

This is how the rule will look when we are done:

Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
    Name matches (FIRST LETTER •)(REST OF TITLE •)

Do the following to the matched file or folder
    Move to /Directory Containing All Alphanumerical Folders/
    Sort into subfolder with pattern (FIRST LETTER •)



Think of a token as a piece of a file name. You can make the piece a letter, a number, a word, a digit, a symbol or any combination thereof. In Hazel there are two kinds of tokens: (1) generic and (2) custom. A generic token is what you see in the default matching floating window - letter, digit, word, etc. - and they can be used to build a file name pattern matching template. However, a GENERIC TOKEN CANNOT be used in the action section of your rules. They are generic - they only are used to match a file. On the other hand, a CUSTOM TOKEN CAN be used in the action section of your rules. This is because a custom token has a name.

In this forum, myself and others will generally note a custom token with a • mark (OPT+8 on a mac).

Step 1: Create a Rule Using Two Custom Matching Tokens


As shown above, we first create a rule and add a name matches condition. Next, we are going to add two custom tokens.

Click within the input box after Name Matches and a floating window appears. Drag custom (•) from the bottom right hand corner into that box. Another floating window appears. From this floating window we add the title and the elements of the custom token. For our demo, we have two. When the first one is created, drag a second custom token to the right of the first.

Custom Token 1:
Name - First Letter
Elements - Single Letter (a)

Custom Token 2:
Name - Rest of Word Letter
Elements - Anything (...)


Step 2: Add Sort into Subfolder Condition after Moving to Parent Directory (Optional)


Hazel executes rule actions sequentially. So, in our example above we can move the file into a parent directory and still execute subsequent actions even though the file no longer exists in the monitor directory.

Now, add a sort into subfolder rule and you will see that both custom tokens appear in your list of possible tokens. Without adding any slashes, drag (First Letter •) into the input dialog.

Hazel will create folders as necessary.

Now, for your particular situation, create the custom tokens as I have indicated above.
a_freyer
 
Posts: 631
Joined: Tue Sep 30, 2008 9:21 am
Location: Colorado


Return to Support