Custom Matching 101: Pass Hazel Variables to Applescript

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:
The embedded AppleScript is this:
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:
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!
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!