osx - AppleScript for successfully importing photos to Aperture -


i trying make apple script importing large collection of photos aperture aperture crashes in situation. reason import each file separately moving library.my initial try works on 1 picture:

on run argv set file_name item 1 of argv   tell application "aperture" activate     tell library 1       if not (exists project "oldpictures")           set theproject make new project properties {name:"oldpictures"}       else           set theproject project "oldpictures"       end if end tell timeout of 600 seconds       import file_name moving theproject end timeout end tell  end run 

but crashes if run in following way

find /volumes/pictures/ -iname "*.jpg" -exec  osascript /users/mac/apperture.scpt  {} \; 

brute-force muscle import

you may want try hardcore script. take ages finish, should work, because ask…it adds each file 1 one specified project , checks make sure there aren't duplicates name only. test script on 13262 pictures. run night, beat built-in importer keeps failing.

i recommend create test project on first run, ensure aren't importing duplicates. optionally, can move images appropriate project in aperture. have set up. if want ignore , import them directly original project, can changing following line:

  • my importtoapertureasreference(itemalias, projectdst)

to

  • my importtoapertureasreference(itemalias, projectname)

os x 10.9.4

aperture 3.5.1

disclaimer: threw together. there room improvement, checks ensure files in list pictures. did not checks that. done in shell command or using system events type of each file passes through. also, computer flip out when run this. suggest don't else on computer.

applescript - import missing files project one-by-one"

by comparing names of pictures names of files

updated 01.08.2014

i improved speed of script make bourne script of work. applescript gets bogged down when talking programs, relatively fast when iterating lists. used bourne script's find command combined basename make new list of of files without paths. applescript iterates list , compares list of names in designated project. if finds 1 not exist in project projectname, then joins path filename , adds project destination projectdst reference. if want copy file library , not reference, need adjust handler importtoapertureasreference or create new 1 called importtoapertureascopying or importtoapertureasmoving.

