#! /usr/bin/python # Simple script that runs imageMagick's convert command to extract the # IPTC keywords from a file, then renames the file as the file name plus # all the keywords followed by the extension. # Unfortunately LightRoom is not nice to shell scripts, so to run this you have # to wrap it in the following AppleScript application: # #on open theObjects # repeat with theObject in theObjects # do shell script "'/Users/USERNAME/Library/Application Support/Adobe/Lightroom/Export Actions/iptc-rename.py' " & (POSIX path of theObject as string) # end repeat #end open # # Place that in your Export Actions folder and it should work beautifully! # # Note that you'll need to change the path in the AppleScript to point to # your Library folder, and you'll have to change the imageMagick variable # below to point to your ImageMagick installation. # # Script by David Black-Schaffer, September 2007 # No rights reserved; no promises of suitability made. # import os import sys import re DEBUG = False imageMagick = "/Applications/Utilities/Other Utilities/Graphics/ImageMagick-6.3.2" convertCommand = "convert" renameCommand = "/bin/mv" keywordSeparator = " " os.environ['MAGICK_HOME'] = imageMagick os.environ['DYLD_LIBRARY_PATH'] = imageMagick + "/lib" os.environ['PATH'] = os.environ['PATH'] + ":" + imageMagick + "/bin" if (DEBUG): log = open("ipct-rename.log.txt", "a") # Get the file name passed in to us from the apple script fileName = sys.argv[1] if (DEBUG): log.write("filename: " + fileName + "\n") # Execute the command to get the results commandToExecute = convertCommand +" " + fileName + " IPTCTEXT:-" if (DEBUG): log.write("commandToExecute: " + commandToExecute +"\n"); convert = os.popen(commandToExecute, "r") # Build the keyword string by attaching the keywords and separators keywordString = "" for line in convert: if (DEBUG): log.write("read line: " + line) match = re.search(".*Keyword=\"(.*)\"", line) if (match): keywordString=keywordString+keywordSeparator+match.group(1) if (DEBUG): log.write("match: " + match.group(1) + " string: " + keywordString +"\n") if (DEBUG): log.write("final string: " + keywordString +"\n") # Extract the prefix and suffix from the file name and change the file name match = re.search("(.*)\.([A-Za-z0-1]+)", fileName) if (match): filePrefix = match.group(1) fileSuffix = match.group(2) if (DEBUG): log.write("filePrefix " + filePrefix + " fileSuffix " + fileSuffix +"\n") newFileName = filePrefix + keywordString + "." + fileSuffix if (DEBUG): log.write("Final new file name: " + newFileName +"\n") commandToExecute = renameCommand +" " + "\""+ fileName + "\""+ " " + "\""+ newFileName+"\"" if (DEBUG): log.write("Move command: " + commandToExecute +"\n") os.system(commandToExecute) else: if (DEBUG): log.write("Failed to match filename!\n")