Aperture GUI Scripting

I’m new to this site so apologies for any faux pas.

I am trying to use GUI scripting to drive Apple’s Aperture in order to create Smart Albums. While Aperture does have extensive support for Applescript, the one function missing is the ability to create a Smart Album.

I have had success in that I can get the script to create a new smart album, clean up a couple of superfluous search criteria and add the one I want, but when it comes to entering text into a text field I run into problems.

Despite attempting to set focus, clicking etc the field, I cannot get it to accept keystrokes.

I have tried various methods but all fail. Interestingly, If I create the smart album without entering the text then run a separate applescript that only enters the text it works (same code in both scripts).

I am new to applescript and particularly GUI scripting so likely doing something wrong. Any help will be much appreciated.

Regards,
Rod


-- Numericals

property STANDARD_DELAY : 0.1

-- UI Elements by name

property HUD_TITLE_PREFIX_name : "Smart Settings: "
property ADD_RULE_BUTTON_name : "Add Rule"
property ADD_RULE_MENU_name : "Add Rule"
property ENABLE_CHECKBOX_name : "Aperture:"
property METADATA_ITEM_name : "Aperture Metadata"

-- UI Elements by index

property HUD_CLOSE_BUTTON_indx : 1
property HUD_LIST_indx : 1
property HUD_SCROLL_AREA_indx : 1
property GROUP_CLOSE_BUTTON_indx : 1
property BASE_SEARCH_GROUP_indx : 1
property SEARCH_TEXT_FIELD_indx : 1
property CRITERIA_PUBUTTON_indx : 1
property SEARCH_TAG_PUBUTTON_indx : 2
property CRITERIA_MENU_indx : 1
property SEARCH_TAG_MENU_indx : 1


-- Call the function
my Create_Metadata_Smart_Album("Albums", "Includes", "Family Portraits")


on Create_Metadata_Smart_Album(Search_Tag_Item_str, Search_Criteria_str, Search_Text_str)
	
	try
		tell application "System Events"
			
			tell process "Aperture"
				
				set frontmost to true
				delay STANDARD_DELAY
				
				-- Create a blank smart album
				keystroke "L" using {command down, shift down}
				delay STANDARD_DELAY
				
				-- Name it
				keystroke Search_Text_str
				keystroke {return}
				delay STANDARD_DELAY
				
				-- Ensure it exists with new name
				set HUD_Window_Title_str to (HUD_TITLE_PREFIX_name & Search_Text_str) as string
				repeat until exists window HUD_Window_Title_str
				end repeat
				
				-- Close the unused standard serach criteria groups
				set Std_Groups_int to the count of every group of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				repeat with Index_int from Std_Groups_int to (BASE_SEARCH_GROUP_indx + 1) by -1
					click button GROUP_CLOSE_BUTTON_indx of group Index_int of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				end repeat
				delay STANDARD_DELAY
				
				-- Add a new blank search criteria group
				click menu button ADD_RULE_BUTTON_name of window HUD_Window_Title_str
				click menu item METADATA_ITEM_name of menu ADD_RULE_MENU_name of menu button ADD_RULE_BUTTON_name of window HUD_Window_Title_str
				delay STANDARD_DELAY
				
				-- Set the metadata tag to be searched
				click pop up button SEARCH_TAG_PUBUTTON_indx of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				click menu item Search_Tag_Item_str of menu SEARCH_TAG_MENU_indx of pop up button 2 of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				delay STANDARD_DELAY
				
				-- Set the search criteria
				click pop up button CRITERIA_PUBUTTON_indx of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				click menu item Search_Criteria_str of menu CRITERIA_MENU_indx of pop up button 1 of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				delay STANDARD_DELAY
				
				
				-- Set the search text
				-- This has some weird interface quirks!!!
				
				set the focused of text field SEARCH_TEXT_FIELD_indx of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str to true
				delay STANDARD_DELAY
				click static text HUD_Window_Title_str of window HUD_Window_Title_str
				delay STANDARD_DELAY
				click text field SEARCH_TEXT_FIELD_indx of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				delay STANDARD_DELAY
				if the focused of text field SEARCH_TEXT_FIELD_indx of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str is true then
					keystroke Search_Text_str
					keystroke {return}
				else
					display dialog "Text Field not getting focus"
				end if
				
				-- Enable the search
				if (the value of checkbox ENABLE_CHECKBOX_name of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str) is not 1 then
					click checkbox ENABLE_CHECKBOX_name of group (BASE_SEARCH_GROUP_indx + 1) of list HUD_LIST_indx of scroll area HUD_SCROLL_AREA_indx of window HUD_Window_Title_str
				end if
				
				-- Close the HUD
				click button HUD_CLOSE_BUTTON_indx of window HUD_Window_Title_str
				
			end tell
		end tell
		return true
	on error error_message
		display dialog error_message
	end try
	
end Create_Metadata_Smart_Album


Model: iMac 27-inch, Late 2012
AppleScript: AppleScript 2.3
Browser: Safari 537.73.11
Operating System: Mac OS X (10.8)

Try to add

activate application “Aperture”
just before the instruction : tell application “System Events”

Yvan KOENIG (VALLAURIS, France) jeudi 23 janvier 2014 09:48:42

Thanks Yvan.

Tried the ‘activate’ line but no success.:expressionless:
Any other suggestions?