Help with applescript for current date (month & year) in file name?


Joined
Jun 25, 2021
Messages
1
Reaction score
0
Hi, I'm trying to create an apple script and run it through Keyboard Maestro. The script will copy a specific file to the clipboard (the actual file. Not the name or the path) and automatically paste it into apple notes. I have it working the way I want it to if the file name is static. The problem is that file name contains the current month and year. For example "2021 June - FileName1". This month it works great! Next month in July it won't. How do I script it so that the file name that is copied has the current date in it. e.g. this month "2021 June - FileName1.jpg" next month "2021 July - FileName1.jpg". I've tried setting variables for the date but I am very new to applescript and can't figure out how to script it correctly. Any help would be greatly appreciated!

This is what I have so far:

*** Keyboard Maestro opens apple notes and makes a new note and then runs this applescript***

set the clipboard to POSIX file "/Users/MyName/Downloads/Bills/2021 June - FileName1.jpg"
delay 0.2
tell application "System Events" to key code 9 using {command down}
delay 0.2
set the clipboard to POSIX file "/Users/MyName/Downloads/Bills/2021 June - FileName2.pdf"
delay 0.2
tell application "System Events" to key code 9 using {command down}
 
Ad

Advertisements

Joined
Jan 25, 2017
Messages
1,008
Reaction score
68
I think this code should output the filename in the format you require.

Code:
set today to date string of (current date)
set [_day, _month, _year] to [day, month, year] of date today


set _month to _month * 1

set _month to text -1 thru -2 of ("0" & _month)

set _year to text -1 thru -4 of ("0" & _year)


if _month = "01" then

    set _month to "January"

else if _month = "02" then

    set _month to "February"

else if _month = "03" then

    set _month to "March"

else if _month = "04 " then

    set _month to "April"

else if _month = "05" then

    set _month to "May"

else if _month = "06" then

    set _month to "June"

else if _month = "07" then

    set _month to "July"

else if _month = "08" then

    set _month to "August"

else if _month = "09" then

    set _month to "September"

else if _month = "10" then

    set _month to "October"

else if _month = "11" then

    set _month to "November"

else

    set _month to "December"

end if


set filename to _year & " " & _month & " - FileName1" & ".jpeg"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top