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
Pour tester votre script je l'ai modifié pour utiliser Numbers.

Vous trouverez ci-dessous, le script ainsi modifié et l'historique créé lors de son exécution.
J'espère que ça vous permettra d'y voir plus clair.

[Script]
set fileName to ((path to desktop as string) & "abcd.numbers") as alias

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:"
        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
            set NewFileName to folderName & "-" & NouveauTexte & "." & lextension
            log NewFileName
            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
            log chemin
            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*)
            if my testPresence(p2f) then
                set result to "Présent" -- traitement si fichier présent
                set NewFileName to folderName & "-" & NouveauTexte & "-1" & "." & lextension
                log NewFileName
                set chemin2 to chemin & ":" & NewFileName
                log chemin2 --> 
                set value of cell "B7" to chemin2 --pour test            
            else
                set result to "Absent" -- traitement si fichier absent
                --do shell script "mv -n " & quoted form of POSIX path of theFilePath & space & quoted form of POSIX path of chemin2
            end if
            do shell script "mv -n " & quoted form of POSIX path of theFilePath & space & quoted form of POSIX path of chemin2
        end tell -- requis par "Numbers"
    end tell
end openThat



on testPresence(p2f)
    try
        do shell script "ls " & quoted form of POSIX path of theFilePath & space & quoted form of POSIX path of chemin2
        return true
    on error
        return false
    end try
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 "0AB06362-8A80-49E5-82E8-58422BF0F329"
    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*)
    (*SSD 1000:Users:**********:Desktop:Desktop-2021-04-10.numbers*)
    do shell script "mv -n '/Users/**********/Desktop/abcd.numbers' '/Users/**********/Desktop/Desktop-2021-04-10.numbers'"
        --> error number -10004
end tell
tell current application
    do shell script "mv -n '/Users/**********/Desktop/abcd.numbers' '/Users/**********/Desktop/Desktop-2021-04-10.numbers'"
        --> ""
end tell
Résultat :
""
[/historique]


La méthode utilisée pour tester l'éventuelle existence d'un document portant déjà le nouveau nom ne me plaisant pas du tout je vais éditer toute cette partie ainsi que d'autres parties du code.

Yvan KOENIG (VALLAURIS, France) vendredi 7 mai 2021 14:39:45
_______________________________________________
Applescript_fr mailing list
Applescript_fr@???
http://listes.patpro.net/mailman/listinfo/applescript_fr