Help with applescript

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

Moderator: Mr_Noodle

Help with applescript Sat Jan 16, 2016 7:08 pm • by Frank90
I dont quite understand how Hazel handles applescript and cant get this to work:

The script looks for .rar files in a folder and deletes them IF there is a .img or .iso or .mp4 file in the same folder.

Code: Select all
on hazelProcessFile(theFile, inputAttributes)
   
   tell application "System Events"
      set rarfiles to name of theFile whose name ends with ".rar"
      set notRarFiles to name of theFile whose name ends with ".iso" or name ends with ".img" or name ends with ".mp4"
      set i to 0
      repeat with aName in notRarFiles
         set i to i + 1
         set item i of notRarFiles to text 1 thru -5 of aName
      end repeat
      repeat with aRarFile in rarfiles
         if text 1 thru -5 of aRarFile is in notRarFiles then delete theFile aRarFile
      end repeat
   end tell
   
end hazelProcessFile



Here is the original applescript

Code: Select all
set theFolder to ((choose folder) as text)

tell application "System Events" to tell folder theFolder
   set rarfiles to name of files whose name ends with ".rar"
   set notRarFiles to name of files whose name ends with ".iso" or name ends with ".img" or name ends with ".mp4"
   set i to 0
   repeat with aName in notRarFiles
      set i to i + 1
      set item i of notRarFiles to text 1 thru -5 of aName
   end repeat
   repeat with aRarFile in rarfiles
      if text 1 thru -5 of aRarFile is in notRarFiles then delete file aRarFile
   end repeat
end tell
Frank90
 
Posts: 6
Joined: Sat Jan 16, 2016 5:28 am

Re: Help with applescript Mon Jan 18, 2016 12:12 pm • by Mr_Noodle
Have you tried running the script outside of Hazel? If it doesn't work there then you need to debug it to see what's going wrong. I suggest outputting the values of the different variables at each step.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Help with applescript Tue Jan 19, 2016 12:29 am • by Frank90
Yes it works fine outside Hazel. I just want to make sure i use correct parameters for Hazel here.

Anyone know of more documentation about scripts and Hazel in general (more than the help text inside Hazel)?
Frank90
 
Posts: 6
Joined: Sat Jan 16, 2016 5:28 am

Re: Help with applescript Tue Jan 19, 2016 10:14 am • by Mr_Noodle
AppleScript is a pretty big topic so you'll have to google around for different resources. Again, though, I recommend putting in statements to display the values of the different variables at different points in the script. Right now, the only obvious thing that I would focus on is that maybe the script assumes everything is in a certain directory which you cannot assume when the script is run by Hazel.
Mr_Noodle
Site Admin
 
Posts: 11872
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support