AppleScript causes instability?

When I ran my iPhoto import script (see end of post) various programs became unstable- mostly iPhoto and Finder would suddenly stop/disappear but as the below shows, other things were affected:

Not all disappearances of iPhoto/Finder caused a crash.log, apparently.

The iPhoto one listed 1815 threads which seems… unusual :frowning: and died of memory exhaustion, which, with that many threads and ~7000 images seems reasonable :slight_smile:

I don’t recall any bizarre behaviour like this prior to running my AppleScript. Even now, the headings in the iTunes list are corrupted :frowning:

Is this ‘expected’/common behaviour? Is there something horribly wrong with my script?

Thanks,
Brian.


-- import photos from a directory structure to iPhoto preserving the directory names as keywords
-- keywords must be pre-created in iPhoto. Directory names that do not map to a created keyword will do nothing
tell application "iPhoto"
	activate
	set every window's miniaturized to true
end tell

set counter to 0

tell application "Finder"
	set dir to choose folder with prompt "Folder tree to import" default location alias "Macintosh HD:Users:brian:Pictures:"
	set allFiles to entire contents of dir
	
	repeat with f in allFiles
		if name of f ends with ".jpg" and name of f does not start with "~imported~" then
			
			set theURL to the URL of f
			set allTokens to every word in theURL
			
			set photosKeywords to {}
			set n to 0
			
			repeat with kw in allTokens
				if n > 5 then
					set end of photosKeywords to text of kw
				end if
				set n to 1 + n
			end repeat
			
			tell application "iPhoto"
				try
					import from (f as alias)
					repeat while importing
						delay 0.01
					end repeat
					
					repeat with kw in photosKeywords
						assign keyword string kw
					end repeat
				end try
			end tell
			
			try
				set name of f to "~imported~" & f's name
			end try
			
			set counter to 1 + counter
			
                        -- I do this because it improves stability
			if counter mod 500 = 0 then
				tell application "iPhoto" to quit
				
				display dialog "Done " & counter & " imports"
				
				tell application "iPhoto"
					activate
					set every window's miniaturized to true
				end tell
			end if
		end if
	end repeat
end tell

You may just be overloading the system. The import command for iPhoto works very well with a list of alias paths, and you will end up with fewer Rolls in your iPhoto structure. I suggest that you try to re-write your script to build a list of paths for import, and then just tell iPhoto to import that entire list.

The trick, of course, is to be able to maintain the keywords for each photo as you have already written. The way I have done this is to generate 2 lists, one of the alias file paths for import, and a second list of the keywords for each photo in the first list. Both lists are created simultaneously, and you import the photos into a specific, temporary album. Once the import is finished, you go back through the photos in the temporary album, assign keywords, then remove all the photos from the temp album.

All my code for this is on a different machine, so I don’t have anything to show you right now. I can say that this works very well, and does not disrupt the system at all.

Good Luck,

Good advice for any app command that will accept a list. First get the list, then do the deed with the list. Otherwise you’re sending a system event and waiting for a response for ever object you want an app to act on.

This is the general idea of what I was getting at:


-- import photos from a directory structure to iPhoto preserving the directory names as keywords
-- keywords must be pre-created in iPhoto. Directory names that do not map to a created keyword will do nothing
tell application "iPhoto"
	if not (exists album "Temps") then new album name "Temps"
	activate
	set every window's miniaturized to true
end tell

set counter to 0
set import_List to {}
set keyword_List to {}

tell application "Finder"
	set dir to choose folder with prompt "Folder tree to import" default location alias "Macintosh HD:Users:brian:Pictures:"
	set allFiles to entire contents of dir
	
	repeat with f in allFiles
		if name of f ends with ".jpg" and name of f does not start with "~imported~" then
			set end of import_List to (f as alias)
			set theURL to the URL of f
			set allTokens to every word in theURL
			
			set photosKeywords to {}
			set n to 0
			
			repeat with kw in allTokens
				if n > 5 then
					set end of photosKeywords to text of kw
				end if
				set n to 1 + n
			end repeat
			set end of keyword_List to photosKeywords
		end if
	end repeat
end tell

tell application "iPhoto"
	try
		import from import_List to album "Temps"
		repeat until importing is false
		end repeat
		
		repeat with pkw from 1 to (keyword_List's length)
			repeat with kw in (keyword_List's item pkw)
				tell photo pkw of album "Temps" to assign keyword string kw
			end repeat
		end repeat
	end try
end tell

Don’t expect this to work the first time; I tossed it together from memory. (I won’t have access to the machine that I have all this code on until tomorrow). It should work, but you know how these things go. The only thing I did not do was the re-naming of the files with the ~imported~ thing you had before. You should be able to throw together another Finder tell at the end to work through the same list ( import_List) to change all the filenames.

I hope this helps,

Hi,

Don’t use the quit command in nested tell blocks. That’s why the Finder is quitting. I hardly ever use nested tell blocks with different applicaitons. It makes the script longer, but you avoid this clashing.

gl,