unlight

Q: Photos and Applescript export

Hi,

 

for Iphoto I had a nice Script to copy new Photos to a Network location:

 

set theDestinationFolder to POSIX file "/location/Fototemp/NewPhotos/" as alias

 

    tell application "iPhoto"

 

set theImagePaths to image path of photos of album "NewPhotos"

    end tell

 

    repeat with thisPath in theImagePaths

set thisFile to POSIX file thisPath as alias
tell application "Finder" to duplicate thisFile to theDestinationFolder with replacing

    end repeat

 

with the new "Photos" I can´t get it to work; so I tried the export function of Photos:

 

tell application "Photos"

    set theContainersList to every container whose name is "NEW PHOTOS"

    set theItemsList to every media item of the first item of theContainersList

    export theItemsList to "Users/Name/Fototemp/NewPhotos/"

end tell

 

but it allways gives me a number -10000 error.

 

Am I missing something?

 

Thanks in advance!

Unlight

iMac (21.5-inch Late 2009), OS X Yosemite (10.10.3), null

Posted on Apr 19, 2015 6:09 AM

Close

Q: Photos and Applescript export

  • All replies
  • Helpful answers

  • by NicFletcher,

    NicFletcher NicFletcher Apr 20, 2015 1:49 AM in response to unlight
    Level 1 (89 points)
    Apr 20, 2015 1:49 AM in response to unlight

    Hi - Try this:

     

    set albSource to "Test Album" -- Album name

    set srcParent to "" -- Folder that album is in

     

    tell application "Photos"

     

      -- Get the the source album container object, checking first whether it exists or is in a folder

      set notExist to false

      if srcParent = "" then

      if exists container albSource then

      set srcContainer to container albSource

      else

      set notExist to true

      end if

      else

      if exists container albSource of container srcParent then

      set srcContainer to container albSource of container srcParent

      else

      set notExist to true

      end if

      end if

     

      if notExist then

      display dialog "Album or folder doesn't exist - Exiting" buttons ¬

      "OK" with icon caution default button "OK"

      return

      end if

     

      -- Can either set the path as an alias directly, or use "choose folder"

      -- set mypath to "Macintosh HD:Users:nifletcher:Desktop:TempPhotos" as alias

      set mypath to (choose folder)

     

      set myList to every media item in srcContainer

     

      export myList to mypath

     

      display dialog "I made it!"

     

    end tell

  • by R C-R,

    R C-R R C-R Apr 20, 2015 2:34 AM in response to unlight
    Level 6 (17,633 points)
    Apr 20, 2015 2:34 AM in response to unlight

    I think the only thing wrong with your script is in your export step you are using the POSIX form of the file path reference, so you have to make that explicit:

     

    use  export theItemsList to POSIX file "Users/Name/Fototemp/NewPhotos/"

    instead of export theItemsList to "Users/Name/Fototemp/NewPhotos/"

     

    If you are going to reference a file path without the POSIX file specifier, you have to use Applescript's "native" colon separated path nomenclature, like this:

    export theItemsList to "Users:Name:Fototemp:NewPhotos:"

     

    (The trailing colon tells the script the path is to a folder, just like the trailing slash does in the POSIX reference.)


    I tested your script with that change, using both the explicit POSIX & "native" forms of the file path specifier, on both a 'smart album' & regular one in my Photos Library, & they all ran fine, with no errors.

  • by NicFletcher,

    NicFletcher NicFletcher Apr 20, 2015 2:45 AM in response to R C-R
    Level 1 (89 points)
    Apr 20, 2015 2:45 AM in response to R C-R

    I wasn't aware about the subtlety of the colon at the end of an HFS path nomenclature... but I noticed my earlier script in this discussion did not have the colon, even though it was a folder. And it works with or without the last colon.

     

    Maybe Photos is taking care of that.

     

    Just FYI.

  • by R C-R,

    R C-R R C-R Apr 20, 2015 4:53 AM in response to NicFletcher
    Level 6 (17,633 points)
    Apr 20, 2015 4:53 AM in response to NicFletcher

    I mentioned the trailing colon or slash thing because it is common practice in many scripts to construct a file path by concatenating partial paths defined as strings, & because not every app will coerce the target of a verb to a folder when required unless that is made explicit with the trailing colon or slash.

     

    It was intended only as an example of why you might need to make the type of a file path reference explicit if the script does not behave as expected. It is one of about a zillion scripting "gotchas" that have caused my scripts to fail more often than I like to think about.

  • by unlight,

    unlight unlight Apr 20, 2015 8:12 AM in response to unlight
    Level 1 (7 points)
    Mac OS X
    Apr 20, 2015 8:12 AM in response to unlight

    Great, seems to work!

     

    THX alot.

    Unlight

  • by unlight,

    unlight unlight Apr 30, 2015 6:36 AM in response to unlight
    Level 1 (7 points)
    Mac OS X
    Apr 30, 2015 6:36 AM in response to unlight

    ****, something strange is going on.

    It seems that it doesn´t work if the album is not selected.

    If I select the album manually and run the script everything works fine, if somthing else is shown in the app

    the script ends with error 10000.

     

    quick´n´dirty version would be to select the album by script; is that even possible?

     

    Thx

    Unlight

  • by NicFletcher,

    NicFletcher NicFletcher Apr 30, 2015 7:20 AM in response to unlight
    Level 1 (89 points)
    Apr 30, 2015 7:20 AM in response to unlight

    Yes, you're right... there is something strange regarding which album is displaying. The AppleEvent Handler error is inconsistent... for smaller runs, sometimes it will work, other times not. So, it is always best to have Photos displaying the appropriate album.

     

    And, unfortunately, no it is not possible to select an Album with AppleScript (or at least through the Photos AppleScript interface). It could be possible to use UI scripting, but it's complex, hard to test, maybe depend on your folder structure, etc. So, I reckon it's just easier to go to Photos and select the right album.

     

    For what it's worth, my favourite Duplicate Manager (PhotoSweeper - on the Mac App Store) when updating the library, requires you to have the "All Photos" album open before it will do its updates. So, it's not just AppleScript it would seem.

     

    Finally, I wrote a script around a week ago, mainly for me just to test how different things worked... but I've not had time to do much more with it.

     

    You're welcome to take a look at it here:  Testing with Photos V1.0

     

    But please NOTE: It's a NOT a script to necessarily do anything (although I do have a go at finding duplicates, updating keywords, etc.)... RATHER, it is a collection of sections that show how to perform base functions such as getting containers or lists, and I wrote lots of notes to explain my findings. You will probably want to COMMENT out several sections when running it... and you will certainly need to read the script.

  • by unlight,

    unlight unlight Apr 30, 2015 7:45 AM in response to NicFletcher
    Level 1 (7 points)
    Mac OS X
    Apr 30, 2015 7:45 AM in response to NicFletcher

    What a shame!

    I used to rely on a workflow I created to export a smartalbum holding the photos of the last 3 month, resize them and copy them to a remote location where they could be fetched by our smartphones, worked like a charm with iPhoto.

     

    I think I´ll wait and hope for apple to get the automator integration done.

     

    Thanks anyway!

     

    Unlight

  • by unlight,

    unlight unlight Apr 30, 2015 9:38 AM in response to unlight
    Level 1 (7 points)
    Mac OS X
    Apr 30, 2015 9:38 AM in response to unlight

    Solved it really dirty with cliclick:

     

    tell application "System Events"

        tell process "Photos"

            tell window 1

                set position to {20, 30}

                set size to {1800, 1000}

            end tell

        end tell

    end tell

    do shell script "cliclick " & "100 247"

     

    where 100,247 is the location of the smartalbum on the screen.

  • by ZeroDivergence,

    ZeroDivergence ZeroDivergence May 13, 2015 2:40 PM in response to NicFletcher
    Level 1 (0 points)
    May 13, 2015 2:40 PM in response to NicFletcher

    Hi Nic,

     

    I adapted your script to export all albums in a folder: https://gist.github.com/chrislaing/c42661365448993572e2

     

    Unfortunately, the API seems to randomly throw fits and return error -10000. If you feel like improving the script, please do so.

     

    Cheers,

     

    Chris

  • by NicFletcher,

    NicFletcher NicFletcher May 13, 2015 4:48 PM in response to ZeroDivergence
    Level 1 (89 points)
    May 13, 2015 4:48 PM in response to ZeroDivergence

    I took a quick look at your script. While I'm only an amateur, I can't see anything wrong with it.

     

    But what I can say - form limited experience with Photos (and much other experience on the internet) is that the Photos AppleScript API is extremely buggy. If you look at some of my other posts (two link to a sample script with notes on my experiments with Photos), you will see that there are some issues in Photos regarding a list of a container and a reference to a container. Even then, occasionally (but much less so), even a reference to an album (container) will cause the -1000 AppleEvent Handler error.

     

    If Photos is open and running, AND the album being processed by AppleScript is selected and visible in Photos, then most scripts will run flawlessly. Even some commercial available utilities require Photos to be open and displaying a specific album. This "bug" is always worse when creating a list as opposed to referencing an album.

     

    So, trying to do wha you are doing - aka loop through many album in a folder using only name in the list is probably bound to fail.

     

    I don't have the time now to experiment, but I suggest you create a variable that references the album object - i.e, use your list of every album name to create a new reference to the album object.

     

    While it feels tedious, it is often quicker to process album by album than try to solve this issue. Only today, with something completely different, I wasted 45 minutes trying to automate something I could have manually intervened with that would added only 5 minutes! In this case, I use a smart album to collect the media items I want to process; by using the same Smart Album name, it means I don't need to change my script every time.

  • by R C-R,

    R C-R R C-R May 13, 2015 5:02 PM in response to NicFletcher
    Level 6 (17,633 points)
    May 13, 2015 5:02 PM in response to NicFletcher

    I think part of the problem is Photos can take a significant amount of time to perform some operations & while Applescripts should default to waiting 2 minutes for an application response before timing out, for some reason this does not always happen with Photos, possibly because the app is signaling to the script that the operation is complete before it really is.


    As I have mentioned elsewhere, the reliability of some of my Photos scripts have been improved by adding a delay statement in repeat loops, but of course that can increase execution time significantly & only trial & error can determine how long a delay is needed, or even if it will help.

  • by unlight,

    unlight unlight May 13, 2015 11:27 PM in response to R C-R
    Level 1 (7 points)
    Mac OS X
    May 13, 2015 11:27 PM in response to R C-R

    Since I implemented the cliclick action it worked flawless. Unfortunately this won´t work for exporting all albums.

    The problems with the 2 minutes I solved by putting the whole export stuff in a timeout block:

     

    tell application "Photos"

            activate

            delay 20

            tell application "System Events"

                tell process "Photos"

                    tell window 1

                        set position to {20, 30}

                        set size to {1800, 1000}

                    end tell

                end tell

            end tell

            do shell script "cliclick " & "100 247"

            with timeout of 600 seconds

                set theContainersList to every container whose name is "New Photos"

                set theItemsList to every media item of the first item of theContainersList

                export theItemsList to "HD:Users:Me:Phototemp:NewPhotos:" as alias

            end timeout

        end tell

     

    This works great for me.

     

    Unlight

  • by m_lance,

    m_lance m_lance Dec 22, 2015 8:33 AM in response to unlight
    Level 1 (0 points)
    Dec 22, 2015 8:33 AM in response to unlight

    how do I use Cliclick?.  I downloaded it and ran the command, however, the script you said worked for above gave me and error: "Photos got an error: sh: cliclick: command not found"