Mk hardlinks from files Hazel finds using enclosing dir name

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

Moderator: Mr_Noodle

I've searched using various strings here and cannot find anything relating to what I am trying to accomplish
stack overflow isn't very helpful as they try to convince me to use a shell script to find the movie files first
along the lines of

Code: Select all
for f in dir/path/*.mp4


hazel can do the searching, I just need help with passing the found movies as a variable to make hardlinks

So I want hazel to search a directory for movies, date last matched is blank.

then run a shell script using the following scheme "enclosing folder movie name.mp4"

what I imagine would work is

Code: Select all
ln -p /path/to/new/folder/enclosingFolderName$1.mp4

where $1 is the movieName, I have another script where $1 is the name of the filetype Hazel has matched.

what I do not know is how to make `enclosingFolderName` a variable either through shell or Hazel

I would appreciate any help. and if this is unclear I will try to better explain
elgallo
 
Posts: 17
Joined: Sat Apr 19, 2014 7:41 am

You can do something like this:
Code: Select all
   If (all) are met
      Extension is mp4
      If (all) are met for (the enclosing folder)
          Name matches (•enclosing folder name)


where (•enclosing folder name) is a custom attribute set to match "anything". That will capture the name of the enclosing folder that you can pass to a script. The problem currently is that you can only pass variables to AppleScript/JavaScript. You can, though, have that script call your shellscript.

Check out the manual for the details of the above.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I've has some luck with passing folder names into a bash/shell script as follows

Code: Select all
   If (all) are met
      Kind is folder
      Date created is in the last 3 minutes
      Name matches (•date)


where (date) is automatically detect date format

and passes that folder name to a shell script

Code: Select all
mkdir -p $1/{Music,Fotos/{cameraName,Timelapse},Video/{B\ Roll,Hyperlapse}}


Where $1 is the name of the folder Hazel matched. and it works, though I suppose the limitation is using the name of folder Hazel matched as a single placeholder? variable that can be called upon to continue making folders within the folder that is matched.


at any rate Thanks Mr. Noodle! I'll give your method a try and report back

couldn't figure out how to pass (•enclosing folderName) and add (•fileName).mp4

but I managed a workaround.

Rule `subfolders`
Code: Select all
    If (All) are met
        Kind is (folder)
    Continue matching rules
    Run rules on folder contents


Rule `copy movies with new name to /path/to/destination`
Code: Select all
    If (all) are met
        Kind is (Movie)
        Name matches (•fileName) #token is (anything)
    If (all) are met (for enclosing folder)
        Name matches (•enclosingFolder) #token is (anything)
        Rename with pattern (•enclosingFolder) (•fileName).mp4
    Copy to folder (destination/path)


I would have prefered to make a hardlink, to save space, but copy is good enough for now.

sidenote to Mr. Noodle perhaps hardlink could be added as an option in addition to (move,copy)
elgallo
 
Posts: 17
Joined: Sat Apr 19, 2014 7:41 am

If you want to pass those custom attributes in, as mentioned before, you'll have to use AppleScript as it's currently not supported with shellscripts. If you use AppleScript, you can specify multiple parameters in and then have it call the shell script and pass them on.

Note that there is a "Make alias" action. Also, hardlinks are problematic as not all filesystems support them for directories (including the newer APFS).
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Code: Select all
folder="${1%/*}"


This should assign the variable folder to the path of the enclosing folder of the file that is matched by Hazel.
smm
 
Posts: 7
Joined: Sun Oct 25, 2015 10:28 am

smm wrote:
Code: Select all
folder="${1%/*}"


This should assign the variable folder to the path of the enclosing folder of the file that is matched by Hazel.


I had tried and failed something similar in shell, I couldn't figure out how to escape the forward slash to included the enclosing folder. (I kept trying `\`)

Mr. Noodle, I failed to mention that these rules would run on a headless Linux server, both the source and destination.

thanks again for your help, I will try and remember these tips next time I... what was I doing?
elgallo
 
Posts: 17
Joined: Sat Apr 19, 2014 7:41 am

You can also try 'dirname' to get the containing directory.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support