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
Version modifiée selon mes préférences.
Je suppose que vous êtes en mesure d'effectuer le retour vers Excel.
Vous avez sans doute remarqué que j'ai du modifier le nom de la routine.
Applescript refusait d'utiliser une routine nommée open.

[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*)

            
            
            if my testPresence(p2f) then
                set result to "Présent" -- traitement si fichier présent
                set NewFileName to folderName & "-" & NouveauTexte & "-1" & "." & 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
                set result to "Absent" -- traitement si fichier absent
            end if
            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]


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