Renaming based on files "where from data"

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

Moderator: Mr_Noodle

Renaming based on files "where from data" Fri Jan 03, 2014 2:55 pm • by waxwing
Looking for a little help on using Hazel to rename some video files I'm downloading for a class I'm in. When I download them for offline playback on my iPad, they are given a generic name like introductory_lesson.mp4. Subsequent downloads get a number tacked on such as (1), (2), etc. Checking Get Info, I see that the Where From data has better data that could be used for naming. For example:

Where from: http://abcdef.somecontentserver.net/les ... ter_01.mp4, http://myschool.edu/introductory_lesson

So, it appears the Mac is changing the file name to the "folder" name of the school's site while the content delivery server has the actual file name.

How might I have Hazel rename the file using the actual filename, "intro_to_business_chapter_01.mp4", in the example above. Can Hazel access and parse that "Where from" data?

Thanks!
waxwing
 
Posts: 6
Joined: Wed Feb 02, 2011 10:20 am

You might be able to access it but it might be tricky to extract that part out. I guess you can try using a match pattern on it and storing the part after the last slash into a custom token. I suggest searching the help on match patterns if you aren't familiar with the feature already.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Renaming based on files "where from data" Sat Jan 04, 2014 9:56 am • by waxwing
I did a bit of noodling on the net and came up with the script that will let me rename files on demand. However, it doesn't run if I try to use it with Hazel.

Code: Select all
-- get Spotlight kMDItemWhereFroms attribute

property getSpecific : true -- show the specific download URL?

-- assume the path that needs to be stripped is consistent do determine length
set aPath to " http://abcdef.somecontentserver.net/lessons/"
set pathLength to (length of aPath) + 1

set theFile to (choose file)
set aFile to quoted form of POSIX path of theFile
set theUrl to (do shell script "mdls -name kMDItemWhereFroms -raw " & aFile)
if theUrl is "(null)" then -- no attribute
   set theUrl to "(URL info not found)"
   set theMessage to ""
else
   if getSpecific then -- get the first item (the download URL)
      set theUrl to paragraph 2 of theUrl
      set here to offset of "\"" in theUrl
      set theUrl to text (here + 1) thru -3 of theUrl -- the download URL
      tell application "Finder"
         try
            set name of file theFile to text pathLength thru -1 of theUrl
         on error e number n
            activate
            display dialog e & " " & n
         end try
      end tell
   else -- get the last item (the page/site URL)
      set theMessage to "page/site "
      set theUrl to paragraph -2 of theUrl
      set here to offset of "\"" in theUrl
      set theUrl to text (here + 1) thru -2 of theUrl
   end if
end if


I believe that Hazel is assuming theFile is the file that is being acted upon so the "set theFile to (choose file)" line can be commented out. Correct? Any idea what else would need to change in order for this to work when called from Hazel? I feel like I've very, very close since it works outside of Hazel. Thanks!
waxwing
 
Posts: 6
Joined: Wed Feb 02, 2011 10:20 am

I'm not sure a script is necessary. Does the script compile properly? If so, then you need to check the logs to see what error came up when you tried to run it.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Renaming based on files "where from data" Tue Jan 07, 2014 10:44 pm • by waxwing
The script does compile. What I posted worked well when run manual, not from Hazel. After some additional web noodling, I noticed several people including the lines
Code: Select all
on hazelProcessFile(theFile)
end hazelProcessFile

at the beginning and end of their code. I added that it seems to be working now. I don't understand what those lines mean but I appear to be in business. This will save me a ton of time. Thanks for the product and the help.
waxwing
 
Posts: 6
Joined: Wed Feb 02, 2011 10:20 am


Return to Support