Moving Containing Folder of A File

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Moving Containing Folder of A File Tue Mar 20, 2007 7:30 pm • by IDontDoWindows
You can use the following AppleScript to have Hazel move the folder containing the selected file to a new location.

Code: Select all
my hazelProcessFile(leFiche)

on hazelProcessFile(leFiche)
   try
   tell application "Finder"
      activate
      set leDossier to container of (selection as alias)
      move leDossier to folder "users:me:documents:" of startup disk
   end tell
   on error
   display dialog "Ummmm.... What?"
   end try
end hazelProcessFile
Last edited by IDontDoWindows on Wed Mar 21, 2007 4:29 pm, edited 2 times in total.
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Tue Mar 20, 2007 9:55 pm • by Mr_Noodle
Thanks you for sharing your script. I was wondering in this case whether you meant to get the container of "leFiche" and not the selection. I haven't actually tried it but this is just from eyeballing it.
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Wed Mar 21, 2007 4:49 pm • by IDontDoWindows
I was so busy solving a problem I missed the point. Yeah, I meant to get the container of the file based on a problem a user posted. The user wanted Hazel to look into files of folders on the desktop and move the containing folder based on what it found.
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Re: Moving Containing Folder of A File Thu May 15, 2008 8:40 pm • by ancient
ok lets clear this up ?

is the code shown above good and accurate ? or does it have a bug ? if faulty can a modified correct version be posted below where the variables used in the applescript code are listed ie like :

variablex = explanation of what the variable represents

im presuming (lefiche) is the file found via the rule conditionals in hazel, which it has passed to the applescript which is to be the object of the actions within the applescript ?

BTW thnx for writing hazel, this is the closest thing i can find to an auto filing tool to organise files on my machine, which is why purchased it right away. let this also serve as a bump for an action in a new version, such as "move containing folder too" etc.
ancient
 
Posts: 1
Joined: Thu May 15, 2008 8:25 pm

Re: Moving Containing Folder of A File Mon May 19, 2008 3:30 pm • by Mr_Noodle
The AppleScript handler has to have a certain name and is passed in a single argument which is the file that matched.

IDontDoWindows should maybe comment on the current operational status of the script (or you could try it out on some test files).

And yes, the feature to be able to apply actions to the parent folder is on the list. More an issue of when. Note that there is an AppleScript improvement slated for the next version which allows an AppleScript to re-target the rule to use a different file such that subsequent actions would apply to that file. You would be able to use that to get what you want until a built-in method of doing it is provided.
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Moving Containing Folder of A File Tue Aug 12, 2008 6:15 am • by IDontDoWindows
The above script was buggy. It received a file from Hazel, but instead of referencing it, it attempted to act on whatever item(s) were selected in the Finder. The corrected script is below.

my hazelProcessFile(leFiche)

on hazelProcessFile(leFiche)
     try
          tell application "Finder"
               activate
               set leDossier to container of (leFiche as alias)
               move leDossier to folder "users:username:documents:" of startup disk
          end tell
          
     on error
           tell application "Finder" to display dialog "Ummmm.... What?"
     end try
end hazelProcessFile

Hazel gets the file and passes it onto the script. Finder gets the name of the folder the file is located in, then moves that folder to username's Documents folder (substitute your user name in place of 'username').

If there's an error, the Finder displays a dialog box.

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Re: Moving Containing Folder of A File Wed Aug 13, 2008 12:19 pm • by Mr_Noodle
Actually, try this:
Code: Select all
tell application "Finder"
   set posix_parent_dir to POSIX path of (container of (item theFile) as text)
   
end tell
return {hazelSwitchFile:posix_parent_dir}


What this script does is re-target the file being processed to its parent. Any subsequent Hazel actions will operate on the parent folder. It uses a new feature in 2.2 which allows you to return a record with some special values to communicate back to Hazel (check the help for more details).

Also, if you are putting this in an external script (instead of in the rules itself), remember to add the handler around it.

This script differs from the above in that it doesn't do the move for you but it's probably better to use Hazel's built-in action for that anyways.
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Moving Containing Folder of A File Wed Aug 13, 2008 2:13 pm • by IDontDoWindows
Oooh!!! Sexy! I didn't know about that! Thanks for the tip! Also like the way you condensed that script into about 2 lines! I'm copying that code snippet for other projects.
IDontDoWindows
 
Posts: 57
Joined: Sun Jan 07, 2007 5:43 am

