Unarchive WITHOUT deleting original archive

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

Moderator: Mr_Noodle

Unarchive WITHOUT deleting original archive Wed May 02, 2012 10:41 am • by Ergohack
I would like to use the "Unarchive" action without it deleting/trashing the original archive.
This is so that I can easily perform actions on the unarchived content, (ie. move to enclosing folder, set label Red)
while still preserving the original archive for later use, (ie. seeding a torrent, keeping the archive since its smaller and I only needed the contents for temporary processing.)
I could just tell Hazel to open the archive with an external program, but this makes it difficult to easily process the resulting files.

Does anyone know how to prevent the "Unarchive" action from trashing the originals?
Or, is there a way I can mimic the action with my desired modifications via Applescript?


*EDIT——————

Just in case this thread goes unanswered, I'll post my own findings in case someone else searches for this.
After some research of my own I came up with this applescript which unrars archives without deleting the original content, then moves the product out of up into the parent directory and sets some colour labels.

Code: Select all
set temp to "______folder_name_unlikely_to_already_be_present_in_this_directory_here___this_should_work__"
set extensionList to {"mp4", "mkv", "avi", "ts"}
tell application "Finder"
   set posix_parent_dir to POSIX path of (container of (item theFile) as string)
   set posix_grandparent_dir to POSIX path of (container of (item (posix_parent_dir as POSIX file)) as string)
   do shell script "mkdir " & quoted form of (posix_parent_dir & temp)
   do shell script "____insert_path_to_unrar_here______ e " & quoted form of (POSIX path of (item theFile as string)) & " " & quoted form of (posix_parent_dir & temp)
   set extractedFiles to every file of folder (posix_parent_dir & temp & "/" as POSIX file)
   repeat with i from 1 to the count of extractedFiles
      if (the name extension of the (item i of extractedFiles) is in the extensionList) then
         set label index of (item i of extractedFiles) to 2 -- red
         do shell script "mv " & quoted form of (POSIX path of ((item i of extractedFiles) as string)) & " " & quoted form of posix_grandparent_dir
      else
         do shell script "rm -rf " & quoted form of (POSIX path of ((item i of extractedFiles) as string))
      end if
   end repeat
   do shell script "rm -rf " & quoted form of (posix_parent_dir & temp)
   -- replace '2' above with the number for the color you'd like to use:
   -- 0=none, 1=orange, 2=red, 3=yellow, 4=blue, 5=purple, 6=green, 7=grey
   set label index of (item (posix_parent_dir as POSIX file)) to 7
end tell


* note that you will need to point the script to an unrar executable; if you installed it from macports the path should be /opt/local/bin/unrar.

* I found that I needed to use the applescript for any subsequent actions on the product of the unarchiving process; for some reason I couldn't get the hazelSwitchFile variable to work. This is probably since I'm fairly new to applescript and am unsure of the proper formatting for this variable's contents.
Ergohack
 
Posts: 2
Joined: Wed May 02, 2012 10:24 am

How about using the copy action on the archive and then unarchiving it?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I'm not sure that would work in my case.
Let me explain what I'm doing.
I'm trying to unarchive tv show episodes as they finish downloading, then move them to the root of that season.

Here's what a folder may look like
--First, a note. I've set my torrent client to add a specific extension to any incomplete files. (.part in this example)

/Series
----/Season 1 (completed, nothing to unrar)
----/Season 2
--------/episode1.mkv
--------/episode2.mkv
--------/episode3 folder
------------/episode3.rar
------------/episode3.r00
------------/episode3.r01
------------/episode3.r02
------------/episode3.r03
------------/etc....
--------/(extracted)episode3.mkv
--------/episode4.mkv
--------/episode5 folder
------------/episode5.rar.part
------------/episode5.r00
------------/episode5.r01
------------/episode5.r02.part
------------/episode5.r03.part
------------/etc....
--------/episode6.mkv
--------/episode7.mkv
--------/episode8 folder
------------/episode8.rar
------------/episode8.r00
------------/episode8.r01
------------/episode8.r02
------------/episode8.r03
------------/etc....
------------/etc....

Here's how I was trying to accomplish that task with hazel.
-Look for folders with don't have any files in their root ending in the ".part" extension. Do, Run rules on folder contents. (I had to use an applescript to for the logic do this, since hazel would descend multiple levels when checking for these files; In my example above, this would cause hazel to wait for all episodes in the season 2 folder to finish downloading before extracting any.)

Here is the applescript, in case anyone cares
Code: Select all
set x to 0
set f to "part"
tell application "Finder"
   set folder_contents to every file of folder (theFile)
   repeat with i in folder_contents
      if (name of i ends with f) then
         set x to 1
         exit repeat
      end if
   end repeat
   if (x is 0) then
      return true
   else
      return false
   end if
end tell


-Select the first file in the archive, if it exists, then run an applescript to extract it to a temporary subfolder, move any movie related files up two directories (to the season folder,) then delete the temporary folder and any remaining contents. Finally the applescript would also add a red label to the finished product(s), and a grey label to the folder the original archive was in.

Using the suggested method
Copying the original archive doesn't work in my case, since these are primarily multipart archives.
For example, this
Image
throws a ton of errors, since it tries to extract each part as it comes out. It should eventually work, but it's really annoying to have hazel spamming my screen with hundreds of Growl error messages.

Having Hazel copy all the files first, then extract, starts to get a little complicated, perhaps harder to follow than my applescript.


i.e.
Rule 1
I would need to set a temporary folder somewhere within the folder structure I am working in, then copy the files into it. Since this folder needs to be somewhere in the (dynamic) directory structure with a static relation to the original files, the only item available for the Copy action is Enclosing Folder. I would also rename them to make finding them in the next rule easier, i.e. append TEMP to the beginning.

Rule 2
Now, since I need to wait for all files to copy before I unarchive, I need to make a new rule. This gets tricky, since Hazel is now looking one folder deeper than the files I need it to work on, so I need to set an applescript in the previous rule using hazelSwitchFile to set the focus to the right directory. Now I can search for files that match both Kind = Archive and Name - Starts With = TEMP, and unarchive them, then mark them red.

Now, I'm pretty sure the above should work for most people, however for me there is a big problem.

All of my media is on a large maczfs {http://code.google.com/p/maczfs/} volume, which has one glaring issue. Metadata doesn't work properly. No metadata means no matching items by Kind. (Maybe once this ZEVO Platinum Edition {http://tenscomplement.com/our-products/zevo-platinum-edition} I can avoid this issue.)

So, I have to resort to matching by file extension only. If I want to copy the files first, then unarchive using Hazel, I have a problem, since I need to have a rule to match every part of the archive, without using the Kind property. With files like, *.r00, *.r01, *.r02 etc this could get either complicated or error prone.
I think it is probably easier to do this by matching the only one file in the archive, then passing it to a command-line version of unrar, and do any additional processing via the script, which is what I did.
My way also has the advantage of being able to colour the folder containing the original archive easily.

In case anyone is wondering, here is what my rules look like

Image

Image
Ergohack
 
Posts: 2
Joined: Wed May 02, 2012 10:24 am

I see. Yeah, multipart rars are a PITA and really kinda stupid in the current age of high bandwidth and tons of storage. I really don't understand why people keep using them. The alternative is to copy the folder containing the rar files and when they are finished downloading, process that folder.

Also, you can use the "Subfolder level" attribute to limit how far down Hazel goes. You can use it to either limit how far down it goes with the "Run rules on folder contents" or to limit when certain rules are applied to files in those subfolders.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support