# Message d'erreur entre finder et script applescript



## deadbone (7 Novembre 2009)

Bonjour,

Je cherche a adapter un script que j'ai trouvé sur le net pour l'adapter à mes besoins : le script original permet de trier des items en fonction de son extension dans des dossiers spécifiques lorsque le dossier d'origine les reçoit. J'ai du le modifier car s'il fonctionnait très bien pour des fichiers, je voulais qu'il puisse faire de même lorsqu'il recevait un dossier de nouveaux items.

Le script fonctionne très bien pour un fichier simple mais je bute sur un message d'erreur quand il s'agit de déplacer le fichier reconnu (la reconnaissance fonctionne très bien à l'intérieur du nouveau dossier) lorsque ce fichier se trouve dans un dossier lui-même ajouté dans le dossier maitre.

Le message d'erreur est le suivant : 
Erreur dans Finder: Il est impossible de rendre document file "fichier_test.txt" of folder "dossier_ajoute" of folder "DOSSIER_MAITRE" of folder "Desktop" of folder "mon_nom_de_user" of folder "Users" of startup disk en type integer" 

Le script en question (bien bidouillé)

```
property media_extension_list : {"txt", "iso", "avi", "mov", "mpg", "mkv", "mpeg", "vob"}
property media_foldername : "_CLIPS"
property montype : ""

on adding folder items to this_folder after receiving added_items
	try
		repeat with this_item in added_items
			tell application "Finder"
				
				set montype to kind of this_item
				
				if montype = "Dossier" then
					
					try
						
						set folder_list to every item of folder this_item
						repeat with new_file in folder_list
							try
								
								if (the name extension of the new_file is in the media_extension_list) then
									tell application "Finder"
										
										--set alert_message to {"fichier trouve"} as Unicode text
										--display dialog the alert_message
										move file new_file to this_folder
										
									end tell
									
								end if
							on error error_message
								display dialog error_message buttons {"OK"} default button 1
							end try
							set montype to kind of new_file
							set alert_message to {montype}
							display dialog the alert_message
							
						end repeat
						
					end try
					
				else
					my testmedia(this_item, this_folder, media_foldername)
				end if
				
				
			end tell
		end repeat
	on error error_message
		display dialog error_message buttons {"OK"} default button 1
	end try
	
end adding folder items to



on makeamove(this_item, root_folder, target_foldername)
	
	tell application "Finder"
		
		if not (exists folder target_foldername of root_folder) then
			
			make new folder at root_folder with properties {name:target_foldername}
		end if
		
		set the target_folder to folder target_foldername of root_folder
		
		my resolve_conflicts(this_item, root_folder, target_folder)
		
		move file this_item to the target_folder
		
	end tell
	
end makeamove

on resolve_conflicts(this_item, root_folder, target_folder) --renames the item if dublicate exists in target folder
	
	tell application "Finder"
		
		set file_extension to the name extension of this_item
		set the file_name to the name of this_item
		
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		
		if (exists document file file_name of target_folder) then
			
			set the name_increment to 1
			
			repeat
				set the new_name to (the trimmed_name & "_" & (name_increment as string) & "." & file_extension) as string
				
				if not (exists document file new_name of the target_folder) then
					
					set the name of document file file_name of folder root_folder to the new_name
					exit repeat
				else
					
					set the name_increment to the name_increment + 1
					
				end if
			end repeat
		end if
	end tell
	
	return the file_name
end resolve_conflicts

on testmedia(this_item2, this_folder2, media_foldername)
	try
		tell application "Finder"
			if (the name extension of the this_item2 is in the media_extension_list) then
				
				my makeamove(this_item2, this_folder2, media_foldername)
				
			end if
		end tell
	on error error_message
		display dialog error_message buttons {"OK"} default button 1
	end try
end testmedia
```

Le but du script est de trier des fichiers multimedia. J'ai ajouté l'extension txt à des fins de tests uniquement 

Si quelqu'un à une idée pour sauver le peu de cheveux qui me reste...


----------



## zacromatafalgar (7 Novembre 2009)

Salut,

Pour que l'erreur ne survienne pas il faudrait remplacer


```
move file new_file to this_folder
```
par

```
move file (new_file as string) to this_folder
```

mais il faudrait également changer le dossier de destination sans quoi seul le ou les premiers éléments seront traités.

Pour arranger ça je te propose de déplacer directement vers le dossier _CLIPS plutôt que dans this_folder

Je n'ai malheureusement pas trop de temps à te consacrer pour l'instant, peut être les jours prochains si personne n'est passé par là


----------



## deadbone (7 Novembre 2009)

Merci beaucoup pour ta réponse.
Du coup, j'ai corrigé (et amélioré le script) donc je livre la teneur ici à ceux que cela pourrait intéresser.

Merci encore !!


```
property media_extension_list : {"mp4", "iso", "avi", "mov", "mpg", "mkv", "mpeg", "vob"}
property media_foldername : "Chemin_posix"
property montype : ""

on adding folder items to this_folder after receiving added_items
	try
		repeat with this_item in added_items
			tell application "Finder"
				
				set montype to kind of this_item
				
				if montype = "Dossier" then
					
					try
						
						set folder_list to every item of folder this_item
						repeat with new_file in folder_list
							try
								
								if (the name extension of the new_file is in the media_extension_list) then
									tell application "Finder"
										move folder (this_item as string) to folder media_foldername
									end tell
									exit repeat
								end if
							on error error_message
								display dialog error_message buttons {"OK"} default button 1
							end try
							
						end repeat
						
					end try
					
				else
					my testmedia(this_item, this_folder, media_foldername)
				end if
				
				
			end tell
		end repeat
		--on error error_message
		--	display dialog error_message buttons {"OK"} default button 1
	end try
	
end adding folder items to



on makeamove(this_item, root_folder, target_foldername)
	
	tell application "Finder"
		
		if not (exists folder target_foldername of root_folder) then
			
			make new folder at root_folder with properties {name:target_foldername}
		end if
		
		set the target_folder to folder target_foldername of root_folder
		
		my resolve_conflicts(this_item, root_folder, target_folder)
		
		move file this_item to the target_folder
		
	end tell
	
end makeamove

on resolve_conflicts(this_item, root_folder, target_folder) --renames the item if dublicate exists in target folder
	
	tell application "Finder"
		
		set file_extension to the name extension of this_item
		set the file_name to the name of this_item
		
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		
		if (exists document file file_name of target_folder) then
			
			set the name_increment to 1
			
			repeat
				set the new_name to (the trimmed_name & "_" & (name_increment as string) & "." & file_extension) as string
				
				if not (exists document file new_name of the target_folder) then
					
					set the name of document file file_name of folder root_folder to the new_name
					exit repeat
				else
					
					set the name_increment to the name_increment + 1
					
				end if
			end repeat
		end if
	end tell
	
	return the file_name
end resolve_conflicts

on testmedia(this_item, this_folder, media_foldername)
	try
		tell application "Finder"
			if (the name extension of the this_item is in the media_extension_list) then
				
				my makeamove(this_item, this_folder, media_foldername)
				
			end if
		end tell
		--on error error_message
		--	display dialog error_message buttons {"OK"} default button 1
	end try
end testmedia
```


----------

