Publié le

Un petit peu d’AppleScript

Pour mon auto entreprise, je note tous mes rendez vous client sur le calendrier Apple.
Depuis 7 ans, une fois par mois je passe une journée à saisir dans mon logiciel de compta perso (une version php/mysql que je me suis codé tout seul) les rendez-vous ce qui est très très long (surtout que faire des factures c’est hyper barbant !)

J’ai donc eu l’idée d’un petit AppleScript qui va me récupérer et me faire un listing de mes entrées dans le calendrier pour un mois sélectionné :

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars
--set mois to the text returned of (display dialog "Mois :" default answer "")

set monthList to {"Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Aout", "Septembre", "Octobre", "Novembre", "Décembre"}
set selected_month to choose from list monthList with prompt "Select the month to export: " OK button name "Continue" cancel button name "Cancel"
if selected_month is false then return
if selected_month = {"Janvier"} then set {selected_month, nbJours} to {1, 32}
if selected_month = {"Fevrier"} then set {selected_month, nbJours} to {2, 29}
if selected_month = {"Mars"} then set {selected_month, nbJours} to {3, 32}
if selected_month = {"Avril"} then set {selected_month, nbJours} to {4, 31}
if selected_month = {"Mai"} then set {selected_month, nbJours} to {5, 32}
if selected_month = {"Juin"} then set {selected_month, nbJours} to {6, 31}
if selected_month = {"Juillet"} then set {selected_month, nbJours} to {7, 32}
if selected_month = {"Aout"} then set {selected_month, nbJours} to {8, 32}
if selected_month = {"Septembre"} then set {selected_month, nbJours} to {9, 31}
if selected_month = {"Octobre"} then set {selected_month, nbJours} to {10, 32}
if selected_month = {"Novembre"} then set {selected_month, nbJours} to {11, 31}
if selected_month = {"Decembre"} then set {selected_month, nbJours} to {12, 32}

set dateInf to "2016-" & selected_month & "-01 00:00:00 +0000"
set uneDate to my getDate(dateInf)

set dateSup to "2016-" & selected_month & "-" & nbJours & " 00:00:00 +0000"
set uneDate2 to my getDate(dateSup)

on getDate(t)
	tell (current date)
		set {year, its month, day, its hours, its minutes, its seconds} to words of t
		return it - ((text 21 thru -1 of t) * 36)
	end tell
end getDate

-- Choose the calendar
tell application "Calendar" to set calendarNames to the name of every calendar
set calendar_name to choose from list calendarNames with prompt "Select the timesheet calendar: " OK button name "Continue" cancel button name "Cancel"
if calendar_name is false then return
set calendar_name to the first item of calendar_name

-- Collect the calendar events as CSV
tell application "Calendar"
	set cal to first calendar whose name is calendar_name
	set selectedEvents to every event of cal whose start date ≥ uneDate and end date < uneDate2
	set output to {}
	repeat with myEvent in selectedEvents
		set duration to (((end date of myEvent) - (start date of myEvent)) / 60) as integer
		set hours to duration div 60
		set days to hours div 24
		set hours to hours mod 24
		set minutes to duration mod 60
		set {year:y, month:m, day:d, hours:h, minutes:min} to the start date of myEvent
		tell (100000000 + (d * 1000000 + m * 10000 + y)) as string to text 4 thru 5 & "/" & text 2 thru 3 & "/" & text 6 thru 9
		set outputDate to the result
		tell (10000 + (h * 100 + min)) as string to text 2 thru 3 & ":" & text 4 thru 5 & ":00"
		set outputTime to the result
		tell (1000000 + (days * 10000 + hours * 100 + minutes)) as string to text 5 thru 5
		set outputDuration to the result
		set summaryText to my replace_chars(the summary of myEvent, "\"", "\"\"")
		set outputLine to (summaryText) & "," & outputDuration & "," & (outputDate)
		set end of output to outputLine as text
	end repeat
end tell

-- Write the CSV out to a file
set AppleScript's text item delimiters to {ASCII character 10}
set saveFile to choose file name with prompt "Save timesheet as:" default name calendar_name & selected_month
set the open_target_file to open for access saveFile with write permission
write (output as text) to the open_target_file starting at 0
close access the open_target_file

-- Sort the file so the events are in the correct order
do shell script "sort -o " & quoted form of POSIX path of saveFile & " " & quoted form of POSIX path of saveFile

Une fois le fichier cvs enregistré, je n’ai plus qu’à l’importer dans mon logiciel de compta pour me faire toutes les factures du mois !

 

Je ne détaillerai pas plus, car je garde le code de mon logiciel pour moi, trop d’heures de travail passées dessus.