How can I avoid permission-related failures when importing in Photos.app via AppleScript?
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.
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.
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.