Re: [AS] Obtenir un chemin POSIX d'une photo de la bibliot…

Page principale
Supprimer ce message
Répondre à ce message
Auteur: Yvan KOENIG
Date:  
À: Liste AppleScript francophone
Nouveaux-sujets: [AS] [pas AS] messages reçus dans Indésirables
Sujet: Re: [AS] Obtenir un chemin POSIX d'une photo de la bibliothèque "Photos"
Je vous dois des excuses car j'ai laissé deux instructions erronées dans mon envoi précédent.

Les instructions correctes sont :

set itsAttributes to text 1 thru 10 of itsAttributes
set itsAttributes2 to text 1 thru 10 of itsAttributes2

J'avais laissé 12 au lieu de 10.

Vous trouverez ci-dessous une version qui utilise des routines "standard" afin de ne traiter que les fichiers image ("public.image").

-- début du script
----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
--use script "Metadata Lib" version "2.0.0"
use scripting additions
----------------------------------------------------------------
property recursiveScan : true
# true = scan subfolders of the selected folder
# false = doesn't scan the subfolders of the selected folder

property dataMode : 0
# -1 means return relative path
# 0 mean return paths
# 1 means return names
# 2 means return path and name separated by tab character
# 3 means return relative container and filename separated by tab character

script o
    property rootLength : 0
    property delim : tab
end script


on run
    my germaine()
end run


#=====

on germaine()
    set sourceFolder to POSIX path of ((path to desktop as text) & "dossier test:") -- utilisé pour tester
    --set sourceFolder to POSIX path of ((path to pictures folder as text) & "Photos Library.photoslibrary:Masters:")
    # crée une liste des POSIX paths
    set theFileInfo to my listFilesAndUTIsIn:sourceFolder
    # élimine tout ce qui n'est pas fichier image
    set thePictures to my filterAnArray:theFileInfo conformingTo:"public.image"
    set theDescriptors to my scanPictures:thePictures

    
    set dateStamp to my dateTimeStamp()
    set p2d to path to desktop as text

    
    my writeto(p2d & "Clichés " & dateStamp & ".txt", my concatList:theDescriptors usingString:linefeed, «class utf8», false)
end germaine


#=====

