Filing expenses into Australian financial years

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

The Australian financial year goes from 1 July through to 30 June. This makes it somewhat complicated to use Hazel to file into those years.

I noticed I could use an embedded AppleScript as a condition in Hazel and started fooling around.

The script is:

Code: Select all
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-"}

tell application "Finder"
   set filename to name of theFile
end tell

set expenseYear to (first text item of filename) as number
set expenseMonth to (second text item of filename) as number

-- Get the last two characters of the Year
set AppleScript's text item delimiters to savedDelimiters
set lastTwoCharactersOfYear to (characters 3 thru 4 of (expenseYear as text))
set financialYear to (lastTwoCharactersOfYear as text) as number


if expenseMonth ≥ 7 then
   set financialYear to financialYear + 1
end if


set returnText to "FY" & financialYear

-- Note: the order of the hazelOutputAttributes list needs to match the order of the outputAttributes in Hazel
return {hazelOutputAttributes:{returnText}, hazelPassesScript:true}


I also set up an output attribute on the embedded script called financialYear.

The script passes back the financial year to Hazel and then uses that to sort into subfolders with the form "FYXX"
deanpribetic
 
Posts: 3
Joined: Wed Jul 06, 2016 10:12 pm

I'm wondering if you can get the same effect by adjusting the date by -6 months (or is it +6 months?). Something to play with anyways.
Mr_Noodle
Site Admin
 
Posts: 9214
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:I'm wondering if you can get the same effect by adjusting the date by -6 months (or is it +6 months?). Something to play with anyways.


Oh wow! You are totally right…Date Adjust the matched date by +6 months and then use the last two digits of the date concatenated after the string "FY".

So much easier…thanks!
deanpribetic
 
Posts: 3
Joined: Wed Jul 06, 2016 10:12 pm

I just found this and it is something that I am currently looking it.

I was wondering what you meant by "adjusting the date"? Do you mean within Hazel or within the AppleScript?
Rug664
 
Posts: 8
Joined: Sun Aug 18, 2013 7:20 am

Within Hazel. When you include a date in your pattern, you can click on it. A menu will appear with different options, including one to adjust the date.
Mr_Noodle
Site Admin
 
Posts: 9214
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Hi
Thank you for helpful script, I have tried to modify the script to sort files in folders based on Financial year
Folder name pattern FY 2018-19, FY 2019-20
Files are name pattern : 2019-07-26_Tesco_stationary
Financial year is between 6th April to 5th April each year.

My first aim is get month right, then wud try dates;
the Output for Script
File 2019-07-26_Tesco_stationary -> FY 2020 ( expected FY 2019-20)
File 2019-03-15_Sainsbury -> FY 2019 ( expected FY 2018-19)
Please advise, also any pointers to add date in sorting wud be helpful
Thank you


Code: Select all
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-"}

tell application "Finder"
   set filename to name of theFile
end tell

set expenseYear to (first text item of filename) as number
set expenseMonth to (second text item of filename) as number
set expenseDate to (third text item of filename) as number

-- Get the last two characters of the Year
set AppleScript's text item delimiters to savedDelimiters
set lastTwoCharactersOfYear to (characters 3 thru 4 of (expenseYear as text))
set RoundedExpYear to (lastTwoCharactersOfYear as text) as number


if expenseMonth ≥ 4 then
   set LongString to expenseYear
   set ShortString to RoundedExpYear + 1
else
   set LongString to expenseYear - 1
   set ShortString to RoundedExpYear
end if


set returnText to "FY" & " " & LongString & "-" & ShortString

-- Note: the order of the hazelOutputAttributes list needs to match the order of the outputAttributes in Hazel
return {hazelOutputAttributes:{returnText}, hazelPassesScript:true}
Lucky
 
Posts: 4
Joined: Mon Oct 21, 2019 2:25 am

Hey Guys, further tweeked the script
It works, expect that file name 2006-09-21, gets sorted in folder FY 2006-7, instead of FY 2006-7
Any Suggestions ?
Also if I add date as logic
Code: Select all
set expenseDate to (third text item of filename) as number

It gives error, any suggestions wud be welcome
working Code
Code: Select all
set savedDelimiters to AppleScript's text item delimiters

set AppleScript's text item delimiters to {"-"}



tell application "Finder"

   

   set filename to name of theFile

   

end tell





set expenseYear to (first text item of filename) as number

set expenseMonth to (second text item of filename) as number





-- Get the last two characters of the Year

set AppleScript's text item delimiters to savedDelimiters

set lastTwoCharactersOfYear to (characters 3 thru 4 of (expenseYear as text))

set RoundedExpYear to (lastTwoCharactersOfYear as text) as number

set longString to expenseYear - 1

set shortString to RoundedExpYear



if expenseMonth ≥ 4 then

   set longString to longString + 1

   set shortString to RoundedExpYear +1

   

end if



set returnText to "FY" & " " & longString & "-" & shortString



-- Note: the order of the hazelOutputAttributes list needs to match the order of the outputAttributes in Hazel

return {hazelOutputAttributes:{returnText}, hazelPassesScript:true}
Lucky
 
Posts: 4
Joined: Mon Oct 21, 2019 2:25 am

Follow up, got it going, it as issue with delimitters, now can define date as criteria to sort folder, works perfect in Applescript editor and hazel too ( did change to filename as thefile)
made a automator action for folder!
new code
Code: Select all
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_"}

tell application "Finder"
   
   set filename to "2020-04-05_onetwo"
   
end tell

set dateString to (first text item of filename) as text
set AppleScript's text item delimiters to savedDelimiters
set savedDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"-"}

set expenseYear to (first text item of dateString) as number
set expenseMonth to (second text item of dateString) as number
set expenseDate to (third text item of dateString) as number


-- Get the last two characters of the Year
set AppleScript's text item delimiters to savedDelimiters
set lastTwoCharactersOfYear to (characters 3 thru 4 of (expenseYear as text))
set RoundedExpYear to (lastTwoCharactersOfYear as text) as number
set longString to expenseYear - 1
set shortString to RoundedExpYear

if expenseMonth ≥ 4 and expenseDate ≥ 6 then
   set longString to longString + 1
   set shortString to RoundedExpYear + 1
   
end if

set returnText to "FY" & " " & longString & "-" & shortString

-- Note: the order of the hazelOutputAttributes list needs to match the order of the outputAttributes in Hazel
return {hazelOutputAttributes:{returnText}, hazelPassesScript:true}
Lucky
 
Posts: 4
Joined: Mon Oct 21, 2019 2:25 am


Return to Tips & Tricks - DO NOT POST QUESTIONS

cron