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
Oops, j'ai posté une mauvaise copie du script.

Voilà la bonne.
[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 --> (*2021-04-10*)
            set NewFileName to folderName & "-" & NouveauTexte & "." & lextension
            log NewFileName --> (*Desktop-2021-04-10.numbers*)
            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 --> (*SSD 1000:Users:yvankoenig: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*)
            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]


Yvan KOENIG (VALLAURIS, France) vendredi 7 mai 2021 14:45:59

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