SSS37

Q: Applescript to get physical path of an image in Yosemite photos app

In iPhoto we had that nice XML file with the metadata for each image in iPhoto.

 

In the Yosemite Photos app, there is no such file.  So, I wrote an Applescript that loops through each and every folder, subfolder, album, etc. and write the details to an XML file.

 

When the code loops, I have:

     repeat with img in every media item of myContainer

and I get these details:

 

  • filename: 2011-07-10 020.JPG
  • keywords: 2011-07-10
  • name: missing value
  • description:                               
  • favorite: false
  • date: Monday, July 4, 2011 at 12:34:30 PM
  • id: 9XCpq6SzSD666TwlCHW8HA
  • height: 3240
  • width: 4320
  • altitude: missing value
  • location: missing valuemissing value

 

     But nothing in those attributes tells me where the actual image file is located on the hard drive.  Does anyone know a way in AppleScript to get physical path of an image in Yosemite photos app?

 

   Thanks!

Mac mini (Late 2012)

Posted on Dec 16, 2015 6:28 PM

Close

Q: Applescript to get physical path of an image in Yosemite photos app

  • All replies
  • Helpful answers

  • by VikingOSX,

    VikingOSX VikingOSX Dec 17, 2015 7:15 AM in response to SSS37
    Level 7 (20,819 points)
    Mac OS X
    Dec 17, 2015 7:15 AM in response to SSS37

    There are two categories of images within a Photos Library: 1) Copied - no external location information stored (default), and image is stored in Photos Library, and 2) Referenced - only pointers to external image locations are stored in the Photos Library, and actual image location information can be obtained from the  File menu, and Contextual menu as Show Referenced File in Finder menu item.

     

    In the Photos Preferences : General panel, there is an Importing: checkbox - Copy items to the Photos library. Apparently, this is enabled by default, as it was on after Photos migrated my iPhoto Library. It also means that I do not have category 2 above, and the menu items are grayed out, because there is no external file location stored in the Photos Library.

     

    More on this here on Ask Different.

  • by SSS37,

    SSS37 SSS37 Dec 17, 2015 7:09 PM in response to VikingOSX
    Level 1 (0 points)
    Dec 17, 2015 7:09 PM in response to VikingOSX

    Hi VikingOSX,

     

         Thanks for the great info but even with leaving the images at the original location, Photos doesn't put the actual location in the image data. 

     

         The data exposed using AppleScript does not include the location of the file and does not include data on faces.  Maybe I have to go in to one of the files in the "database" folder - like "metaSchema.db" or "Person.db".  Is there a way to read those database files using AppleScript?

     

         Or is there another way to go?

     

               Thanks!

  • by Pierre L.,

    Pierre L. Pierre L. Dec 19, 2015 2:23 PM in response to SSS37
    Level 5 (4,484 points)
    Dec 19, 2015 2:23 PM in response to SSS37

    SSS37 wrote:

     

    Does anyone know a way in AppleScript to get physical path of an image in Yosemite photos app?

     

    For the photos stored in a Photos library, the following script should return the physical path of the first selected photo (in Photos.app). However, the code is probably too slow for what you have in mind.

     

    set theLibraryName to "YourLibraryName.photoslibrary" -- as an example

     

    set theMastersFolder to (path to pictures folder as text) & theLibraryName & ":Masters" as alias

    tell application "Photos"

         set theSelecteMediaItem to item 1 of (get selection) -- the first selected photo

         set theFilename to filename of result

    end tell

     

    tell application "Finder"

         set thePath to (file 1 of entire contents of theMastersFolder whose name is theFilename) as alias

    end tell

  • by VikingOSX,

    VikingOSX VikingOSX Dec 19, 2015 4:26 PM in response to Pierre L.
    Level 7 (20,819 points)
    Mac OS X
    Dec 19, 2015 4:26 PM in response to Pierre L.

    Pierre,

     

    I had to revise your AppleScript slightly to get it to work with my Photos library on 10.11.2. It does return the filename, but not the full path. Apparently, it won't work with the as alias clauses either. If I omit the as POSIX file clause in the Finder, and use reveal thePath, the Finder path bar shows the image deep inside the Photos Library, not out in the filesystem.; Bump.

     

    # set theLibraryName to "YourLibraryName.photoslibrary" -- as an example

    set theLibraryName to "Photos Library:" as text

     

    set theMastersFolder to (path to pictures folder as text) & theLibraryName & "Masters:"

    tell application "Photos"

          set theSelecteMediaItem to item 1 of (get selection) -- the first selected photo

          set theFilename to filename of result as text

    end tell

     

    tell application "Finder"

          set thePath to (file 1 of entire contents of folder theMastersFolder whose name contains theFilename) as POSIX file

    end tell

    Result:

    file ":myselected_image.jpg"

  • by SSS37,

    SSS37 SSS37 Dec 19, 2015 7:39 PM in response to VikingOSX
    Level 1 (0 points)
    Dec 19, 2015 7:39 PM in response to VikingOSX

    Thank you both very much.  As you said Pierre, it does work but with all the photos in my library it would take a very long time and there are some duplicate file names.

     

    Has anyone ever read the Photos database file with AppleScript?  In the database folder there are files like (1) metaSchema.db or (2) Person.db  I am wondering if they contain the information on "faces" and the physical path?

     

    Thanks again!

  • by VikingOSX,

    VikingOSX VikingOSX Dec 19, 2015 9:23 PM in response to SSS37
    Level 7 (20,819 points)
    Mac OS X
    Dec 19, 2015 9:23 PM in response to SSS37

    Those are sqlite3 databases. I have perused through the table data in metaSchema.db and unsurprising, there is no filepath information.

  • by Pierre L.,

    Pierre L. Pierre L. Dec 20, 2015 6:31 AM in response to VikingOSX
    Level 5 (4,484 points)
    Dec 20, 2015 6:31 AM in response to VikingOSX

    VikingOSX wrote:

     

    I had to revise your AppleScript slightly to get it to work with my Photos library on 10.11.2.

     

    Hi VikingOSX,

     

    I can't understand why you had to revise my script. Here, under OS X 10.11.2 and with a newly created library named “TEST”, my script returns the following path for the first one of a few selected photos:

    alias "Macintosh HD:Users:pierre:Pictures:TEST.photoslibrary:Masters:2015:12:20:20151220-140530: G10 20110418 182253.JPG"

     

    (The first line of the script was

         set theLibraryName to "TEST.photoslibrary"

    and “G10 20110418 182253.JPG” is the name of the first selected photo.)


    Edit — No space before G10 20110418 182253.JPG” in the true returned path.

  • by VikingOSX,

    VikingOSX VikingOSX Dec 20, 2015 7:22 AM in response to Pierre L.
    Level 7 (20,819 points)
    Mac OS X
    Dec 20, 2015 7:22 AM in response to Pierre L.

    Pierre,

     

    I owe you and SSS37 an apology. I rediscovered this morning that your original AppleScript, when run against my own Photos Library — does work as posted, and reveals the full HFS path to the selected image within the Photos Library.

     

    I pride myself on catching my own mistakes before they are posted, and especially before they cascade into perceived, but incorrect changes to the code of others before they are reposted. There is very high embarassment and self-criticism here this morning, and it will ensure that this does not happen again.

  • by Pierre L.,

    Pierre L. Pierre L. Dec 20, 2015 9:28 AM in response to VikingOSX
    Level 5 (4,484 points)
    Dec 20, 2015 9:28 AM in response to VikingOSX

    No problem, VikingOSX, errare humanum est… Many thanks for the clarification.

  • by SSS37,

    SSS37 SSS37 Dec 20, 2015 11:45 AM in response to VikingOSX
    Level 1 (0 points)
    Dec 20, 2015 11:45 AM in response to VikingOSX

    It's all good.

     

    VikingOSX would you be able to share the code for looking in those sqlite3 databases.  Do they have the info on "faces" and which photos they are associated with?


    Thanks again,


             

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux Dec 21, 2015 1:30 PM in response to SSS37
    Level 4 (3,408 points)
    Mac OS X
    Dec 21, 2015 1:30 PM in response to SSS37

    Hi,

     

    SSS37 wrote:

     

     

    Has anyone ever read the Photos database file with AppleScript?

     

     

    It's possible to obtain the paths from the database.

    But you must copy the database in another location because the database is locked by the Photos application and sqlite3 can't read value from any table of this database (when "Photos" is open).

     

    property libPhotos : "/Users/myName/Pictures/Photos Library.photoslibrary" --> change it to the path of your package
    property tempLibDB : missing value
    
    -- check the path, error if it not exists
    libPhotos as POSIX file as alias
    set libDB to quoted form of (libPhotos & "/database/Library.apdb")
    
    -- make a temporary folder, and copy the database to this new folder
    tell application "Finder" to set tempFolder to (make new folder) as text
    set tempLibDB to quoted form of POSIX path of (tempFolder & "tempLib.db")
    do shell script "cp -f " & libDB & "  " & tempLibDB -- copy the database
    
    set allPaths to {}
    tell application "Photos"
        repeat with img in media items of container 1
            set theID to id of img
            set end of allPaths to my getPath(theID)
        end repeat
    end tell
    
    tell application "System Events" to delete (alias tempFolder) -- delete the temporary folder
    allPaths -- show this list in the editor
    
    
    on getPath(t)
        (* for a referenced file, sqlite3 get the full path of the file.
        Otherwise sqlite3 get the relative path of the file, the script concatenate the relative path to Previews folder or Masters folder. *)
        set r to do shell script "sqlite3  -separator $'\\n' " & tempLibDB & " 'select RKMaster.imagePath, RKMaster.fileIsReference from RKMaster, RKVersion  where RKVersion.uuid = \"" & t & "\" and RKMaster.modelid = RKVersion.modelid '"
        if r is not "" then
            set f to paragraph 1 of r
            try
                if paragraph 2 of r is "1" then return f as POSIX file as alias -- 1 equal referenced file, return path of the original file
                return (libPhotos & "/Previews/" & f) as POSIX file as alias -- return the path of the file in Previews folder
            end try
            try
                if paragraph 2 of r is not "1" then return (libPhotos & "/Masters/" & f) as POSIX file as alias -- not exists in Previews then return the path of the file in Masters folder
            end try
        end if
        return missing value -- not a valid path
    end getPath
    

     

    ----

     

    To know if some photo has a face, here's a pseudo code:

     

    do shell script "sqlite3 fullPath_of_theDatabase_person.db 'select personId from RKFace where imageId = \"" & someMedia_Item_ID & "\"'"

    if the result is "" then

       no face

    else

       one face or more

    end if

  • by SSS37,

    SSS37 SSS37 Dec 24, 2015 8:35 AM in response to Jacques Rioux
    Level 1 (0 points)
    Dec 24, 2015 8:35 AM in response to Jacques Rioux

    Thank you very much!  I am playing around with it now.

    Seems like I get a lot of "missing value" but will fine tune.

    Really appreciate all the help!

  • by belia60,

    belia60 belia60 Jun 16, 2016 8:47 AM in response to Jacques Rioux
    Level 1 (4 points)
    Jun 16, 2016 8:47 AM in response to Jacques Rioux

    The query is wrong, not modelID = modelID, but RKVersion.masterUuid = RKMaster.Uuid