Move files based on name - TVShows.app

Complete AS newbie here, but I’m trying to learn :expressionless:

So I am using the application named TVShows, which is amazing btw. It is downloading shows to a directory conveniently named “Tv Shows”. I have manually created sub-folders for each individual show, e.g.:

Tv Shows
-30 Rock
-Chuck
-The Office

The downloads are kept in my “Downloads” folder until it has completed, my torrent client Transmission, then moves it to the “Tv Shows” root. I need a script or automator action that will move the files into the appropriate folder based off the file name. If it’s a new show, i don’t mind created a new folder for it, so that’s not an issue.

The files almost ALWAYS follow this format: showname.SE01E01.xxxx.avi or two.wordshowname.SE01.E01.xxx.avi (xxx = extraneous naming convention that can change, but doesn’t matter), e.g: Chuck.S02E01.HDTV.XVID.avi. I don’t need it to filter by season or episode numbers, just the Name of the show.

I would like this to run as soon as the directory “Tv Shows” has been changed, which is why I originally looked at Folder actions. But i can’t figure out a way to do an action without doing a separate one for each show name. Does this make sense?

Again, I am in early learning stage, but am excited and willing to learn. I haven’t really found anything via Google that suggests this request, but maybe I missed something.

Thanks for any and all help or recommendations!

This looks very familiar. :wink:
http://bbs.macscripter.net/viewtopic.php?id=27600

Sheesh! How in the hell did I miss that?

That sounds exactly like what I need. Now to figure out how to edit to my needs…

This is going to be a long night!:confused:

Thanks.

Ok. I’m super close. I’ve modified your script mentioned in the previous thread, and it MOSTLY works.


tell application "Finder"
	set thisfolder to (choose folder) as alias
	set mylist to (every file in thisfolder whose name is not "thenames.scpt") as alias list
	set thisfolder to thisfolder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	repeat with eachfile in mylist
		set n to name of eachfile
		set thefirst to text item 1 of n
		set themid to text item 2 of n
		set thelast to text item -2 of n
		if themid = thelast then
			set thefoldername to thefirst
		else
			set thefoldername to thefirst & " " & themid
		end if
		try
			set newfolder to thisfolder & thefoldername as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
		end try
		move eachfile to thisfolder & thefoldername as alias
	end repeat
end tell

The problem I’m having is that with longer file names, such as man.vs.wild.SE01E03.avi, or the.sarah.silverman.program.SE01E01.avi, it creates new folders called “Man Vs” or “The Sarah” instead of moving it into the existing folders for said shows.

I’m actually ok with it NOT creating new folders, as long as it will move the files into the existing folders according to filename, i’m just not sure what to add/remove to do that. Help, please!

This is a little closer to what you want, but if there are numbers in the names it will lump them together. Example:
Name.jpg, Name2.jpg, and Name3.some.more.text.A01B02.other.stuff.jpg will all end up in the same folder.

tell application "Finder"
	set thisfolder to (choose folder) as alias
	set mylist to (every file in thisfolder) as alias list
	set thisfolder to thisfolder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	repeat with eachfile in mylist
		set n to name of eachfile
		set thefirst to text item 1 of n
		set themid to text item 2 of n
		set thelast to text item -2 of n
		set thecommand to "echo " & n & " | perl -pe 's/[0-9]/\\n/g'"
		set thefoldername to paragraph 1 of (do shell script thecommand)
		set wdcount to count of words in thefoldername
		if wdcount > 1 then
			set thefoldername to words 1 thru -2 of thefoldername as text
		else
			set thefoldername to word 1 of thefoldername
		end if
		try
			set newfolder to thisfolder & thefoldername as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
		end try
		move eachfile to thisfolder & thefoldername as alias
	end repeat
	set AppleScript's text item delimiters to tid
end tell

Without modifying it, I get AS error ‘Can’t Get word 1 of “”.’

I must be missing something…

I can’t duplicate the problem. Is it happening on a specific file name?

Here’s a new version with some of the old, now extraneous parts removed:

tell application "Finder"
	set thisfolder to (choose folder) as alias
	set mylist to (every file in thisfolder) as alias list
	set thisfolder to thisfolder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	repeat with eachfile in mylist
		set n to name of eachfile
		set thecommand to "echo " & n & " | perl -pe 's/[0-9]/\\n/g'"
		set thefoldername to paragraph 1 of (do shell script thecommand)
		set wdcount to count of words in thefoldername
		if wdcount > 1 then
			set thefoldername to words 1 thru -2 of thefoldername as text
		else
			set thefoldername to word 1 of thefoldername
		end if
		try
			set newfolder to thisfolder & thefoldername as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
		end try
		move eachfile to thisfolder & thefoldername as alias
	end repeat
	set AppleScript's text item delimiters to tid
