Fille organization

Hello, i’m looking for some help. i’m a newbie with applescript.

My objective is to automatically organize a picture folder by putting files, wich name is always composed from 8 digits, on a scriptlet that will distribute these pictures in subfolders wich name correspond to the filename decomposition.

I mean filename : YYYYMMDD-X and the folder structure is YYYY/MM/DD

I wrote a script that checks the filename
check the target folder path. If the path does not exists it is created.
if the path exists it moves the file fom current location to the target folder.
Seems simple and not too complicated BUT !
The main problem is that the scriplet doesn’ work. there must be some silly mistakes in my code and i really do not get what they are.

Here is the code. if someone could give me a clue or some good advice i would be very grateful.


property parent_folder : (path to "cusr") as string) & "Tri:"
on run
	beep
	display dialog "pour utiliser ce script, glisser déposer des fichiers dessus, il seront rangés selon la spécification ANJAC" buttons {"OK"} default button 1 with icon 2 giving up after 10
	return
end run
on open added_items
	try
		tell application "Finder"
			
			repeat with i from 1 to (count of added_items)
				set LeFichier to (item i of added_items)
				set var1 to (text 1 thru 2 of LeFichier) as string
				set var2 to (text 3 thru 4 of LeFichier) as string
				set var3 to (text 5 thru 6 of LeFichier) as string
				set folder1 to (parent_folder & var1 & ":")
				set folder2 to (parent_folder & var1 & ":" & var2 & ":")
				set folder3 to (parent_folder & var1 & ":" & var2 & ":" & var3 & ":")
				if folder1 = false then make new folder at folder parent_folder with properties {name:var1}
				if folder2 = false then make new folder at folder folder1 with properties {name:var2}
				if folder3 = false then make new folder at folder folder2 with properties {name:var3}
				set source_folder to (container of LeFichier) as string
				move ((source_folder & LeFichier) as alias) to folder folder3
			end repeat
		end tell
	end try
end open

Tryphon

The first thing I see wrong with it is the code:

set LeFichier to (item i of added_items) 
set var1 to (text 1 thru 2 of LeFichier) as string 

At this point, ‘LeFichier’ is an alias object that identifies one of the items dropped on the script. You can not get ‘text 1 through 2’ of an alias. That doesn’t make any sense.

Instead, you need to:

set var 1 to (text 1 through 2 of (name of LeFichier)) as string

or, even better replace the repeat loop with something like:

repeat with LeFichier in added_items
   set nomDeFichier to name of LeFichier
   set var1 to (text 1 through 2 of nomDeFichier) as string
   set var2 to (text 3 through 4 of nomDeFichier) as string
   set var3 to (text 5 through 6 of nomDeFichier) as string
  ...

(excuse my French :slight_smile: )

In fact i’m making a confusion between alias and strings. I introduced a few correction to the code.

Still, it still doesnt work when after modifying the code.


property parent_folder : ((path to "cusr") as string) & "Tri:"
on run
	beep
	display dialog "pour utiliser ce script, glisser déposer des fichiers dessus, il seront rangés selon la spécification ANJAC" buttons {"OK"} default button 1 with icon 2 giving up after 10
	return
end run
on open added_items
	try
		tell application "Finder"
			repeat with i from 1 to (count of added_items)
				set LeFichier to (item i of added_items)
				set nomDeFichier to name of LeFichier
				set var1 to (text 1 through 4 of nomDeFichier) as string
				set var2 to (text 5 through 6 of nomDeFichier) as string
				set var3 to (text 7 through 8 of nomDeFichier) as string
				set folder1 to ((parent_folder & var1 & ":") as string)
				---set folder2 to ((parent_folder & var1 & ":" & var2 & ":") as string)
				---set folder3 to ((parent_folder & var1 & ":" & var2 & ":" & var3 & ":") as string)
				if ((folder1) as alias) = false then make new folder at folder parent_folder with properties {name:var1}
				----if folder2 = false then make new folder at folder folder1 with properties {name:var2}
				----if folder3 = false then make new folder at folder folder2 with properties {name:var3}
				---set source_folder to (container of LeFichier) as string
				move ((source_folder & LeFichier) as alias) to folder ((folder1 & ":") as alias)
			end repeat
		end tell
	end try
end open

Could you please tell me what’s wrong now…

Hi all :slight_smile:
Salut “pao@anjac.fr:wink:

Here one solution - Voici une solution possible :

property DossierParent : (((path to "cusr") as string) & "Tri:" ) as alias

on run
	beep
	display dialog "Veuillez glisser/déposer des fichiers 
sur l'icône du script pour les ranger 
selon la spécification ANJAC" buttons {"OK"} default button 1 ¬
		with icon 1 giving up after 10
	return
end run

