Bonjour,

Je développe un petit script qui nous sera utile pour gérer les whitelist, blacklist et scorelist de notre serveur Mail. Le script est fini mais afin de le rendre plus lisible et modifiable par la suite je créé des routines (ou fonctions).

Voici une partie de code du script:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
		----------------------CAS MODIFIER----------------------
 
		if resultat2 is "Modifier" then
 
			--On recupere le fichier ScoreList
			--chemin pour les tests
			set chemin_vers_le_fichier to ((path to desktop as text) & "Score.txt") as alias
			--Chemin pour production
			--set chemin_vers_le_fichier to (("/Library/Server/Mail/Config/amavisd/" as text) & "scorelist") as alias
 
			--conversion du fichier en liste avec la routine
			set contenu_Fichier to returnFileContentsAsList(chemin_vers_le_fichier)
 
			--tri par ordre alphabétique
			set tri to simple_sort(contenu_Fichier)
 
			--Affichage des domaines présents dans le fichier
			set ListA to (choose from list tri with prompt "Choississez le domaine que vous voulez modifier?" with title resultat) as string
 
			display dialog "Quel modification voulez-vous effectuer?" & linefeed default answer ListA buttons {"OK", "Annuler"} default button 1 with title resultat
			set modification to text returned of the result as string
 
 
			--si la ligne à modifier est la dernière du fichier, il n'y aura pas de return. Il faut donc tester cela.
			set comptageDeLigne to (count of contenu_Fichier)
			set testDerniereLigne to item comptageDeLigne of contenu_Fichier
			display dialog testDerniereLigne
 
			-- Find and replace a phrase in a TextEdit document
			--si dernière ligne du fichier
			if testDerniereLigne is equal to ListA then
				tell application "TextEdit"
					open ((path to desktop as text) & "Score.txt")
 
					-- Find and replace
					set text of front document to replace_chars(text of front document, ListA, modification) of me
					quit
				end tell
			else
				--si pas dernière ligne du fichier
				tell application "TextEdit"
					open ((path to desktop as text) & "Score.txt")
 
					-- Find and replace
					set text of front document to replace_chars(text of front document, ListA & return, modification & return) of me
					quit
				end tell
			end if
et la partie que j'essaye de rendre fonction:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
			--si la ligne à modifier est la dernière du fichier, il n'y aura pas de return. Il faut donc tester cela.
			set comptageDeLigne to (count of contenu_Fichier)
			set testDerniereLigne to item comptageDeLigne of contenu_Fichier
			display dialog testDerniereLigne
 
			-- Find and replace a phrase in a TextEdit document
			--si dernière ligne du fichier
			if testDerniereLigne is equal to ListA then
				tell application "TextEdit"
					open ((path to desktop as text) & "Score.txt")
 
					-- Find and replace
					set text of front document to replace_chars(text of front document, ListA, modification) of me
					quit
				end tell
			else
				--si pas dernière ligne du fichier
				tell application "TextEdit"
					open ((path to desktop as text) & "Score.txt")
 
					-- Find and replace
					set text of front document to replace_chars(text of front document, ListA & return, modification & return) of me
					quit
				end tell
			end if
Cette dernière partie apparait plusieurs fois dans mon code et des changements y ont été apportés, voila pourquoi j'ai besoin de la convertir en fonction.

Mon soucis est qu'un fois qu'on se trouve dans la fonction, apparait un message d'erreur disant que je n'ai pas les droits d'accès au fichier.

Y a t'il une solution ou explication à cela?

Je met ma fonction aussi, ca peut servir
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--Une routine permettant de tester si la ligne du fichier est la dernière ou non puis fait la modification dans le fichier concerné
on test_derniere_ligne(contenu_Fichier, ListA, modification, resultat)
	--si la ligne à modifier est la dernière du fichier, il n'y aura pas de return. Il faut donc tester cela.
	set comptageDeLigne to (count of contenu_Fichier)
	set testDerniereLigne to item comptageDeLigne of contenu_Fichier
 
	-- Find and replace a phrase in a TextEdit document
	--si dernière ligne du fichier
	if testDerniereLigne is equal to ListA then
		display dialog "1"
		tell application "TextEdit"
			open ((path to desktop as text) & resultat & ".txt")
			-- Find and replace
			set text of front document to replace_chars(text of front document, ListA, modification) of me
			quit
		end tell
	else
		display dialog "2"
 
		--si ce n'est pas la dernière ligne du fichier
		tell application "TextEdit"
			open ((path to desktop as text) & resultat & ".txt")
			-- Find and replace
			set text of front document to replace_chars(text of front document, ListA & return, modification & return) of me
			quit
		end tell
	end if
end test_derniere_ligne
et je l'appelle comme ceci:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
test_derniere_ligne(contenu_Fichier, ListA, modification, resultat)

Merci d'avance de vos réponses.