Page 1 of 1

Custom Matching 101: Pass Hazel Variables to Applescript

PostPosted: Wed Oct 10, 2012 1:01 pm
by a_freyer
Some folks have been asking for a method to pass Hazel variables to AppleScript.

This tutorial will allow you to pass any number of variables to AppleScript, so long as in total the variable content is less than 255 characters (OSX HFS limitation, although wikipedia seems to think we can use a filename of unlimited length).

Here is a quick tutorial using custom matching (please see that tutorial if you're new to custom matching).

This is how the rule will look when we are done:

Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
    ... whatever you use to match the file or folder ...
    Name matches (RENAMING TOKEN NAME •)
    Extension matches (RENAMING TOKEN EXTENSION •)

Do the following to the matched file or folder
    Rename with pattern: (first token to pass)|(second token to pass) | ...
    Run AppleScript with Embedded Script
    Rename with pattern: (RENAMING TOKEN NAME •).(RENAMING TOKEN EXTENSION •)



The embedded AppleScript is this:

Code: Select all
tell application "Finder"
   set myHazelTokenDelimiters to "|"
   set theListOfCustomTokens to name of theFile
   set AppleScript's text item delimiters to {myHazelTokenDelimiters}
   -- Now, the tokens are available in theListOfCustomTokens as items 1,2,3 etc...
   
   -- The following is an example
   activate
   display dialog text item 2 of theListOfCustomTokens
end tell


Step 1: Create a Rule Using Two Custom Matching Tokens and Arbitrary File Matching


Because Hazel only passes the filename to AppleScript, that is what we're going to use to store the variables. Basically, we're going to rename the file to a string of tokens, pass the file to applescript, and un-rename the file. So first, we need Hazel to remember the filename.

Note: we need custom tokens to remember the filename because Hazel's automatic tokening of filename and extension changes when the file renames. The extension will be cut off when we rename the file so we will need to save that one too.

Create two custom tokens that both are ONLY anything (...)

Custom Token for the Name Condition:
Name - RENAMING TOKEN NAME
Elements - Anything (...)

Custom Token for the Extension Condition:
Name - RENAMING TOKEN EXTENSION
Elements - Anything (...)

Note: We need to create two conditions because Hazel splits up the name and extension. If we tried to match the name to a (name).(extension) we would not match anything because we would be trying to match an extension within the filename itself.

Simple right?

Don't forget to add arbitrary matching as well! As it stands, matching the name and extension to anything will match every file in the folder!

Here is where we stand:

Code: Select all
if (all) of the following conditions are met for (the file or folder being matched)
    ... whatever you use to match the file or folder ...
    Name matches (RENAMING TOKEN NAME •)
    Extension matches (RENAMING TOKEN EXTENSION •)



Step 2: Rename, Pass, and Un-Rename


Pretty simple, so I'll just explain the delimiter. Looking at the above applescript, the variable myHazelTokenDelimter must be embedded in the script - so make sure that whatever you use is used consistently. It can be anything, but a pipe is pretty uncommon so I suggest that.

Hope this helps someone!

Re: Custom Matching 101: Pass Hazel Variables to Applescript

PostPosted: Fri Jan 11, 2013 2:46 am
by JBB
I've done something similar by setting the comment field of the file using Hazel. Either Applescript or a shell script can access this without going through renaming, remembering the name, etc. In csh this works:

`mdls -name kMDItemFinderComment "$1" | awk -F'=' '{print $2}' | tr -d '"'`

You can of course create lists also. It's not 100% reliable (sometimes the script gets run before the comment sticks).

I'd like to see a way to pass tokens and hardcoded values to shell scripts.

Re: Custom Matching 101: Pass Hazel Variables to Applescript

PostPosted: Fri Jan 11, 2013 10:56 am
by a_freyer
One problem with FinderComments is that they aren't actually stored in the metadata of the file, they're stored in the .ds_store. You can see this by setting the FinderComment and then checking CMD+i; your comments will not be there. Similarly, if your set the comment in Finder, it will not appear after an mdls.

I agree, a way to pass tokens to scripts would be great.

Re: Custom Matching 101: Pass Hazel Variables to Applescript

PostPosted: Thu Aug 15, 2013 8:49 pm
by sjk
Browsing through some topics in this subforum to expand my knowledge I noticed[1]:

a_freyer wrote:One problem with FinderComments is that they aren't actually stored in the metadata of the file, they're stored in the .ds_store.

Spotlight (aka Finder) Comments are also stored with the com.apple.metadata:kMDItemFinderComment extended attribute (since 10.5?). Presumably them still being supported in .DS_Store files is at least for backwards compatibility.

You can see this by setting the FinderComment and then checking CMD+i; your comments will not be there. Similarly, if your set the comment in Finder, it will not appear after an mdls.

That's no longer true, at least with my testing on 10.8.4.

[1] Of course there's outdated info on this forum but I'm not on a mission to actively seek it and offer corrections. :)

Re: Custom Matching 101: Pass Hazel Variables to Applescript

PostPosted: Wed Dec 11, 2013 2:30 am
by deverman
Can we please have a more automatic way of passing custom matching variables to applescripts? I would like to be able to pass the amount of the bill that I need to make a bank transfer on so I don't have to open the PDF to look it up.