Error when running Workflow to tag file

Hello. I am new to AppleScript and Hazel so please be gentle.
I wrote a workflow that downloads an XML file from thetvdb.com and then uses that information to rename and tag a file. It works fine when I run it myself, but there is a problem when I put it in one of the rules for Hazel. I get different errors every time the rule matches. Sometimes it even gives me an error and runs it anyways. Looking through my Hazel.log I see errors like the ones below:
2011-12-31 12:35:18.584 hazelfolderwatch[1242] Error executing Automator workflow /Users/Shared/Hazel/Workflows/American Horror Story - Rename and Tag.workflow: Error Domain=com.apple.Automator Code=-212 "The action “Run AppleScript” encountered an error." UserInfo=0x10033e5c0 {NSLocalizedDescription=The action “Run AppleScript” encountered an error., NSLocalizedRecoverySuggestion=Check the actionʼs properties and try running the workflow again., NSLocalizedFailureReason=Can’t make {alias "Macintosh HD:Users:Shared:Users:Temp:American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4", alias "Macintosh HD:Users:Shared:Users:Temp:American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4"} into type alias.}
2011-12-31 12:39:33.000 (null)[0] American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4: Rule American Horror Story matched.
2011-12-31 12:39:41.886 hazelfolderwatch[1264] Error executing Automator workflow /Users/Shared/Hazel/Workflows/American Horror Story - Rename and Tag.workflow: (null)
2011-12-31 12:39:41.889 hazelfolderwatch[1264] [Error] Automator workflow failed: Error executing automator workflow /Users/Shared/Hazel/Workflows/American Horror Story - Rename and Tag.workflow on file /Users/Shared/Users/Temp/American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4.
As I stated earlier the code seems to run correctly when I run it myself. Hazel won't run it if she matches it unless I select to "Run Rules Now", and then it seems 50/50 whether or not it will run. The workflow is (Get Selected Items, Run Applescript, Move file to Trash). This is what the code looks like:
__________________________________________________
on run {input, parameters}
set xml_file to (POSIX path of (path to temporary items)) & "file.xml"
do shell script "curl -L http://www.thetvdb.com/api/1D62F2F90030 ... all/en.xml" & " -o " & xml_file
-- previous line contains series specific feed
repeat with i in input
try
tell application "Finder" to set file type of file i to "MooV"
end try
tell application "Finder"
set this_file to input as alias
--set TheName to name of (this_file as alias)
set posix_this_file to POSIX path of (this_file as alias)
--set parentFolderPath to text 1 thru ((offset of TheName in posix_this_file) - 2) of posix_this_file
-- pull series name out of the xml file
set seriesNameGrepResult to do shell script ("grep SeriesName " & xml_file)
set AppleScript's text item delimiters to "<SeriesName>"
set tempSeriesName to text item 2 of seriesNameGrepResult
set AppleScript's text item delimiters to "</SeriesName>"
set seriesBaseName to text item 1 of tempSeriesName
--deal with season and episode information
set AppleScript's text item delimiters to "."
set episodeString to (text item 4 of posix_this_file)
-- previous line contains series specific search
set AppleScript's text item delimiters to "E"
set episodeNum to text item -1 of episodeString
set tempSeason to text item 1 of episodeString
set AppleScript's text item delimiters to "S"
set seasonNum to text item -1 of tempSeason
--set AppleScript's text item delimiters to "."
--set fileExt to (text item -1 of posix_this_file)
--code for search strings through XML
set strippedENum to do shell script ("expr " & episodeNum & " + 0")
set strippedSNum to do shell script ("expr " & seasonNum & " + 0")
set episodeNameGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep EpisodeName | sed \"s/\\'/\"\"/g\" ")
set AppleScript's text item delimiters to "<EpisodeName>"
set tempepisodeNameGrepResult to text item 2 of episodeNameGrepResult
set AppleScript's text item delimiters to "</EpisodeName>"
set episodeName to text item 1 of tempepisodeNameGrepResult
-- set name of this_file to seriesBaseName & " - " & episodeString & "." & fileExt
-- Rename the file
-- Set Tags for file
set genreGrepResult to do shell script ("grep Genre " & xml_file)
set AppleScript's text item delimiters to "|"
set genreResult to text item 2 of genreGrepResult
set epidGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep id ")
set AppleScript's text item delimiters to "<id>"
set tempepidGrepResult to text item 2 of epidGrepResult
set AppleScript's text item delimiters to "</id>"
set epidResult to text item 1 of tempepidGrepResult
set episodeFullNameGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep EpisodeName ")
set AppleScript's text item delimiters to "<EpisodeName>"
set tempepisodeFullNameGrepResult to text item 2 of episodeFullNameGrepResult
set AppleScript's text item delimiters to "</EpisodeName>"
set episodeFullNameResult to text item 1 of tempepisodeFullNameGrepResult
set overviewGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep Overview ")
set AppleScript's text item delimiters to "<Overview>"
set tempOverviewGrepResult to text item 2 of overviewGrepResult
set AppleScript's text item delimiters to "</Overview>"
set overviewResult to text item 1 of tempOverviewGrepResult
set actorsGrepResult to do shell script ("grep Actors " & xml_file)
set AppleScript's text item delimiters to "<Actors>"
set tempactorsGrepResult to text item 2 of actorsGrepResult
set AppleScript's text item delimiters to "</Actors>"
set tempActorsResult_1 to text item 1 of tempactorsGrepResult
set tempActorsResult_2 to do shell script ("echo \"" & tempActorsResult_1 & "\" | sed -e \"s/.//\"")
set tempActorsResult_3 to do shell script ("echo \"" & tempActorsResult_2 & "\" | sed -e \"s/\\(.*\\)./\\1/\"")
set actorsResult to do shell script ("echo \"" & tempActorsResult_3 & "\" | sed \"s/\\|/\",\"/g\"")
set yearGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep FirstAired ")
set AppleScript's text item delimiters to "<FirstAired>"
set tempyearGrepResult to text item 2 of yearGrepResult
set AppleScript's text item delimiters to "</FirstAired>"
set tempyearResult to text item 1 of tempyearGrepResult
set AppleScript's text item delimiters to "-"
set yearResult to text item 1 of tempyearResult
--set posterGrepResult to do shell script ("grep Poster")
--set AppleScript's text item delimiters to "<Poster>"
--set posterGrepResult to text item 2 of posterGrepResult
--set AppleScript's text item delimiters to "</Poster>"
--set tempPosterResult to text item 1 of tempposterGrepResult
--set posterResult to "http://www.theTVDB.com/banners/" & tempPosterResult
--display dialog posterResult
tell application "iTunes"
set newAddition to (add (i as alias))
--sets tv show tags and imports into iTunes
tell newAddition to set video kind to TV show
tell newAddition to set show to seriesBaseName
tell newAddition to set season number to strippedSNum
tell newAddition to set episode number to strippedENum
tell newAddition to set episode ID to epidResult
tell newAddition to set genre to genreResult
tell newAddition to set name to episodeFullNameResult
tell newAddition to set description to overviewResult
tell newAddition to set artist to actorsResult
tell newAddition to set year to yearResult
tell newAddition to set track number to strippedENum
--tell newAddition to set artwork to posterResult
end tell
end tell
end repeat
return input
end run
__________________________________________________
Parts of the file were commented out because I decided to let iTunes rename the file based on the tags when it imports it.
Any help anyone can offer would be greatly appreciated. Please let me know if you need more information or clarification. Thank you in advance.
I wrote a workflow that downloads an XML file from thetvdb.com and then uses that information to rename and tag a file. It works fine when I run it myself, but there is a problem when I put it in one of the rules for Hazel. I get different errors every time the rule matches. Sometimes it even gives me an error and runs it anyways. Looking through my Hazel.log I see errors like the ones below:
2011-12-31 12:35:18.584 hazelfolderwatch[1242] Error executing Automator workflow /Users/Shared/Hazel/Workflows/American Horror Story - Rename and Tag.workflow: Error Domain=com.apple.Automator Code=-212 "The action “Run AppleScript” encountered an error." UserInfo=0x10033e5c0 {NSLocalizedDescription=The action “Run AppleScript” encountered an error., NSLocalizedRecoverySuggestion=Check the actionʼs properties and try running the workflow again., NSLocalizedFailureReason=Can’t make {alias "Macintosh HD:Users:Shared:Users:Temp:American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4", alias "Macintosh HD:Users:Shared:Users:Temp:American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4"} into type alias.}
2011-12-31 12:39:33.000 (null)[0] American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4: Rule American Horror Story matched.
2011-12-31 12:39:41.886 hazelfolderwatch[1264] Error executing Automator workflow /Users/Shared/Hazel/Workflows/American Horror Story - Rename and Tag.workflow: (null)
2011-12-31 12:39:41.889 hazelfolderwatch[1264] [Error] Automator workflow failed: Error executing automator workflow /Users/Shared/Hazel/Workflows/American Horror Story - Rename and Tag.workflow on file /Users/Shared/Users/Temp/American.Horror.Story.S01E04.HDTV.XviD-ASAP.[VTV].mp4.
As I stated earlier the code seems to run correctly when I run it myself. Hazel won't run it if she matches it unless I select to "Run Rules Now", and then it seems 50/50 whether or not it will run. The workflow is (Get Selected Items, Run Applescript, Move file to Trash). This is what the code looks like:
__________________________________________________
on run {input, parameters}
set xml_file to (POSIX path of (path to temporary items)) & "file.xml"
do shell script "curl -L http://www.thetvdb.com/api/1D62F2F90030 ... all/en.xml" & " -o " & xml_file
-- previous line contains series specific feed
repeat with i in input
try
tell application "Finder" to set file type of file i to "MooV"
end try
tell application "Finder"
set this_file to input as alias
--set TheName to name of (this_file as alias)
set posix_this_file to POSIX path of (this_file as alias)
--set parentFolderPath to text 1 thru ((offset of TheName in posix_this_file) - 2) of posix_this_file
-- pull series name out of the xml file
set seriesNameGrepResult to do shell script ("grep SeriesName " & xml_file)
set AppleScript's text item delimiters to "<SeriesName>"
set tempSeriesName to text item 2 of seriesNameGrepResult
set AppleScript's text item delimiters to "</SeriesName>"
set seriesBaseName to text item 1 of tempSeriesName
--deal with season and episode information
set AppleScript's text item delimiters to "."
set episodeString to (text item 4 of posix_this_file)
-- previous line contains series specific search
set AppleScript's text item delimiters to "E"
set episodeNum to text item -1 of episodeString
set tempSeason to text item 1 of episodeString
set AppleScript's text item delimiters to "S"
set seasonNum to text item -1 of tempSeason
--set AppleScript's text item delimiters to "."
--set fileExt to (text item -1 of posix_this_file)
--code for search strings through XML
set strippedENum to do shell script ("expr " & episodeNum & " + 0")
set strippedSNum to do shell script ("expr " & seasonNum & " + 0")
set episodeNameGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep EpisodeName | sed \"s/\\'/\"\"/g\" ")
set AppleScript's text item delimiters to "<EpisodeName>"
set tempepisodeNameGrepResult to text item 2 of episodeNameGrepResult
set AppleScript's text item delimiters to "</EpisodeName>"
set episodeName to text item 1 of tempepisodeNameGrepResult
-- set name of this_file to seriesBaseName & " - " & episodeString & "." & fileExt
-- Rename the file
-- Set Tags for file
set genreGrepResult to do shell script ("grep Genre " & xml_file)
set AppleScript's text item delimiters to "|"
set genreResult to text item 2 of genreGrepResult
set epidGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep id ")
set AppleScript's text item delimiters to "<id>"
set tempepidGrepResult to text item 2 of epidGrepResult
set AppleScript's text item delimiters to "</id>"
set epidResult to text item 1 of tempepidGrepResult
set episodeFullNameGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep EpisodeName ")
set AppleScript's text item delimiters to "<EpisodeName>"
set tempepisodeFullNameGrepResult to text item 2 of episodeFullNameGrepResult
set AppleScript's text item delimiters to "</EpisodeName>"
set episodeFullNameResult to text item 1 of tempepisodeFullNameGrepResult
set overviewGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep Overview ")
set AppleScript's text item delimiters to "<Overview>"
set tempOverviewGrepResult to text item 2 of overviewGrepResult
set AppleScript's text item delimiters to "</Overview>"
set overviewResult to text item 1 of tempOverviewGrepResult
set actorsGrepResult to do shell script ("grep Actors " & xml_file)
set AppleScript's text item delimiters to "<Actors>"
set tempactorsGrepResult to text item 2 of actorsGrepResult
set AppleScript's text item delimiters to "</Actors>"
set tempActorsResult_1 to text item 1 of tempactorsGrepResult
set tempActorsResult_2 to do shell script ("echo \"" & tempActorsResult_1 & "\" | sed -e \"s/.//\"")
set tempActorsResult_3 to do shell script ("echo \"" & tempActorsResult_2 & "\" | sed -e \"s/\\(.*\\)./\\1/\"")
set actorsResult to do shell script ("echo \"" & tempActorsResult_3 & "\" | sed \"s/\\|/\",\"/g\"")
set yearGrepResult to do shell script ("grep -C 10 \"<EpisodeNumber>" & strippedENum & "\" " & xml_file & " | grep -B 19 \"<SeasonNumber>" & strippedSNum & "\" | grep FirstAired ")
set AppleScript's text item delimiters to "<FirstAired>"
set tempyearGrepResult to text item 2 of yearGrepResult
set AppleScript's text item delimiters to "</FirstAired>"
set tempyearResult to text item 1 of tempyearGrepResult
set AppleScript's text item delimiters to "-"
set yearResult to text item 1 of tempyearResult
--set posterGrepResult to do shell script ("grep Poster")
--set AppleScript's text item delimiters to "<Poster>"
--set posterGrepResult to text item 2 of posterGrepResult
--set AppleScript's text item delimiters to "</Poster>"
--set tempPosterResult to text item 1 of tempposterGrepResult
--set posterResult to "http://www.theTVDB.com/banners/" & tempPosterResult
--display dialog posterResult
tell application "iTunes"
set newAddition to (add (i as alias))
--sets tv show tags and imports into iTunes
tell newAddition to set video kind to TV show
tell newAddition to set show to seriesBaseName
tell newAddition to set season number to strippedSNum
tell newAddition to set episode number to strippedENum
tell newAddition to set episode ID to epidResult
tell newAddition to set genre to genreResult
tell newAddition to set name to episodeFullNameResult
tell newAddition to set description to overviewResult
tell newAddition to set artist to actorsResult
tell newAddition to set year to yearResult
tell newAddition to set track number to strippedENum
--tell newAddition to set artwork to posterResult
end tell
end tell
end repeat
return input
end run
__________________________________________________
Parts of the file were commented out because I decided to let iTunes rename the file based on the tags when it imports it.
Any help anyone can offer would be greatly appreciated. Please let me know if you need more information or clarification. Thank you in advance.