Error running workflow when not done manually

Hello. I am new to AppleScript so please be gentle.

I wrote a workflow that downloads an XML file from thetvdb.com and then uses that information to rename and import a movie file with tags into iTunes. It works fine when I run it manually, but there is a problem when I put it in one of the rules for Hazel or try to make it into a folder action. If I use 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 “”
set tempSeriesName to text item 2 of seriesNameGrepResult
set AppleScript’s text item delimiters to “”
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 "” & strippedENum & “" " & xml_file & " | grep -B 19 "” & strippedSNum & "" | grep EpisodeName | sed "s/\'/""/g" ")

set AppleScript’s text item delimiters to “”
set tempepisodeNameGrepResult to text item 2 of episodeNameGrepResult
set AppleScript’s text item delimiters to “”
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 "” & strippedENum & “" " & xml_file & " | grep -B 19 "” & strippedSNum & "" | grep id ")
set AppleScript’s text item delimiters to “”
set tempepidGrepResult to text item 2 of epidGrepResult
set AppleScript’s text item delimiters to “”
set epidResult to text item 1 of tempepidGrepResult

set episodeFullNameGrepResult to do shell script (“grep -C 10 "” & strippedENum & “" " & xml_file & " | grep -B 19 "” & strippedSNum & "" | grep EpisodeName ")
set AppleScript’s text item delimiters to “”
set tempepisodeFullNameGrepResult to text item 2 of episodeFullNameGrepResult
set AppleScript’s text item delimiters to “”
set episodeFullNameResult to text item 1 of tempepisodeFullNameGrepResult

set overviewGrepResult to do shell script (“grep -C 10 "” & strippedENum & “" " & xml_file & " | grep -B 19 "” & strippedSNum & "" | grep Overview ")
set AppleScript’s text item delimiters to “”
set tempOverviewGrepResult to text item 2 of overviewGrepResult
set AppleScript’s text item delimiters to “”
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 “”
set tempactorsGrepResult to text item 2 of actorsGrepResult
set AppleScript’s text item delimiters to “”
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 "” & strippedENum & “" " & xml_file & " | grep -B 19 "” & strippedSNum & "" | grep FirstAired ")
set AppleScript’s text item delimiters to “”
set tempyearGrepResult to text item 2 of yearGrepResult
set AppleScript’s text item delimiters to “”
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 “”
–set posterGrepResult to text item 2 of posterGrepResult
–set AppleScript’s text item delimiters to “”
–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.

Model: iMac
AppleScript: 2.2.1
Browser: Safari 534.52.7
Operating System: Mac OS X (10.7)

On the Hazel web site FAQ, there is this statement:

I’m guessing that’s your problem.

Thank you for the quick response. Hazel is currently monitoring a folder other than the one that the files are created in. As a test I have been dropping the files in the folder that it is supposed to be monitoring and it still complains. I have tried converting this over to folder action and dropping the file in the folder, but it does nothing, so I was hoping someone might be able to tell me if there seems to be something wrong with the code.

I’m stymied – here’s hoping someone else sees the problem.

Or, “cannot make list into alias”.
I suspect the error occurs here:

tell application "Finder"
set this_file to input as alias
-- more code

“input”, in an Automator script, is a list, so correct code would be:

set this_file to (1st item of input) as alias

But if that’s the cause of the failure it should fail as a standalone script as well, I think.
Worth a try, though.

Thank you. That worked for Hazel’s end. It now processes the rule with no grumbling, but I now it isn’t working as it originally did. It is processing every file and not just the show that matches…and the one that matches it imports correctly with the tags, but then it imports it a second time without the tagging section, so it looks like I have to figure that part out. Thank you again for the fix. Hazel is now happy.

The error message also told us that Hazel passes the same item twice. Something between Hazel and the workflow isn’t cooperating. Maybe post this in the AppleScript forum at Noodlesoft.