Re: Moving Containing Folder of A File Mon Sep 29, 2008 11:37 am • by elishasessions
I've got this working for moving folders of music to my NAS, and then importing the music into iTunes.

In my Downloads folder, I first delete empty folders, then run rules on folder contents. Then I have this rule:

Code: Select all
If all of the following conditions are met:
Subfolder Depth is greater than 0
Kind is Music

Do the following:
Run Applescript - embedded script (I pasted in Mr. Noodles' script above)
Move file to folder "music" (this is on my NAS)


I then have a rule which moves just music on the top level:

Code: Select all
If all of the following conditions are met:
Subfolder Depth is 0
Kind is Music

Do the following:
Move file to folder "music" (this is on my NAS)


For the music folder on my NAS, I run rules on folder contents, then have a rule that says:
Code: Select all
If all of the following conditions are met:
Kind is Music
Date last modified is Today

Do the following:
Import into iTunes to playlist "new"
elishasessions
 
Posts: 4
Joined: Mon Jun 09, 2008 1:03 pm

Re: Moving Containing Folder of A File Tue Oct 14, 2008 12:40 pm • by emmanuel
I love Hazel, and this topic has deepened that love :).

A question, though: I want to open PAR2 files in the default application when the download of an NZB is finished. The NZB download writes multiple files into a directory, so I want to use the parent directory's modification time to determine when the download is complete.

To do this, I'd like to take the applescript posted by Mr. Noodle, evaluate the mod time of the parent dir, and continue to operate on the file in the dir (the PAR2) if the mod time test passes.

I know I could write an AppleScript to handle everything after detecting the PAR2, but I'd like to keep the logic as encapsulated inside Hazel as possible. Any hints/pointers?
emmanuel
 
Posts: 6
Joined: Tue Oct 14, 2008 12:06 pm

Re: Moving Containing Folder of A File Wed Oct 15, 2008 12:29 pm • by Mr_Noodle
When is the whole download done? You could use the script above to switch to the parent and set a comment/color it or whatever to indicate it is ready to be processed.

You can then have another rule looking for folders with that comment/color and run an applescript to process the par2 files in it. I'm still a bit vague on what this actually is so you'll have to adapt it to your case.
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Moving Containing Folder of A File Wed Oct 15, 2008 2:23 pm • by emmanuel
Thanks for the quick reply. I should have been clearer - the download is written directly into the parent dir, so the parent dir's mod time is constantly updated while the download is in progress. That will give me a cue

I didn't even think about using colors to indicate download completion status. That would do double duty - provide me with a visual cue about what's done and what's not, and provide a way to pass information to Hazel about what action to take.

I'm new to thinking of solving problems using Hazel's strengths, but it's becoming clearer. I'll post my complete solution once it's working and polished.

Thank you, Mr. Noodle!
emmanuel
 
Posts: 6
Joined: Tue Oct 14, 2008 12:06 pm

Re: Moving Containing Folder of A File Mon Aug 16, 2010 12:41 pm • by dieselpower44
Mr_Noodle wrote:Actually, try this:
Code: Select all
tell application "Finder"
   set posix_parent_dir to POSIX path of (container of (item theFile) as text)
   
end tell
return {hazelSwitchFile:posix_parent_dir}


What this script does is re-target the file being processed to its parent. Any subsequent Hazel actions will operate on the parent folder. It uses a new feature in 2.2 which allows you to return a record with some special values to communicate back to Hazel (check the help for more details).

Also, if you are putting this in an external script (instead of in the rules itself), remember to add the handler around it.

This script differs from the above in that it doesn't do the move for you but it's probably better to use Hazel's built-in action for that anyways.


Hey,
I've just tried this in one of my rules for "if there is THIS file inside the folder, jump back a directory" but then after it has jumped back, any rules after this don't get evaluated. Any ideas what might be going on?
dieselpower44
 
Posts: 2
Joined: Mon Aug 16, 2010 8:36 am

Re: Moving Containing Folder of A File Fri Aug 20, 2010 4:12 pm • by Mr_Noodle
You'll have to check the logs to see what happened. Post here or email me what you find there.
Mr_Noodle
Site Admin
 
Posts: 8571
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: Moving Containing Folder of A File Mon Aug 23, 2010 9:49 am • by silvine
Is there anyway I can tailor this script to trash empty folders in my download folder?
silvine
 
Posts: 17
Joined: Mon Feb 08, 2010 3:30 pm

Next

Return to Tips & Tricks - DO NOT POST QUESTIONS