on open ListeFichiers
	try
		tell application "Finder"
			repeat with Boucle from 1 to (count of ListeFichiers)
				
				set LeFichier to (item Boucle of ListeFichiers)
				set NomFichier to (name of LeFichier)
				
				set NomDossier1 to (text 1 through 4 of NomFichier)
				set NomDossier2 to (text 5 through 6 of NomFichier)
				set NomDossier3 to (text 7 through 8 of NomFichier)
				
				set CheminDossier1 to ((DossierParent as string) & NomDossier1 & ":")
				set CheminDossier2 to (CheminDossier1 & NomDossier2 & ":")
				set CheminDossier3 to (CheminDossier2 & NomDossier3 & ":")
				
				try
					set Dossier1 to CheminDossier1 as alias
				on error
					set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
				end try
				
				try
					set Dossier2 to CheminDossier2 as alias
				on error
					set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
				end try
				
				try
					set Dossier3 to CheminDossier3 as alias
				on error
					set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
				end try
				
				try
					move LeFichier to Dossier3
				on error
					beep 2
					display dialog "Une erreur c'est produit lors du déplacement du fichier..." with icon 0
				end try
				
			end repeat
		end tell
	end try
end open

Tested - Testé : Os9, As 1.4
PS. C’est quoi une “spécification ANJAC” ???
:wink:

Génial, Great !

ca marche du tonnerrrrre, works perfectly.
MERCI. Je vais maintenant m’efforcer de l’enrichir en en faisant du script de dossier et
en jouant avec les extensions possibles. Je mets ca en ligne asap

:))
la spécification c’est un fichier nommé :
“annéemoisjour-objetdufichier_typeanjac_qualité.extension”

typeanjac c’est i pour image L pour logo etc.
qualité ca va de soi et c’est facultatif.

par exemple : aammjj-blabla_I_HD.EPS

Y’en a qui s’amuse drolement en nommant leurs fichiers dans cette boîte :?

This little script allows to sort files in a chronological folder tree.

Merci encore pour cette aide. les scripts c’est excellent javascript:emoticon(‘:)’)

Hi everybody, I trie to convert this scriptlet to a folder attached script. It runs but only one file at a time, the repeat fonction doesn’t work.

i suppose that finder consider only the first item dropped even if they’re more than one
this way the repeat command works from 1 to 1 and stops.

My problem is that there’s already items present in the destination folder so i cannot use a “sort every item in folder” to solve my problem. Maybe I’m making a stupid newbie mistake !! Could someone explain me how i could manage this ? And make the script repeat with every items added to it.


on adding folder items to DossierParent after receiving ListeFichiers
	try
		tell application "Finder"
			repeat with Boucle from 1 to (count of ListeFichiers)
				
				set LeFichier to (item Boucle of ListeFichiers)
				set NomFichier to (name of LeFichier)
				
				set NomDossier1 to (text 1 through 2 of NomFichier)
				set NomDossier2 to (text 3 through 4 of NomFichier)
				set NomDossier3 to (text 5 through 6 of NomFichier)
				
				set CheminNiveau1 to (DossierParent & NomDossier1 & ":")
				set CheminNiveau2 to (CheminNiveau1 & NomDossier2 & ":")
				set CheminNiveau3 to (CheminNiveau2 & NomDossier3 & ":")
				
				try
					move LeFichier to CheminNiveau3 as alias
				on error
					try
						set Dossier1 to CheminNiveau1 as alias
					on error
						set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
					end try
					
					try
						set Dossier2 to CheminNiveau2 as alias
					on error
						set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
					end try
					
					try
						set Dossier3 to CheminNiveau3 as alias
					on error
						set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
					end try
					
					try
						move LeFichier to Dossier3 with replacing
					on error
						beep 2
						display dialog "Une erreur c'est produit lors du déplacement du fichier..." with icon 0
					end try
				end try
			end repeat
		end tell
	end try
	--end repeat
end adding folder items to

Merci de votre attention :slight_smile:

Tryphon

Hi all :slight_smile:
Salut Tryphon :wink:

Je viens d’essayer, et ça fonctionne très bien chez moi !
I have just tested, and that work very well at home!

Os 9.0.4 - As 1.4

YES, this is it, My mind becomes clearer every minute, and the bug i think was in the parent folder notion

after conversion of the scriptlet I started to use dossier_parent (parent-folder) as an alias and there was a conflict at some point with cheminniveau1 (pathlevel1) were i needed to refer to dossier parent. In the previous version i didn’t neede to write it had to be considered as a string. In this one I forgot to do it and welll… Now it works just fine and that’s pretty cool :)))

here’s my mistake:

set CheminNiveau1 to (DossierParent & NomDossier1 & ":")

Corrected :

set CheminNiveau1 to (((DossierParent) as string) & NomDossier1 & ":")

It’s time to say goodnigth everybody, thnx for your support.
especially fred you cheered me up and it allowed me to find the answer.

By the way here’s the full code of this workin script :

