photos-applescript-banner
•
OS X AUTOMATION
AUTOMATOR
IWORK AUTOMATION
•

Photos and Scripting

Automation begins with scripting. It is the foundational element that enables the user to query and control applications in OS X, unfettered by the limitations imposed on other forms of application-to-application communication. And best-of-all, because of its built-in scripting support, you can add Photos to your repertoire of automation tools.

Applications that support script access, are said to be “scriptable” and each one includes a user-readable dictionary of all the terms, objects, and commands that can be used by a script to communicate with the application. And like other scriptable applications, Photos has a scripting dictionary you use to create scripted solutions for managing and exporting your image library.

And when it comes to writing scripts, OS X offers two native script-language options for you choose from: AppleScript or JavaScript for Automation (JXA). AppleScript is a well-established and supported English-like language that those new to scripting find to be an approachable automation tool. JavaScript for Automation (JXA), new to OS X, is an extension of the core JavaScript language that those with coding-experience find to be familiar and understandable.

Regardless of which language you prefer, they both interact with the Photos scripting architecture to get the job done!

Here’s an script example that provides a quick way to assign keywords to selected Photos images:

Add Keywords to Selected Photos (AppleScript)
  
01-- Script is written using AppleScriptObj-C, so load Cocoa Foundatation framework
02use framework "Foundation"
03use scripting additions
04 
05property keyWordString : "" -- his property will remember last entry
06 
07tell application "Photos"
08 set currentSelection to the selection
09 if currentSelection is {} then error number -28
10 set dialogMessage to "Enter a comma-delimited list of keywords to apply to the selected media items:"
11 display dialog dialogMessage default answer keyWordString with icon 1 buttons {"Cancel", "Replace", "Append"} default button 3
12 copy the result to {button returned:buttonPressed, text returned:keyWordString}
13 -- convert the comma-delimited string into a list of strings
14 set AppleScript's text item delimiters to ","
15 set keywordsToApply to every text item of keyWordString
16 set AppleScript's text item delimiters to ","
17 -- trim any white space around the strings
18 set whiteSpaceSet to ¬
19 current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
20 repeat with i from 1 to the count of keywordsToApply
21 set thisWord to item i of keywordsToApply
22 set CocoaString to (current application's NSString's stringWithString:thisWord)
23 set trimmedKeyword to ¬
24 (CocoaString's stringByTrimmingCharactersInSet:whiteSpaceSet) as string
25 set item i of keywordsToApply to trimmedKeyword
26 end repeat
27 -- process each selected photo
28 repeat with i from 1 to the count of currentSelection
29 set thisItem to item i of currentSelection
30 -- get the current keywords of the photo
31 set theseKeywords to the keywords of thisItem
32 if theseKeywords is missing value then
33 set keywords of thisItem to keywordsToApply
34 else if keywordsToApply is {} then -- option for deleting all existing keywords
35 if buttonPressed is "Replace" then
36 set keywords of thisItem to {}
37 end if
38 else
39 if buttonPressed is "Append" then
40 -- check to see if new keyword already exists
41 set filteredKeywords to {}
42 repeat with q from 1 to the count of keywordsToApply
43 set thisKeyword to item q of keywordsToApply
44 if thisKeyword is not in theseKeywords then
45 set the end of the filteredKeywords to thisKeyword
46 end if
47 end repeat
48 if filteredKeywords is not {} then
49 -- append checked keywords to existing keywords
50 set keywords of thisItem to (theseKeywords & filteredKeywords)
51 end if
52 else -- replace keywords with new ones
53 set keywords of thisItem to keywordsToApply
54 end if
55 end if
56 end repeat
57end tell

And the same script written in JXA:

Add Keywords to Selected Photos (JXA)
  
01var Photos = Application("Photos")
02Photos.includeStandardAdditions = true
03 
04var currentSelection = Photos.selection()
05 
06if (currentSelection.length > 0) {
07 
08 var dialogMessage = "Enter a comma-delimited list of keywords to apply to the selected media items:"
09 
10 var result = Photos.displayDialog(dialogMessage, {
11 defaultAnswer: "",
12 withIcon: 1,
13 buttons: ["Cancel", "Replace", "Append"],
14 defaultButton: 3
15 })
16 
17 var buttonPressed = result.buttonReturned
18 var keywordString = result.textReturned
19 
20 var keywordsToApply = keywordString.split(",").map(function(element) {
21 return element.trim()
22 })
23 
24 for (var item of currentSelection) {
25 var keywords = item.keywords()
26 
27 if (!keywords) {
28 item.keywords = keywordsToApply
29 }
30 else if (keywordsToApply.length == 0) {
31 if (buttonPressed == "Replace") {
32 item.keywords = []
33 }
34 }
35 else if (buttonPressed == "Append") {
36 var filteredKeywords = []
37 
38 for (var keyword of keywordsToApply) {
39 if (!keywords.includes(keyword)) {
40 filteredKeywords.push(keyword)
41 }
42 }
43 
44 if (filteredKeywords.length != 0) {
45 item.keywords = keywords.concat(filteredKeywords)
46 }
47 }
48 else {
49 item.keywords = keywordsToApply
50 }
51 }
52}

NEXT TOPIC: Photos Utilities Script Library

TOPICS

  • Automator Overview

  • Scripting Overview
  • Photos Script Library

DISCLAIMER

THIS WEBSITE IS NOT HOSTED BY APPLE INC.

Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. PHOTOSAUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. PHOTOSAUTOMATION.COM provides this only as a convenience to our users. PHOTOSAUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and PHOTOSAUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from PHOTOSAUTOMATION.COM and that PHOTOSAUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.