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
Sujet: Re: [AS] Obtenir un chemin POSIX d'une photo de la bibliothèque "Photos"
Merci de ce retour.
Je ne comprenais pas ce qui faisait problème avec les espaces.

Si le nombre de photos à problème n'est pas trop élevé vous pouvez corriger leurs privilèges mais a priori, reconstruire la photothèque ne coute guère mais n'oubliez pas qu'Apple conseille de la sauvegarder avant de tenter de la reconstruire

Comme la bibliothèque peut contenir un grand nombre de clichés je n'utilise pas le Finder (que de toutes façons je déteste) pour lister ces derniers.

-- début du script

----------------------------------------------------------------
use AppleScript version "2.5"
use framework "Foundation"
--use script "Metadata Lib" version "2.0.0"
use scripting additions
----------------------------------------------------------------


on listFolder:targetDir
    
    set posixPath to POSIX path of targetDir
    set off7 to (count posixPath) + 1
    set theFolderURL to current application's NSURL's fileURLWithPath:posixPath

    
    set NSDirectoryEnumerationSkipsHiddenFiles to a reference to 4

    
    set NSFileManager to a reference to current application's NSFileManager's defaultManager()

    
    --    set {theURLs, theError} to NSFileManager's defaultManager()'s contentsOfDirectoryAtURL:targetDir includingPropertiesForKeys:{} options:(NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
    set typeIdentifierKey to current application's NSURLTypeIdentifierKey
    set keysToRequest to current application's NSArray's arrayWithObject:(typeIdentifierKey)
    set theURLs to (NSFileManager's enumeratorAtURL:theFolderURL includingPropertiesForKeys:keysToRequest options:6 errorHandler:(missing value))'s allObjects()

    
    if theURLs is equal to missing value then error (theError's localizedDescription() as text)

    
    --set theURLs to theURLs's allObjects()

    
    set sortDesc to current application's NSSortDescriptor's sortDescriptorWithKey:"path" ascending:true selector:"localizedStandardCompare:" -- Finder-style sorting

    
    set theURLs to theURLs's sortedArrayUsingDescriptors:{sortDesc}

    
    set theDescriptors to {}

    
    repeat with aURL in theURLs
        set itsPath to (aURL's valueForKey:"path") as text
        set {theResult, theValue, theError} to (aURL's getResourceValue:(specifier) forKey:(current application's NSURLIsDirectoryKey) |error|:(specifier))
        if theResult as boolean is false then error (theError's |localizedDescription|() as text) number (theError's code() as integer)

        
        
        set stripped to text off7 thru -1 of itsPath
        if theValue as boolean then
            -- c'est un dossier, on s'en fout
        else
            try
                set itsAttributes to do shell script "stat -F " & quoted form of itsPath
                set itsAttributes to text 1 thru 12 of itsAttributes
            on error
                set itsAttributes to "????"
            end try

            
            --    set end of theDescriptors to itsPath & tab & itsAttributes
            set end of theDescriptors to stripped & tab & itsAttributes

            
        end if

        
    end repeat
    return theDescriptors
end listFolder:


set targetDir to (path to pictures folder as text) & "Photos Library.photoslibrary:Masters:" as alias
set theDescriptors to my listFolder:targetDir

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

my writeto(p2d & "Clichés " & space & dateStamp & ".txt", my recolle(theDescriptors, linefeed), «class utf8», false)

#=====

on recolle(l, d)
    local oTIDs, t
    set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
    set t to l as text
    set AppleScript's text item delimiters to oTIDs
    return t
end recolle


#=====
(*
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

Le contenu du fichier créé ressemble à :

2014/07/16/20140716-175539/DSC_0160.NEF    -rwxrwxrwx 2
2014/08/07/20140807-102118/DSC_0165.NEF    -rwxrwxrwx 2
2015/10/10/20151010-200530/P1010002.JPG    -rw-r--r-- 1
2015/10/10/20151010-200530/P1010003.JPG    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0010.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0011.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0012.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0013.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0014.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0015.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0016.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0019.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0021.NEF    -rw-r--r-- 1
2015/10/10/20151010-200856/DSC_0022.NEF    -rw-r--r-- 1


-rwxrwxrwx signifie :
(Moi) Lecture et écriture
Staff Lecture et écriture
everyone Lecture et écriture

-rw-r--r-- signifie :
(Moi) Lecture et écriture
Staff Lecture seulement
everyone Lecture seulement

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 1 juillet 2019 18:58:33

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