Apple script for financial year

Discussion in 'Mac Programming' started by stuckagain, Jun 21, 2014.

  1. stuckagain macrumors newbie

    Joined:
    Jun 21, 2014
    #1
    I need a script that returns the current financial year. So, for any date in the period 1 April 2014 - 31 March 2015, the script would return "2014/15", and for any date in the period 1 April 2015 - 31 March 2016, the script would return "2015/16", ad infinitum,

    I suspect this lends itself to an apple script solution, but alas I don't yet have this skill, and Google didn't help either. Can anyone assist?

    A million thanks.
     
  2. LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
    #2
    Where do you need the returned result? Are you working in Excel, Numbers, or are you working on an application in Java, Xcode…

    Have a look at Automator, it should do that quite well.
    AppleScript could do the trick as well. Have a look:
    Code:
    display dialog "Fill in the month:" default answer "eg 6"
    set v_month to the text returned of the result as number
    display dialog "Fill in the year:" default answer "eg 2014"
    set v_year to the text returned of the result as number
    if v_month is greater than 3 then
    	set v_return to v_year & "/" & (v_year + 1) as string
    else
    	set v_return to (v_year - 1) & "/" & v_year as string
    end if
    return v_return
    
    Be more precise on where you need it.
     
  3. stuckagain thread starter macrumors newbie

    Joined:
    Jun 21, 2014
    #3
    Thanks for your help and apologies for lack of clarity - I am new to all this!

    I want a script for the TextExpander Mac app. This accepts AppleScript and ShellScript. I want it to convert the current system date into the financial year, so now would be "2014/15", and this will allow me to use a short-cut to insert that text into any application as text via textexpander.

    Ideally I would also like to take that script and create a second script that does exactly the same thing except it inserts the next financial year, which for today would be "2015/16".

    Many thanks for the help thus far.
     
  4. LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
    #4
    There is not too much to change to achieve that. Simply use the code above and add the system date instead of the display dialogs…

    Code:
    set x to current date
    set v_month to month of x
    set v_year to year of x
    if v_month is greater than 3 then
    	set v_return to v_year & "/" & (v_year + 1) as string
    else
    	set v_return to (v_year - 1) & "/" & v_year as string
    end if
    return v_return
    Hope that helps. But since I didn't test the script in TextExpander, your mileage may vary.
     
  5. stuckagain thread starter macrumors newbie

    Joined:
    Jun 21, 2014
    #5
    @LinusR Thanks for all your help. I modified a bit to get what I wanted.

    Your code produced 2014/2015. Whereas, I wanted 2014/15. So I did ...

    Code:
    set x to current date
    set v_month to month of x
    set v_year to year of x
    if v_month is greater than 3 then
    	set v_return to v_year & "/" & (v_year - 1999) as string
    else
    	set v_return to (v_year - 1) & "/" & (v_year - 2000) as string
    end if
    return v_return
    i guess there might be a programatic way to do this in a way that works after 2099, but I won't be around to benefit and this works just great!

    I also modified it for the following financial year as follows:

    Code:
    set x to current date
    set v_month to month of x
    set v_year to year of x
    if v_month is greater than 3 then
    	set v_return to (v_year + 1) & "/" & (v_year - 1998) as string
    else
    	set v_return to (v_year) & "/" & (v_year - 1999) as string
    end if
    return v_return
    It all works just fine! Many many thanks. Have a great day
     
  6. LinusR, Jun 21, 2014
    Last edited: Jun 21, 2014

    LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
  7. stuckagain thread starter macrumors newbie

    Joined:
    Jun 21, 2014
    #7
    Did you mean to include some code? It didnt come thru
     
  8. LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
    #8
    No problem, there was a mistake in the code I posted for every year, here is the working version:

    Code:
    set x to current date
    set v_month to month of x
    set v_year to year of x
    
    set v_year_second to v_year mod 10
    set v_year_first to ((v_year / 10) - (v_year / 10 mod 1) as integer) mod 10
    
    if v_month is greater than 3 then
    	if v_year_second is 9 then
    		set v_year_second to 0
    		if v_year_first is 9 then
    			set v_year_first to 0
    		else
    			set v_year_first to v_year_first + 1
    		end if
    	else
    		set v_year_second to v_year_second + 1
    	end if
    	
    	set v_return to v_year & "/" & (v_year_first & v_year_second) as string
    else
    	set v_return to (v_year - 1) & "/" & (v_year_first & v_year_second) as string
    end if
    
    return v_return
    
    This should work perfectly.
     
  9. stuckagain thread starter macrumors newbie

    Joined:
    Jun 21, 2014
    #9
    It works just great. Can I ask how it should be modified to give the following financial year, i.e. 2015/16?

    Many thanks
     
  10. LinusR macrumors regular

    LinusR

    Joined:
    Jan 3, 2011
    #10
    No problem, just change the third line:
    Code:
    set x to current date
    set v_month to month of x
    [B]set v_year to (year of x) + 1[/B]
    
    ...
    
    If you want to learn AppleScript or at least the basics I suggest "AppleScript 1-2-3" by Soghoian and Cheeseman, it's a great book.

    Hope that helps.
     
  11. stuckagain thread starter macrumors newbie

    Joined:
    Jun 21, 2014
    #11
    You are very kind. I plan to go to the bookstore next week! Thanks again
     

Share This Page