set inputpath "volumes:kielbasa:camera:1970 - fotos" string set inputpathhfs inputpath alias log "inputpathhfs: " & inputpathhfs set projectname "1970 - fotos" -- check project duplicates set projectdst "applescript import" -- missing files added set importedlist {}  set inputpathpx text 1 through -2 of posix path of inputpathhfs log "inputpathpx: " & inputpathpx set countitemscommand "find " & quoted form of inputpathpx & " -type f -maxdepth 1 \\! -name '.*' | wc -l" set getitemscommand "find " & quoted form of inputpathpx & " -type f -maxdepth 1 ! -name '.*' -exec sh -c 'basename \"{}\"' \\;"  set itemcount (do shell script countitemscommand) integer set itemlist paragraphs of (do shell script getitemscommand) list set missingitemslist {}  set namesinprojectlist getallnamesinproject(projectname) list  -- check whether project contains object/item (image/video) same name repeat itemstep 1 count of itemlist     set thisitem item itemstep of itemlist     log "this item: " & thisitem     set itemsplit splitextension(thisitem)     set itemname item 1 of splitextension(thisitem)     set itemext item 2 of splitextension(thisitem)     log "item name: " & itemname & ", item ext: " & itemext     log "checking if project " & projectname & " contains " & itemname     set foundimages findall(namesinprojectlist, itemname)     if foundimages {} -- if empty, there no match image should added aperture         set end of missingitemslist contents of thisitem -- makes new list of missing images extensions (no path), import aperture need join item full path          set picalias (inputpathhfs string) & thisitem alias         log "importing: " & picalias         importtoapertureasreference(picalias, projectdst)     else         repeat foundindex in foundimages             deleteitem(namesinprojectlist, foundindex)             log "found index: " & foundindex             log "removed namesinprojectlist: " & thisitem         end repeat     end if end repeat   return missingitemslist  (* tell application "system events"     repeat itemstep 1 count of itemlist         set thisitem item itemstep of itemlist         log "this item: " & thisitem         set itemsplit splitextension(thisitem)         set {itemname, itemext} {item 1 of itemsplit, item 2 of itemsplit}         log "item name: " & itemname & ", item ext: " & itemext         log "checking if project " & projectname & " contains " & itemname         -- compare lists         if namesinprojectlist not contain itemname             if otherlist not contain itemname                 -- generate alias filename joining inputpath , filename                 set thisfilealias inputpath & ":" & thisitem alias -- eg "volumes:kielbasa:camera:1970 - fotos000_0014.jpg"                 importtoapertureasreference(thisfilealias, projectdst)                 log "imported: " & thisfilealias                 set end of importedlist thisfilealias             end if         end if      end repeat end tell *) return importedlist  on importtoapertureasreference(picalias, projectname)     log "importing: " & picalias     tell application "aperture"         tell library 1             if not (exists project projectname)                 set projectdst make new project properties {name:projectname}             else                 set projectdst project projectname             end if             import picalias referencing projectdst         end tell     end tell end importtoapertureasreference   on getallnamesinproject(projectname)     tell application "aperture"         tell library 1             tell project projectname                 set namelist name of image versions             end tell         end tell     end tell     return namelist end getallnamesinproject   on splitextension(file_name)     set dot "."     tell applescript         set ot text item delimiters         set text item delimiters dot         if (count text items of file_name) > 1             set out_name (text items 1 through -2 of file_name) string             set ext last text item of file_name         else             set out_name file_name             set ext ""         end if         set text item delimiters ot         if ext not "" set ext dot & ext         return {out_name, ext}     end tell end splitextension  on deleteitem(lst, idx)     local lst, idx, len, ndx, l     try         if lst's class not list error "not list." number -1704         script k             property l : lst         end script         set len count of k's l         set ndx idx integer         if ndx 0             error "index 0 out of range." number -1728         else if ndx < 0             set ndx len + 1 + ndx             if ndx < 1 error "index " & idx & ¬                 " out of range." number -1728         else if ndx > len             error "index " & idx & " out of range." number -1728         end if         if ndx 1             return rest of k's l         else if ndx len             return k's l's items 1 thru -2         else             return (k's l's items 1 thru (ndx - 1)) & ¬                 (k's l's items (ndx + 1) thru -1)         end if     on error emsg number enum         error "can't deleteitem: " & emsg number enum     end try end deleteitem  on findall(lst, val)     local lst, val, res     try         if lst's class not list error "not list." number -1704         if {val} not in lst return {}         set res {}         script k             property l : lst         end script         repeat 1 count of k's l             if k's l's item val set res's end         end repeat         return res     on error emsg number enum         error "can't findall: " & emsg number enum     end try end findall 

python - import missing files project one-by-one (uses applescript)

applescript choking on load of 13262 pictures. lists applescript when big, decided write python script. python, subtract 2 sets (lists sets) 1 create new list containing missing items. removes need checking each item in list again each item in list. speeds process significantly.