on adding folder items to DossierParent after receiving ListeFichiers
	tell application "Finder"
		repeat with Boucle from 1 to number of items in ListeFichiers
			set LeFichier to item Boucle of ListeFichiers
			
			set NomFichier to name of LeFichier
			
			set NomDossier1 to (text 1 through 2 of NomFichier)
			set NomDossier2 to (text 3 through 4 of NomFichier)
			set NomDossier3 to (text 5 through 6 of NomFichier)
			
			set CheminNiveau1 to (((DossierParent) as string) & NomDossier1 & ":")
			set CheminNiveau2 to (CheminNiveau1 & NomDossier2 & ":")
			set CheminNiveau3 to (CheminNiveau2 & NomDossier3 & ":")
			
			try
				move LeFichier to CheminNiveau3 as alias with replacing
			on error
				try
					set Dossier1 to CheminNiveau1 as alias
				on error
					set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
				end try
				
				try
					set Dossier2 to CheminNiveau2 as alias
				on error
					set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
				end try
				
				try
					set Dossier3 to CheminNiveau3 as alias
				on error
					set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
				end try
				
				try
					move LeFichier to Dossier3 with replacing
				on error
					beep 2
					display dialog "Une erreur c'est produit lors du déplacement du fichier..." with icon 0
				end try
			end try
		end repeat
	end tell
end adding folder items to

Hi all :slight_smile:
Slaut Tryphon :wink:

Une idée peut-être, mais je ne peut pas la tester pour l’instant… Deux scripts :
An idea perhaps, but I cannot test it for the moment… Two scripts:

  1. C’est le script attaché au dossier de destination

  2. It is the script attached to the destination folder

  3. Le script de traitement des éléments glissés dans le dossier.
    Nous nommerons ce deuxième script “MonScript.scpt”, par exemple, que nous placerons dans le dossier de destination

  4. the script of processing of the elements droped into the file.
    We will name this second script “MonScript.scpt”, for example, which we will place into the destination folder

  5. Script attaché au dossier,

  6. Script attached to the folder

on adding folder items to DossierParent after receiving ListeFichiers
	run script (((DossierParent as text) & "MonScript.scpt") as alias) with parameters ListeFichiers
end adding folder items to
  1. Script de traitement
  2. Script of processing
on run ListeFichiers
	try
		tell application "Finder"
			repeat with Boucle from 1 to (count of ListeFichiers)
				
				set LeFichier to (item Boucle of ListeFichiers)
				set NomFichier to (name of LeFichier)
				
				set NomDossier1 to (text 1 through 2 of NomFichier)
				set NomDossier2 to (text 3 through 4 of NomFichier)
				set NomDossier3 to (text 5 through 6 of NomFichier)
				
				set CheminNiveau1 to (DossierParent & NomDossier1 & ":")
				set CheminNiveau2 to (CheminNiveau1 & NomDossier2 & ":")
				set CheminNiveau3 to (CheminNiveau2 & NomDossier3 & ":")
				
				try
					move LeFichier to CheminNiveau3 as alias
				on error
					try
						set Dossier1 to CheminNiveau1 as alias
					on error
						set Dossier1 to (make new folder at DossierParent with properties {name:NomDossier1}) as alias
					end try
					
					try
						set Dossier2 to CheminNiveau2 as alias
					on error
						set Dossier2 to (make new folder at Dossier1 with properties {name:NomDossier2}) as alias
					end try
					
					try
						set Dossier3 to CheminNiveau3 as alias
					on error
						set Dossier3 to (make new folder at Dossier2 with properties {name:NomDossier3}) as alias
					end try
					
					try
						move LeFichier to Dossier3 with replacing
					on error
						beep 2
						display dialog "Une erreur c'est produit lors du déplacement du fichier..." with icon 0
					end try
				end try
			end repeat
		end tell
	end try
end run

Je ne sais pas si ça peut fonctionner correctement, mais, je pense qu’il faut essayer.
I do not know if that can work correctly, but, I think that it is necessary to test.

Bonne chance :wink:

j’ai calé ma solution au dessus, le concept ci-dessous m’était venu à l’idée mais je n’arrivais pas à le mettre en forme. Super en tous cas je testerai aussi pour voir et broder. Bon cette fois c’est dit: Bonne NUIT.

Hello there,
here is a little variation of this script, I switched from “try - error” to “if not exists”.
here’s the function relative to the tree :


property cheminPAO : ("PAO" & ":IMAGES:")

to process_file(this_file)
	tell application "Finder"
		if "_HD." is in name of this_file then
			set aFile to name of this_file
			set NDoss1 to ((text 1 through 2 of aFile) & ":")
			set NDoss2 to ((text 3 through 4 of aFile) & ":")
			set NDoss3 to ((text 5 through 6 of aFile) & ":")
			set decomposed_name to (cheminPAO & NDoss1 & NDoss2 & NDoss3) as string
			
			if (not (exists folder ((cheminPAO) & (NDoss1)))) then
				set outputFolder1 to make new folder at cheminPAO with properties {name:(text 1 through 2 of aFile)}
			end if
			if (not (exists folder (cheminPAO & NDoss1 & NDoss2))) then
				set outputFolder0 to make new folder at ((cheminPAO & NDoss1) as alias) with properties {name:(text 3 through 4 of aFile)}
			end if
			if (not (exists folder (cheminPAO & NDoss1 & NDoss2 & NDoss3))) then
				set outputFolder to make new folder at ((cheminPAO & NDoss1 & NDoss2) as alias) with properties {name:(text 5 through 6 of aFile)}
			end if

                   move item this_file to folder decomposed_name as alias with replacing  
end process_file           

Hope it helps.
Regards
Igor {ex-tryphon} :cool: