3

The new Photos.app does not come with Automator support (although there are third-party actions available). It does, however, have some basic AppleScript support, including support for importing a list of files.

AppleScript Dictionary

However, I can't get this action to work correctly. Any attempts to use it, such as via the following simple script

tell application "Photos"
    set theList to {"/Full/Path/To/Some/Photo.jpg"}
    import theList
end tell

will cause Photos.app to report that "no metadata" could be retrieved from the file because it may not be a photo (however, the same file can be manually imported). Further investigation reveals that the automated approach is failing due to the lack of permission to read the file.

From the Console.app:

4/11/15 9:07:49.333 AM sandboxd[253]: ([3934]) Photos(3934) deny file-read-data /Full/Path/To/Some/Photo.jpg

(followed by a host of similar error messages). The third-party Automator actions I linked above have the same problem, for what it is worth.

How can I give the appropriate permission to Photos.app or the executing AppleScript/Automation workflow to perform this import? Note that the "permission" appears to be something other than (or in addition to) the usual file permissions, because setting the file and folder to "read/write from by everybody" (the nuclear option) does not change the behavior.

  • not my strong point, but that doesn't look like a perms error, but a sandboxing one – Tetsujin Apr 11 '15 at 16:35
  • Yeah, I think that's absolutely the case, but I've no idea what to go tweak to fix or work around it. – user3259 Apr 11 '15 at 16:37
  • 1
    from Applescript docs - When sending commands to a sandboxed application, such as TextEdit in OS X Mountain Lion, parameters that refer to files must be of an explicit file-like type and not a bare string, or the target application will not be able to access the file. For example, file "Macintosh HD:Users:me:sample.txt", POSIX file "/Users/me/sample.txt", or the result of choose file would all be acceptable, but the string "/Users/me/sample.txt" would not. – Tetsujin Apr 11 '15 at 16:40
  • Seems like adding POSIX file would fix it – Tetsujin Apr 11 '15 at 16:41
  • Sounds promising, but AppleScript tells me it "can't make file 'path:to:file' into the expected type" when execution reaches the "import" line. :( – user3259 Apr 11 '15 at 17:04
2

Based on this from an old Applescript Release Notes on sandboxing…

When sending commands to a sandboxed application, such as TextEdit in OS X Mountain Lion, parameters that refer to files must be of an explicit file-like type and not a bare string, or the target application will not be able to access the file. For example, file "Macintosh HD:Users:me:sample.txt", POSIX file "/Users/me/sample.txt", or the result of choose file would all be acceptable, but the string "/Users/me/sample.txt" would not.

&

Note: As mentioned under Compatibility above, sandboxed applications will not be able to access files referred to in commands using a string path. Any parameters or properties in your application that refer to files should be declared as type file, and not type text. Apple Event Manager will add sandbox extensions to events that have file-like parameters, but only if the parameter is of a recognized file-like type: typeAlias, typeFileURL, cFile, and so on. Without these extensions, the file will not be accessible from the target application’s sandbox, and the command will probably fail.

It would seem that the 'fix' is to use POSIX file to prevent the sandbox issue

After some hammering at how to make Applescript work with POISIX file of a list, I came up with this…

tweaked

set theList to {"/Volumes/Downloads/ScreenShot.png", "/Volumes/Downloads/ScreenShot2.png", "/Volumes/Downloads/ScreenShot3.png"}
set l to {}
repeat with f in theList
    set l to l & (POSIX file f)
end repeat
tell application "Photos" to import l

It ain't perfect, as every import is a new one, but it works.
Now works in one import.

  • tweaked it - better, one import – Tetsujin Apr 11 '15 at 17:53
  • What is your outermost "tell" there? I can't tell Finder nor System Events to do this; both give me the same "can't get POSIX file <...> from file <...>" :( – user3259 Apr 11 '15 at 18:23
  • Okay, got it: what you posted has to be outside a tell entirely (I had it inside one because I need to tell System Events to get every file in a folder; forgot you could actually have statements outside a tell/function, so that's why I was still getting an error). – user3259 Apr 11 '15 at 18:36
  • Glad you got it. I assumed your initial list would be constructed differently, but had no way to know exactly how. – Tetsujin Apr 12 '15 at 9:40
  • @Tetsujin Any idea on how to handle this question? This looks like the same problem, but I can't get it applied to my issue. apple.stackexchange.com/questions/269539/… – SpacyRicochet Jan 19 '17 at 20:57
0

You can also insert this line anywhere before the export line in your code:

open for access file f

This seemed to do the trick for me.

You must log in to answer this question.