#!/usr/bin/env python # -*- coding: utf-8 -*-  # imports import subprocess subprocess import popen, pipe import unicodedata import codecs import os   inputpath = "/volumes/kielbasa/camera/1970 - fotos" ## source folder containing images , videos want ensure in project (projectoriginal) projectoriginal = "1970 - fotos" ## photos original input (if want import harddrive 1 one, create empty project , use here) projectdst = "python import" ## import of images , videos missing project. debugging project (so don't screw up). can drag images on projectoriginal project after ensuring worked correctly.  importedlist = []  getitemscommand = """find '%s' -type f -maxdepth 1 ! -name '.*' -exec sh -c 'basename "{}"' \;""" % inputpath  #subprocess.check_output(*popenargs, **kwargs) ## itemslistext list of files in hd folder extensions (aperture projects import filenames without extensions, henceforth refer names). list used reconstruct file paths later. important have if have files different extensions (e.g. images , movies, jpg, jpeg, png, raw) itemslistext = subprocess.check_output(getitemscommand, shell=true, executable="/bin/bash").splitlines() itemcount = len(itemslistext)  def getitemsinproject(projectoriginal):     cmd = """osascript<<end     tell application "aperture"             tell library 1                 tell project "%s"                     return name of image versions                 end tell             end tell         end tell     end""" % projectoriginal     itemslistext = subprocess.check_output(cmd,shell=true,executable="/bin/bash").split(', ')     return itemslistext  ## namesinprojectlist list containing names of images inside projectoriginal project. namesinprojectlist = getitemsinproject(projectoriginal)  ## namesonhdlist list populated of items in itemslistext, without extensions. indexes must same between these 2 lists reconstructing file paths later. namesonhdlist = []  itemstep,thisitem in enumerate(itemslistext):     #print itemstep, thisitem.decode('utf-8')     name,ext = os.path.splitext(thisitem)     namesonhdlist.append(name)  missingitemslist = [] itemstep,thisitem in enumerate(namesonhdlist):     if thisitem in namesinprojectlist:         ## place holder         continue     else:         ## reconstructs full paths out of missing item names getting name + extension itemslistext (the indexes in these 2 lists must same)         missingitemslist.append(os.path.join(inputpath,itemslistext[itemstep]))  ## debugging only. can compare total number of missing items difference between project , hd items. if number same sum of items in missingitemslist, working. print len(missingitemslist) print missingitemslist   def importtoapertureasreference(objectpath,projectdestination):     scpt = '''     on run {objectpath,projectdestination}         set objectalias posix file objectpath alias         set projectname projectdestination         tell application "aperture"             tell library 1                 if not (exists project projectname)                     set projectdst make new project properties {name:projectname}                 else                     set projectdst project projectname                 end if                 import objectalias referencing projectdst             end tell         end tell     end run     '''     args = [objectpath, projectdestination]     p = popen(['osascript', '-'] + args, stdin=pipe, stdout=pipe, stderr=pipe)     stdout, stderr = p.communicate(scpt)     print (p.returncode, stdout, stderr)     print "--> imported: " + objectpath +  " project: " +  projectdestination  ## add items missingitemslist aperture project destination (projectdst) countitems = len(missingitemslist) itemstep,thisitem in enumerate(missingitemslist):     importtoapertureasreference(thisitem,projectdst)     print "item: " + str(itemstep) + " of " + str(countitems) 

originally-posted applescript

set inputpath "volumes:kielbasa:camera:1970 - fotos" alias log "inputpath: " & inputpath set projectname "1970 - fotos" -- check project duplicates set projectdst "applescript import" -- missing files added set importedlist {}  set inputpathpx text 1 through -2 of posix path of inputpath log "inputpathpx: " & inputpathpx set countitemscommand "find " & quoted form of inputpathpx & " -type f \\! -name '.*' | wc -l" set getitemscommand "find " & quoted form of inputpathpx & " -type f ! -name '.*'"  set itemcount (do shell script countitemscommand) integer set itemlist paragraphs of (do shell script getitemscommand) list   set nameslist getallnamesinproject(projectname) list set otherlist getallnamesinproject("test project") list   tell application "system events"     repeat itemstep 1 count of itemlist         tell me set itemalias posix file (item itemstep of itemlist) alias         log itemalias         set itemname name of itemalias         set filenamenoext item 1 of splitextension(itemname)         log "checking if project " & projectname & " contains " & filenamenoext         if nameslist not contain filenamenoext             if otherlist not contain filenamenoext                 importtoapertureasreference(itemalias, projectdst)                 set end of importedlist itemalias             end if         end if     end repeat end tell  return importedlist  on importtoapertureasreference(picalias, projectname)     log "importing: " & picalias     tell application "aperture"         tell library 1             if not (exists project projectname)                 set projectdst make new project properties {name:projectname}             else                 set projectdst project projectname             end if             import picalias referencing projectdst         end tell     end tell end importtoapertureasreference   on getallnamesinproject(projectname)     tell application "aperture"         tell library 1             tell project projectname                 set namelist name of image versions             end tell         end tell     end tell     return namelist end getallnamesinproject   on splitextension(file_name)     set dot "."     tell applescript         set ot text item delimiters         set text item delimiters dot         if (count text items of file_name) > 1             set out_name (text items 1 through -2 of file_name) string             set ext last text item of file_name         else             set out_name file_name             set ext ""         end if         set text item delimiters ot         if ext not "" set ext dot & ext         return {out_name, ext}     end tell end splitextension 

tip: can watch happening if click on events or response option @ bottom of applescript editor (better before running script)


Comments