IPB

Bienvenue invité ( Connexion | Inscription )

> [Résolu] Insérer lettres et nombres aléatoires dans des noms de fichier, avec AppleScript
Options
Pompon
posté 21 Aug 2006, 23:29
Message #1


Macbidouilleur de bronze !
**

Groupe : Membres
Messages : 294
Inscrit : 25 Dec 2002
Lieu : Paris
Membre no 5 299



Bonjour,

Je suis en train de préparer mes photos afin de les mettre en ligne avec PhpWebGallery. J'ai fait un fichier automator qui permet de copier les photos dans un dossier en les redimensionnant et en créeant des vignettes. Jusqu'ici tout va bien.

J'aurai voulu ajouter avant l'extension dans le nom des fichiers une série (configurable) de chiffres et de lettres aléatoires.
Exemple : test_100.jpg -> test_100_apr45.jpg (l'idéal serait de mixer chiffres, lettres et casse pour obtenir par exemple test_100_Ap45R.jpg mais ce n'est pas le plus important).

J'ai trouvé ça sur internet :
-> AppleScript to generate a random number & prefix to filename??
-> Renaming files in a folder by adding a suffix before the extension
-> Random in Applescript

J'ai fait un mix des trois :
CODE
on run
    tell application "Finder"
        copy selection to selectedFiles
        set shuffle_pool to files of selectedFiles
        set shuffle_count to count shuffle_pool
        set duplicates to {}
        repeat with aFile in selectedFiles
            repeat
                set an_item to random number (1000) from 1 to shuffle_count
                if an_item is not in duplicates then exit repeat
            end repeat
            tell aFile
                set theOriginalName to do shell script "basename " & name of aFile & " ." & name extension
                set name of aFile to theOriginalName & an_item & "." & name extension
            end tell
        end repeat
    end tell
end run

J'y suis presque mais je n'arrive pas à intégrer de lettres aléatoires. Lorsque je tente de mettre le code provenant du dernier site, je n'obtiens plus de chiffres aléatoires. C'est l'un ou l'autre mad.gif

C'est sûrement tout bête mais je n'y connais pas grand chose en AppleScript et malgré quelques bidouillages je n'arrive pas à obtenir ce que je souhaite. De plus je pars sur des scripts que ne sont peut-être pas écrit dans ce but à la base.

Si quelqu'un peut m'aider, je suis preneur wink.gif
Merci

Edit : résolu cf. le code Applescript ici, ou l'utilitaire plus élaboré "ZarbiRename" développé pour l'occasion en PostCardWare par AlbertLevert à cette adresse. thumb.gif

Ce message a été modifié par Pompon - 8 Mar 2007, 23:55.
Go to the top of the page
 
+Quote Post
 
Start new topic
Réponse(s)
AlbertLevert
posté 4 Dec 2006, 19:25
Message #2


Macbidouilleur de bronze !
**

Groupe : Membres
Messages : 295
Inscrit : 10 Dec 2003
Membre no 12 371



Y'avait un bug !

les fichiers Photoshop pas reconnu ... car pas dans la liste

Voila qui est corrigé, avec les sous-dossier !


PS: J'ai ajouté le ©copyright wink.gif
CODE
-- Generateur_zarbi.applescript
-- Created by AlbertLevert / pmac on 03.12.06.
-- Copyright 2006 Puissance Mac - Switzerland. All rights reserved.
-- PostcardWare, if you use this Applescript, send me a postcard !