end tell

oh, that seemed to be from 30.rock.e01S01.avi
Removing that file and folder allowed the script to go through to completion, but it’s putting “.” in the folder name, e.g “American.Dad” instead of “American Dad” as it’s currently set up. So it’s creating a new folder rather than placing it in the existing.

Also, i receive an error when testing with 90210 (it’s for my wife, i swear!), thinking it was because of the numerical name, but if gave the error: "Can’t Make alias “insert path to file here” into type <>.

What do you make of that?

Hadn’t planned on file names starting with numbers…

This should do it.

tell application "Finder"
	set thisfolder to (choose folder) as alias
	set mylist to (every file in thisfolder) as alias list
	set thisfolder to thisfolder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	repeat with eachfile in mylist
		set n to name of eachfile
		set w1 to text item 1 of n
		set thecommand to "echo " & n & " | perl -pe 's/[0-9]/\\n/g'"
		set thefoldername to paragraph 1 of (do shell script thecommand)
		set wdcount to count of words in thefoldername
		if wdcount > 1 then
			set thefoldername to words 1 thru -2 of thefoldername as text
			set thefoldername to (do shell script "echo " & thefoldername & " | perl -pe 's/\\./ /g'") as text
			
		else
			if thefoldername = "" then
				set thefoldername to w1
			else
				set thefoldername to word 1 of thefoldername
			end if
		end if
		try
			set newfolder to thisfolder & thefoldername as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
		end try
		move eachfile to thisfolder & thefoldername as alias
	end repeat
	set AppleScript's text item delimiters to tid
end tell

You, my friend, are amazing!

It could not be better than what you’ve just provided to me. Outstanding. Thank you so much for you help and guidance.
I’m now trying to define “thisfolder”, but am foggy on the naming convention for applescript. what needs to separate the folders? “:” or “/”?
Do enclose the entire path in "s?

SO CLOSE!!!

This will show you the exact path of any folder you choose:

tell application "Finder"
	set thisfolder to choose folder
	display dialog thisfolder as text
end tell

Thanks again. I had just missed the final “:” in my path, but now it’s working fine.

Do you have any recommendations to have that script run automatically when a new file is dumped into it?

As of right now, i just created an Automator workflow that just runs that script, then saved as a plug-in/folder action. ANy better way to do this?

You could make it a folder action script. They go in the folder with the posix path:
‘/Library/Scripts/Folder Action Scripts’

I haven’t tested this, but it should be close:

on adding folder items to this_folder after receiving mylist
tell application "Finder"
	set thisfolder to this_folder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	repeat with eachfile in mylist
		set n to name of eachfile
		set w1 to text item 1 of n
		set thecommand to "echo " & n & " | perl -pe 's/[0-9]/\\n/g'"
		set thefoldername to paragraph 1 of (do shell script thecommand)
		set wdcount to count of words in thefoldername
		if wdcount > 1 then
			set thefoldername to words 1 thru -2 of thefoldername as text
			set thefoldername to (do shell script "echo " & thefoldername & " | perl -pe 's/\\./ /g'") as text
			
		else
			if thefoldername = "" then
				set thefoldername to w1
			else
				set thefoldername to word 1 of thefoldername
			end if
		end if
		try
			set newfolder to thisfolder & thefoldername as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:thefoldername}
		end try
		move eachfile to thisfolder & thefoldername as alias
	end repeat
	set AppleScript's text item delimiters to tid
end tell
end adding folder items to

That is a thing of beauty! It’s working flawlessly now. Thanks again for the help.
I know I’m not the only one who uses TVShows.app, so hopefully others will find use for it too.
Works even betten when it’s all on a SMB Share streaming directly to XBMC!

I used to use this exact script, with a starting workflow so I could as exceptions with what was passed to it, when I was using leopard. I then foolishly upgraded to snow leopard, and now for some reason it no longer works. I can’t seem to figure out why. I use it in a workflow, so it found the finder items, then filtered them, moved them, then ran the script on those items which were passed through.

Any help is appreciated.

Hi,
I am new to this too…
Took this script, but I dont get it to work correctly. Would be great with some help.

The file for example is: The.Blacklist.S01E13.HDTV.x264-LOL.mp4
The folder it creates is: The.Blacklist.S

So the two problems are: dont remove the dots, and it keeps the S from season I think.

Also to get this working automatically, I dont get to save scripts in the folder action scripts on my mac.

Running 10.9.1 on my mac if that does any changes to my problems above.

Thanks for any help.

Best regards

Dimitris