on scanPictures:thePictures
    # On ne reçoit que des fichiers images, il n'y a plus lieu de filter les dossiers
    set theDescriptors to {}

    
    repeat with itsPath in thePictures
        set aURL to (current application's |NSURL|'s fileURLWithPath:itsPath)

        
        set stripped to text (o's rootLength) thru -1 of itsPath
        set itsAttributes2 to ""

        
        try
            set itsAttributes to do shell script "stat -F " & quoted form of itsPath
            set itsAttributes to text 1 thru 10 of itsAttributes
        on error
            set itsAttributes to "????"
        end try

        
        if itsAttributes is not "-rw-r--r--" then # Adapter le test à ce que vous voulez comme permissions
            -- "-rwxr-xr-x" pour chmod 755
            -- "-rwxrwxrwx" pour chmod 777

            
            do shell script "chmod 644 " & quoted form of itsPath with administrator privileges # Adapter à ce que vous voulez comme permissions. Le mot de passe ne sera demandé qu'une fois
            set itsAttributes2 to do shell script "stat -F " & quoted form of itsPath
            set itsAttributes2 to text 1 thru 10 of itsAttributes2
        end if
        --    set end of theDescriptors to itsPath & tab & itsAttributes
        if itsAttributes2 is not "" then
            set end of theDescriptors to stripped & tab & itsAttributes & tab & itsAttributes2
        else
            set end of theDescriptors to stripped & tab & itsAttributes
        end if

        
    end repeat
    return theDescriptors
end scanPictures:


#=====

on listFilesAndUTIsIn:sourceFolder
    set fileManager to current application's NSFileManager's defaultManager()
    set aURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
    set (o's rootLength) to (aURL's |path|()'s |length|()) + 1 -- use to trim paths
    if recursiveScan then
        set theOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
        set theEnumerator to fileManager's enumeratorAtURL:aURL includingPropertiesForKeys:{} options:theOptions errorHandler:(missing value)
        set directoryContents to theEnumerator's allObjects()
    else
        set directoryContents to fileManager's contentsOfDirectoryAtURL:aURL includingPropertiesForKeys:{} options:0 |error|:(missing value)
    end if
    set tempArray to current application's NSMutableArray's arrayWithCapacity:(directoryContents's |count|()) -- array to hold values

    
    repeat with aURL in directoryContents
        set {thePictures, theUTI} to (aURL's getResourceValue:(reference) forKey:(current application's NSURLTypeIdentifierKey) |error|:(missing value))
        (tempArray's addObject:(current application's NSDictionary's dictionaryWithObjects:{aURL, theUTI} forKeys:{"theURL", "theUTI"}))
    end repeat

    
    return tempArray
end listFilesAndUTIsIn:


#=====

on filterAnArray:tempArray conformingTo:someUTI
    set thePredicate to current application's NSPredicate's predicateWithFormat_("theUTI UTI-CONFORMS-TO %@", someUTI)
    set foundItemList to (tempArray's filteredArrayUsingPredicate:thePredicate)'s valueForKey:"theURL"

    
    if dataMode = 0 then
        # Return as a list of POSIX Paths
        set foundItemList to (foundItemList's valueForKey:"path") as list
    else
        set newArray to current application's NSMutableArray's array() -- to store names

        
        set theCount to foundItemList's |count|()
        if dataMode = -1 then
            repeat with i from 1 to theCount
                set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
                set theRelative to (oneURL's |path|()'s substringFromIndex:(o's rootLength))
                (newArray's addObject:("…/" & theRelative))
            end repeat
        else if dataMode = 1 then
            # Return a list of fileNames
            repeat with i from 1 to theCount
                set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
                set theName to oneURL's lastPathComponent()
                (newArray's addObject:theName)
            end repeat
        else if dataMode = 2 then
            # Return a list of couples posix path of folder containing the file and filename separated by a tab character (or a comma)
            repeat with i from 1 to theCount
                set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
                set theName to oneURL's lastPathComponent()
                set theContainer to oneURL's URLByDeletingLastPathComponent()
                --set theContainer to theContainer's |path|() as text
                (newArray's addObject:((theContainer's |path|() as text) & (o's delim) & theName))
            end repeat
        else
            # Return a list of couples posix path of folder containing the file and filename separated by a tab character
            repeat with i from 1 to theCount
                set oneURL to (foundItemList's objectAtIndex:(i - 1)) -- zero-based indexes
                set theRelative to (oneURL's |path|()'s substringFromIndex:(o's rootLength))
                set theName to theRelative's lastPathComponent()
                set theRelativeContainer to theRelative's stringByDeletingLastPathComponent()
                (newArray's addObject:("…/" & theRelativeContainer & (o's delim) & theName))
            end repeat
        end if
        set foundItemList to newArray as list
    end if

    
    return foundItemList
end filterAnArray:conformingTo:


#=====

on concatList:theList usingString:d1
    set anArray to current application's NSArray's arrayWithArray:theList
    return (anArray's componentsJoinedByString:d1) as text
end concatList:usingString:



#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
    -- targetFile is the path to the file you want to write
    -- theData is the data you want in the file.
    -- dataType is the data type of theData and it can be text, list, record etc.
    -- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
    try
        set targetFile to targetFile as «class furl»
        set openFile to open for access targetFile with write permission
        if not apendData then set eof of openFile to 0
        write theData to openFile starting at eof as dataType
        close access openFile
        return true
    on error
        try
            close access targetFile
        end try
        return false
    end try
end writeto


#=====

on dateTimeStamp()
    tell (current date) to return (((its year) * 10000 + (its month) * 100 + (its day)) as text)

    
end dateTimeStamp

#=====
-- fin du script

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 2 juillet 2019 10:33:36


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