property lesChiffre : {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
property LesMinusCule : {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
property LesMaj : {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}


property LargeurouHAuteurMaxi_1 : 800
property LargeurouHAuteurMaxi_2 : 150
property PdfFile : "Document PDF"
property JPGFile : "Image JPEG"
property AVIFile : "Film AVI"
property MovFile : "Séquence QuickTime"
property vMwFile : "Windows Media Audio/Video File"
property The3GpFile : "Séquence 3GPP"
property JPGFilePS : "Fichier JPEG Adobe Photoshop"

property DossierThumbnails : "thumbnail"



property LaPageIndex : "<html>
    <head>
        <title>PhpWebGallery</title>
    </head>
    <body>
        No access authorized
    </body>
</html>"



on run
    set leTexteIndex to ""
    set leDossierAconvertir to choose folder
    set leBureau to (path to desktop folder) as string
    
    display dialog "Nom du nouveau dossier" default answer "test" buttons {"Ok", "Annuler"} default button 1
    copy the result as list to {text_returned, button_pressed}
    
    if button_pressed = "Annuler" then
        return
    end if
    
    set DossierDest to text_returned
    log DossierDest
    
    
    set MonDossierArrivee to leBureau
    set MonDossierArrivee2 to MonDossierArrivee & DossierDest
    tell application "Finder"
        
        set leNbFichierDuDossier to (count of every file of folder leDossierAconvertir)
        set leNbDossier to (count of every folder of folder leDossierAconvertir)
        
        try
            make new folder at folder MonDossierArrivee with properties {name:DossierDest}
        on error
            log "Le dossier " & DossierDest & " existe déja !"
            return
        end try
        
        if leNbFichierDuDossier > 0 then
            try
                make new folder at folder MonDossierArrivee2 with properties {name:DossierThumbnails}
            on error
                --log "Le dossier DossierThumbnails existe déja !"
            end try
        end if
    end tell
    
    
    if leNbFichierDuDossier = 0 and leNbDossier = 0 then
        beep
        display dialog "Aucun fichier  et/ou dossier trouvé !" buttons {"ok"} giving up after 5
        return
    end if
    
    
    --Traite les images du dossier
    if leNbFichierDuDossier > 0 then
        set TheDossierAconvertir to leDossierAconvertir
        TraitementUnDossier(TheDossierAconvertir, leNbFichierDuDossier, MonDossierArrivee2)
    end if
    
    
    -- Traite les sousDossiers
    if leNbDossier > 0 then
        set NbDossierATraiter to leNbDossier
        set LeSousDossAtraiter to leDossierAconvertir
        set DossierDestSD to leBureau & DossierDest
        TraiteLesSousDossier(LeSousDossAtraiter, NbDossierATraiter, DossierDestSD)
    end if
    
end run


on TraiteLesSousDossier(leDossierAconvertir, leNbDossier, DossierDest)
    tell application "Finder" to set LesNomDossier to (name of every folder of folder leDossierAconvertir)
    
    repeat with xx from 1 to leNbDossier
        set LEDossierTmp to (item xx of LesNomDossier) as string
        set TheDossierAconvertir to alias ((leDossierAconvertir & LEDossierTmp) as string)
        
        tell application "Finder"
            set leNbFichierDuDossier to (count of every file of folder TheDossierAconvertir)
            set leNbDossierSD to (count of every folder of folder TheDossierAconvertir)
        end tell
        
        if leNbFichierDuDossier > 0 then
            set MonDossierArrivee to DossierDest as string
            
            if last character of MonDossierArrivee = ":" then
                set MonDossierArrivee2 to MonDossierArrivee & LEDossierTmp
            else
                set MonDossierArrivee2 to MonDossierArrivee & ":" & LEDossierTmp
            end if
            
            tell application "Finder"
                try
                    make new folder at folder MonDossierArrivee with properties {name:LEDossierTmp}
                on error
                    --log "Le dossier " & LEDossierTmp & " existe déja !"
                end try
                try
                    make new folder at folder MonDossierArrivee2 with properties {name:DossierThumbnails}
                on error
                    --log "Erreur Le dossier DossierThumbnails existe déja ! = " & MonDossierArrivee2
                end try
            end tell
            TraitementUnDossier(TheDossierAconvertir, leNbFichierDuDossier, MonDossierArrivee2)
        end if
        
        if leNbDossierSD > 0 then
            set NbDossierATraiter to leNbDossierSD
            set LeSousDossAtraiter to alias ((leDossierAconvertir & LEDossierTmp) as string)
            set DossierDestSD to alias ((DossierDest & ":" & LEDossierTmp) as string)
            TraiteLesSousDossier(LeSousDossAtraiter, NbDossierATraiter, DossierDestSD)
        end if
        
    end repeat
    
end TraiteLesSousDossier







on TraitementUnDossier(leDossierAconvertir, leNbFichier, MonDossierArriveeTmp)
    
    set LesTypeImgListe to {JPGFile, JPGFilePS}
    set LesTypeDocListe to {AVIFile, MovFile, vMwFile, PdfFile, The3GpFile}
    
    
tell application "Finder"
    set selectedFiles to (select every file of folder leDossierAconvertir)
    close folder leDossierAconvertir
end tell
    
    repeat with xx from 1 to leNbFichier
        
        --Generateur de Nom zarbi :D
        set UnTruc to ""
        if leNbFichier = 1 then
            set aFile to selectedFiles as string
        else
            set aFile to item xx of selectedFiles
            set aFile to aFile as string
        end if
        tell application "Finder"
            set leNom to name of (alias aFile)
            set leType to kind of (alias aFile)
            set LExtension to name extension of (alias aFile)
        end tell
        
        set LongExt to (count of character of LExtension) + 1
        log "leType = " & leType
        set lettres_chifres to some item of LesMinusCule & some item of LesMaj & (random number from 10 to 99)
        set nbrList to {}
        repeat until (count UnTruc) = 4 --mélange majuscule, minuscule et les deux chiffres
            tell (random number from 1 to 4) to if it is not in nbrList then
                set UnTruc to UnTruc & item it of lettres_chifres
                set end of nbrList to it
            end if
        end repeat
        
        set leNomLong to (count of character of (leNom as string))
        set Nombase to text 1 thru (leNomLong - LongExt) of leNom
        set LExtension to text from (leNomLong - (LongExt - 1)) to leNomLong of leNom
        set NomFinal to Nombase & "_" & UnTruc & LExtension
        
        set NomFinal to my searchReplaceText(" ", "_", NomFinal)
        
        set NouvelleImage_1 to (MonDossierArriveeTmp & ":" & NomFinal) as string
        set NouvelleImage_2 to (MonDossierArriveeTmp & ":" & DossierThumbnails & ":" & NomFinal) as string
        
        
        
        if leType is in LesTypeImgListe then
            -- traitement des images
            tell application "Image Events"
                launch
                set Image1 to open aFile
                set Image2 to Image1
                scale Image1 to size LargeurouHAuteurMaxi_1
                save Image1 in NouvelleImage_1
                scale Image1 to size LargeurouHAuteurMaxi_2
                save Image2 in NouvelleImage_2
                close Image1
            end tell
        else if leType is in LesTypeDocListe then
            --traitments des autres doc
            set lePathUnixOrig to quoted form of POSIX path of aFile
            set lePathUnixNormal to quoted form of POSIX path of NouvelleImage_1
            set lePathUnixThunbail to quoted form of POSIX path of NouvelleImage_2
            set leScriptCopie1 to "cp " & lePathUnixOrig & " " & lePathUnixNormal
            set leScriptCopie2 to "cp " & lePathUnixOrig & " " & lePathUnixThunbail
            
            do shell script leScriptCopie1
            do shell script leScriptCopie2
        end if
    end repeat
    
    -- creation du fichier index.html
    set LePathIndex to MonDossierArriveeTmp & ":index.html"
    set LePathIndex2 to MonDossierArriveeTmp & ":" & DossierThumbnails & ":index.html"
    
    write_to_file(LaPageIndex, LePathIndex)
    write_to_file(LaPageIndex, LePathIndex2)
end TraitementUnDossier






on write_to_file(this_data, target_file_path)
    try
        set the target_file_path to the target_file_path as text
        set the open_target_file to open for access the file target_file_path with write permission
        --set eof of the open_target_file to 0 --> remove this line if you want written data appended to existing data
        write this_data to the open_target_file starting at eof
        close access the open_target_file
        return true
    on error
        try
            close access the open_target_file
        end try
        return false
    end try
end write_to_file




on searchReplaceText(searchTerm, replaceTerm, theText)
    set oldTID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to searchTerm
    set theText to theText's text items
    set AppleScript's text item delimiters to replaceTerm
    set theText to theText as text
    set AppleScript's text item delimiters to oldTID
    return theText
end searchReplaceText


Ce message a été modifié par AlbertLevert - 4 Dec 2006, 19:43.


--------------------
G4 2x1250, 1024, SoundStick, iSight, 22" LaCie, 20" Sony
Go to the top of the page
 
+Quote Post

Les messages de ce sujet
- Pompon   [Résolu] Insérer lettres et nombres aléatoires dans des noms de fichier   21 Aug 2006, 23:29
- - p1ngu01   salut, tu pourrais utiliser le code ascii pour les...   21 Aug 2006, 23:39
- - Pompon   Ok pour le code ASCII p1ngu01... On ne peut pas l...   21 Aug 2006, 23:52
|- - p1ngu01   CITATION(Pompon @ 22 Aug 2006, 00:52) 184...   22 Aug 2006, 00:15
- - AlbertLevert   Et comme ceci ? CODEproperty lesChiffre : {...   22 Aug 2006, 03:21
|- - baron   [H.S.] Hormis la beauté du geste, je peux te dema...   22 Aug 2006, 04:36
- - Pompon   Vous êtes vraiment très efficaces messieurs les ...   22 Aug 2006, 11:15
- - AlbertLevert   Voila une version plus fonctionnelle ... CITATION...   22 Aug 2006, 12:33
- - Pompon   Yes ça marche... mais si je sélectionne plusieur...   22 Aug 2006, 12:45
- - AlbertLevert   Oooops, il faut mettre la ligne CODEset UnTruc t...   22 Aug 2006, 13:18
- - Pompon   Parfait merci beaucoup AlbertLevert, c'est par...   22 Aug 2006, 14:13
- - Pompon   Bon je crois que je vais avoir de nouveau besoin d...   22 Aug 2006, 17:51
- - AlbertLevert   Encore une ? (de variante) il te reste juste a ch...   22 Aug 2006, 19:55
- - p1ngu01   salut, ou encore en faisant (en une ligne) : set ...   22 Aug 2006, 22:30
- - Pompon   Encore merci AlbertLevert, ton script fonctionne ...   22 Aug 2006, 22:43
|- - AlbertLevert   CITATION(Pompon @ 22 Aug 2006, 23:43) 184...   23 Aug 2006, 02:23
- - Pompon   Salut AlbertLevert le nocturne, Pourquoi Automato...   23 Aug 2006, 20:31
- - pardon   sinon, tu peux aussi essayer ce script shell : COD...   24 Aug 2006, 02:12
- - AlbertLevert   CODE sips -z 600 800 --out "../$nom...   24 Aug 2006, 09:37
- - pardon   bah, c'est comme tu veux je suppose que les i...   24 Aug 2006, 12:14
- - Pompon   Merci de ta contribution pardon ! Pour les ph...   24 Aug 2006, 21:46
- - pardon   euh, si si ça fait bien aussi ce que tu demandai...   25 Aug 2006, 02:15
- - Pompon   Re, Je reviens sur ce post pour re-re-re-demander...   31 Aug 2006, 17:25
|- - JackJr300   Salut CITATION(Pompon @ 31 Aug 2006, 17...   31 Aug 2006, 17:44
- - Pompon   Merci beaucoup JackJr300 c'est tout bon ! ...   31 Aug 2006, 18:08
- - Pompon   ... J'ai encore une question Est-il possi...   1 Sep 2006, 19:28
- - p1ngu01   salut, ouais, il suffirait de faire une seule list...   1 Sep 2006, 20:58
|- - JackJr300   Bonsoir Ceci mélange une majuscule, une minu...   1 Sep 2006, 21:38
- - Pompon   Salut p1ngu01, Faire des "doublons de type...   1 Sep 2006, 21:33
- - p1ngu01   ah ben, il suffit de faire une liste comme ca : p...   1 Sep 2006, 21:41
- - Pompon   Merci pour vos astuces ! La solution de p1ngu...   1 Sep 2006, 22:50
- - Pompon   Merci JackJr300 ta modif fonctionne à merveille ...   2 Sep 2006, 09:45
- - Pompon   Toc toc toc me revoilà Salut à vous ! B...   3 Dec 2006, 17:25
|- - AlbertLevert   CITATION(Pompon @ 3 Dec 2006, 17:25) 2005...   4 Dec 2006, 00:54
- - AlbertLevert   Voila déja une version pour créer le fichier ind...   4 Dec 2006, 01:09
- - Pompon   Salut AlbertLevert, Merci pour ton aide !...   4 Dec 2006, 12:58
|- - AlbertLevert   CITATION(Pompon @ 4 Dec 2006, 12:58) 2006...   4 Dec 2006, 16:29
- - Pompon   Ok tout marche parfaitement, merci AlbertLevert ...   4 Dec 2006, 19:10
- - AlbertLevert   Y'avait un bug ! les fichiers Photoshop p...   4 Dec 2006, 19:25
- - Pompon   Je n'avais pas vu le problème avec les fichie...   5 Dec 2006, 00:14
|- - AlbertLevert   CITATION(Pompon @ 5 Dec 2006, 00:14) 2007...   6 Dec 2006, 02:09
- - Pompon   Salut AlbertLevert, Désolé pour le retard. J...   8 Dec 2006, 17:32
|- - AlbertLevert   CITATION(Pompon @ 8 Dec 2006, 17:32) 2013...   11 Dec 2006, 01:38
- - Pompon   Merci AlbertLevert, Toujours aussi efficace et au...   12 Dec 2006, 00:48
|- - AlbertLevert   CITATION(Pompon @ 12 Dec 2006, 00:48) 201...   12 Dec 2006, 06:14
- - Pompon   Salut, Je viens de refaire un test de ta nouvelle...   12 Dec 2006, 09:39
|- - AlbertLevert   CITATION(Pompon @ 12 Dec 2006, 09:39) 201...   12 Dec 2006, 16:55
- - Pompon   Ok je viens de tester cette nouvelle version (la 1...   13 Dec 2006, 12:36
|- - AlbertLevert   CITATION(Pompon @ 13 Dec 2006, 12:36) 201...   14 Dec 2006, 04:38
- - Pompon   Youpi encore une nouvelle version avec plein de co...   14 Dec 2006, 11:36
|- - AlbertLevert   CITATION(Pompon @ 14 Dec 2006, 11:36) 202...   14 Dec 2006, 12:19
- - Pompon   Oups, je viens de faire un edit de mon post préc�...   14 Dec 2006, 12:38
|- - AlbertLevert   CITATION(Pompon @ 14 Dec 2006, 12:38) 202...   15 Dec 2006, 12:48
- - Pompon   Que d'évolutions en perspective ! Il va f...   17 Dec 2006, 01:30
|- - AlbertLevert   CITATION(Pompon @ 17 Dec 2006, 01:30) 202...   18 Dec 2006, 02:34
- - Pompon   De mon côté, la seule fenêtre que je peux redim...   18 Dec 2006, 19:54
|- - AlbertLevert   CITATION(Pompon @ 18 Dec 2006, 19:54) 202...   18 Dec 2006, 21:59
- - Pompon   Un nouveau cru, youpi ! Au début j'ai eu...   19 Dec 2006, 12:45
|- - AlbertLevert   CITATION(Pompon @ 19 Dec 2006, 12:45) 202...   19 Dec 2006, 16:24
- - Pompon   Effectivement le problème avec les sous-dossiers ...   20 Dec 2006, 01:20
|- - AlbertLevert   CITATION(Pompon @ 20 Dec 2006, 01:20) 202...   31 Dec 2006, 07:31
- - Pompon   Salut AlbertLevert, Merci pour ton travail. Je va...   2 Jan 2007, 22:29
- - AlbertLevert   Salut, surement du au nombres. si tu as le temps...   3 Jan 2007, 03:15
- - Pompon   Je n'ai pas pu télécharger la version a10, e...   6 Jan 2007, 19:37
|- - AlbertLevert   CITATION(Pompon @ 6 Jan 2007, 19:37) 2047...   6 Jan 2007, 23:39
- - Pompon   Merci, j'ai pu tester la version 1.0a10. Tout ...   7 Jan 2007, 14:28
|- - AlbertLevert   CITATION(Pompon @ 7 Jan 2007, 14:28) 2048...   7 Jan 2007, 17:28
- - Pompon   Un petit retour d'utilisation et quelques idé...   8 Jan 2007, 17:16
|- - AlbertLevert   CITATION(Pompon @ 8 Jan 2007, 17:16) 2050...   8 Jan 2007, 21:27
- - Pompon   Salut AlbertLevert, J'ai vu que tu avais modi...   24 Jan 2007, 17:54
- - Pompon   Salut AlbertLevert, J'ai vu que tu avais remi...   8 Mar 2007, 23:54
- - ArtiShow   Bonjour, cet utilitaire à l'air de faire exa...   23 Jun 2008, 16:43


Reply to this topicStart new topic
1 utilisateur(s) sur ce sujet (1 invité(s) et 0 utilisateur(s) anonyme(s))
0 membre(s) :

 



Nous sommes le : 7th December 2025 - 19:45