Re: [AS] Un script pour renommer un fichier

Page principale
Supprimer ce message
Répondre à ce message
Auteur: Yvan KOENIG via Applescript_fr
Date:  
À: Liste AppleScript francophone
CC: Yvan KOENIG
Sujet: Re: [AS] Un script pour renommer un fichier
Voila le même script qui cette fois incrémente le nouveau nom autant que nécessaire.

[script]
set fileName to ((path to desktop as string) & "abcd.numbers") as alias --> alias "SSD 1000:Users:**********:Desktop:abcd.numbers"

my openThat(fileName)

on openThat(fileName) -- vérification de la nature du paramètre
    if class of fileName is list then set fileName to first item of fileName
    -- set filePath to fileName as text -- retourne le chemin d'accès Hfs
    tell application "Finder"
        set theFileName to name of fileName --> "abcd.numbers"
        set theFilePath to fileName as string --> "SSD 1000:Users:**********:Desktop:abcd.numbers"
        set lextension to name extension of fileName --> "numbers"
        set nomcourt to text 1 thru ((length of theFileName) - (1 + (length of lextension))) of theFileName
        log result --> "abcd"
        set folderPath to folder of fileName as alias --> alias "SSD 1000:Users:**********:Desktop:"
        set folderName to name of folderPath --> "Desktop"
    end tell
    tell application "Numbers" --"Microsoft Excel"
        activate
        open fileName -- filename est un alias
        tell document 1 to tell sheet 1 to tell table 1 -- requis par "Numbers"
            set theDate to (value of cell "H2") as date -- retourne une date Applescript conforme aux réglages système
            set theYear to year of theDate as string -- je suppose que l'année est sur quatre chiffres
            if theYear < 100 then set theYear to theYear + 2000 -- suppose que la date relève du XXIème siècle
            set theYear to theYear as string
            set theMonth to text 2 thru 3 of ((100 + (month of theDate as number)) as string) -- on aura toujours deux chiffres
            set theDay to text 2 thru 3 of ((100 + (day of theDate)) as string) -- on aura toujours deux chiffres
            set NouveauTexte to theYear & "-" & theMonth & "-" & theDay -- crée une chaîne, inutile de forcer cette nature
            log NouveauTexte --> (*2021-04-10*)
            set NewFileName to folderName & "-" & NouveauTexte & "." & lextension
            log NewFileName --> (*Desktop-2021-04-10.numbers*)
            (*
            -- inutile, l'extration a été effectuée dans le bloc Finder
            set L to length of theFilePath
            set R to 0 as integer
            repeat with i from 1 to L
                if character i of theFilePath = ":" then
                    if R < i then
                        set R to i
                    end if
                end if
            end repeat
            set R to R - 1
            set chemin to text from character 1 to R of theFilePath
            *)
            set chemin to folderPath as text
            log chemin --> (*SSD 1000:Users:**********:Desktop:*)
            set chemin2 to chemin & NewFileName
            log chemin2 --> (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10.numbers*)
            --    set p2f to chemin2
            --    log p2f --> (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10.numbers*)

            
            set knt to 0
            repeat 5 times
                if my testPresence(chemin2) then
                    -- traitement si fichier présent
                    --set result to "Présent" 
                    set knt to knt + 1
                    set NewFileName to folderName & "-" & NouveauTexte & "-" & knt & "." & lextension
                    log NewFileName --> (*Desktop-2021-04-10-1.numbers*)
                    set chemin2 to chemin & NewFileName
                    log chemin2 --> (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-1.numbers*)
                    set value of cell "B7" to chemin2 --pour test    
                else
                    -- traitement si fichier absent
                    --set result to "Absent" 
                    exit repeat
                end if
            end repeat
            tell application "Finder" to set name of fileName to NewFileName
        end tell -- requis par "Numbers"
    end tell
end openThat


on testPresence(p2f)
    tell application "Finder"
        return (exists file p2f)
    end tell
end testPresence
[/script]


[historique]
tell current application
    path to desktop as string
        --> "SSD 1000:Users:**********:Desktop:"
end tell
tell application "Finder"
    get name of alias "SSD 1000:Users:**********:Desktop:abcd.numbers"
        --> "abcd.numbers"
    get name extension of alias "SSD 1000:Users:**********:Desktop:abcd.numbers"
        --> "numbers"
    (*abcd*)
    get folder of alias "SSD 1000:Users:**********:Desktop:abcd.numbers"
        --> alias "SSD 1000:Users:**********:Desktop:"
    get name of alias "SSD 1000:Users:**********:Desktop:"
        --> "Desktop"
end tell
tell application "Numbers"
    activate
    open alias "SSD 1000:Users:**********:Desktop:abcd.numbers"
        --> document id "2BC7A1D7-F613-4FCA-8552-53727884C43E"
    get value of cell "H2" of table 1 of sheet 1 of document 1
        --> date "samedi 10 avril 2021 à 00:00:00"
    (*2021-04-10*)
    (*Desktop-2021-04-10.numbers*)
    (*SSD 1000:Users:**********:Desktop:*)
    (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10.numbers*)
end tell
tell application "Finder"
    exists file "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10.numbers"
        --> true
end tell
tell application "Numbers"
    (*Desktop-2021-04-10-1.numbers*)
    (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-1.numbers*)
    set value of cell "B7" of table 1 of sheet 1 of document 1 to "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-1.numbers"
end tell
tell application "Finder"
    exists file "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-1.numbers"
        --> true
end tell
tell application "Numbers"
    (*Desktop-2021-04-10-2.numbers*)
    (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-2.numbers*)
    set value of cell "B7" of table 1 of sheet 1 of document 1 to "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-2.numbers"
end tell
tell application "Finder"
    exists file "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-2.numbers"
        --> true
end tell
tell application "Numbers"
    (*Desktop-2021-04-10-3.numbers*)
    (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-3.numbers*)
    set value of cell "B7" of table 1 of sheet 1 of document 1 to "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-3.numbers"
end tell
tell application "Finder"
    exists file "SSD 1000:Users:**********:Desktop:Desktop-2021-04-10-3.numbers"
        --> false
    set name of alias "SSD 1000:Users:**********:Desktop:abcd.numbers" to "Desktop-2021-04-10-3.numbers"
        --> "Desktop-2021-04-10-3.numbers"
end tell
Résultat :
"Desktop-2021-04-10-3.numbers"


[/historique]

Yvan KOENIG (VALLAURIS, France) vendredi 7 mai 2021 19:27:03

_______________________________________________
Applescript_fr mailing list
Applescript_fr@???
http://listes.patpro.net/mailman/listinfo/applescript_fr