Page 1 of 1

Mk hardlinks from files Hazel finds using enclosing dir name

PostPosted: Wed Jun 13, 2018 8:11 am
by elgallo
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

Re: Mk hardlinks from files Hazel finds using enclosing dir

PostPosted: Wed Jun 13, 2018 10:49 am
by Mr_Noodle
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.

Re: Mk hardlinks from files Hazel finds using enclosing dir

PostPosted: Thu Jun 14, 2018 7:57 am
by elgallo
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)

Re: Mk hardlinks from files Hazel finds using enclosing dir

PostPosted: Thu Jun 14, 2018 11:35 am
by Mr_Noodle
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).

Re: Mk hardlinks from files Hazel finds using enclosing dir

PostPosted: Thu Jun 14, 2018 2:28 pm
by smm
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.

Re: Mk hardlinks from files Hazel finds using enclosing dir

PostPosted: Fri Jun 15, 2018 4:25 am
by elgallo
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?

Re: Mk hardlinks from files Hazel finds using enclosing dir

PostPosted: Fri Jun 15, 2018 10:47 am
by Mr_Noodle
You can also try 'dirname' to get the containing directory.