How can I select the menu Item?

hi all:D

I got this script from this site but when I try to run it I got this message with line four

AppleScript Error
System events got an error:
NSReceiverEvaluationScriptError: 4

can any one help me?:confused:

(mainly it is for sellect the menu it is not get the correct menu even in some othe applications
what I need to know how I can select the menu item)

tell application “System Events”
launch
tell process “Printer Setup Utility”
click menu item “Add Printer.” of menu 1 of menu bar item “Printers” of menu bar 1
tell window “Printer Browser”
click button “IP Printer” of tool bar 1
set value of combo box 1 of group 1 of group 1 of group 2 of group 1 to “xxx.xxx.x.x”
delay 2
keystroke tab & tab & return
click button “Add”
end tell

end tell

end tell
quit application “Printer Setup Utility”

Hi, alsadr. Here are some things you could look at.

  1. System Events should be running anyway, so you don’t need to tell it to launch.
  2. When using GUI scripting (menus, etc.), it’s a good idea to set the ‘frontmost’ of the process to ‘true’ first to ensure that its menus are active.
  3. The three dots in menu item names “.” are usually an ellipsis character (Option-; on English-language keyboards), not three full stops.
tell application "Printer Setup Utility" to activate
tell application "System Events" 
	tell application process "Printer Setup Utility"
		set frontmost to true 
		click menu item "Add Printer." of menu 1 of menu bar item "Printers" of menu bar 1 
		tell window "Printer Browser" 
			click button "IP Printer" of tool bar 1 
			set value of combo box 1 of group 1 of group 1 of group 2 of group 1 to "xxx.xxx.x.x" 
			delay 2 
			keystroke tab & tab & return 
			click button "Add" 
		end tell 
        
	end tell 
    
end tell 
tell application "Printer Setup Utility" to quit

Printer Setup Utility is apparently scriptable in its own right, but, not having a printer attached to my Tiger machine, I’m not able to experiment to see if what you want to achieve can be done that way instead.

By the way, this “Mac OS” forum is meant specifically for versions of Mac OS before Mac OS X. (The title’s a little ambiguous, I’m afraid.) You’re more likely to get replies to your particular queries if you ask in the “OS X” forum.

hi Nigel
thank you for your help but still not working
this is the Event Log I got when I run what you give me


tell application “Printer Setup Utility”
activate
end tell
tell application “System Events”
set frontmost of application process “Printer Setup Utility” to true
click menu item “Add Printer.” of menu 1 of menu bar item “Printers” of menu bar 1 of application process “Printer Setup Utility”
“System Events got an error: NSReceiverEvaluationScriptError: 4”

ok … let me clear some points
system 10.4.5
G4 and G5
the system is running under multi lang. ( I have English, Français and Arabic)
the order for sorted list start with Arabic (even if I change it to English it is not working )
any other information you like to know ask me … and I will update you

thank you
ahmed alsadr

Moved to OS X

Hi again, alsadr. (Do you prefer “alsadr” or “ahmed”?)

I’ve been experimenting to see what sort of situation can cause the error you’re receiving. Assuming for the moment that there’s no problem with your multi-lingual system, the main possible cause seems to be the right window not being open at the right time for the command being given. For instance, the “Add Printer.” menu item only works if the “Printer List” window is open and in front. The following version of the script works perfectly on my own (English only) machine under a variety of conditions. If it doesn’t work for you, you could try inserting short delays before the lines that give you trouble.

tell application "Printer Setup Utility" to activate

tell application "System Events"
	tell application process "Printer Setup Utility"
		set frontmost to true
		if (count (windows whose description is "dialog")) > 0 then
			-- Printer Setup Utility is opening with no printers. Click the splash dialog's "Add" button.
			click button "Add" of window 1
		else
			-- Printer Setup Utility is already open and/or printer(s) are already defined.
			-- The Printer List window must be frontmost in order to use the "Add Printer." menu item.
			if not ((window "Printer List" exists) and (name of window 1 is "Printer List")) then keystroke "l" using command down
			click menu item "Add Printer." of menu 1 of menu bar item "Printers" of menu bar 1
		end if
		tell window "Printer Browser"
			click button "IP Printer" of tool bar 1
			set value of combo box 1 of group 1 of group 1 of group 2 of group 1 to "xxx.xxx.x.x"
			keystroke tab & tab & tab & return
			click button "Add"
		end tell
		
	end tell
	
end tell

-- If the specified printer already exists, a dialog appears asking for confirmation,
-- but the following line makes Printer Setup Utility quit without adding the duplicate. 
tell application "Printer Setup Utility" to quit

If the problem does turn out to be some menu-naming confusion on your multi-lingual system, it may be possible to use the numbers, rather than the names, of the menu items and buttons. This is great for avoiding language problems, but could cause problems if the menu order or button order changes in later versions of Printer Setup Utility.

hi Nigel Garvey
it is ok for me … my name is ahmed and family name is alsadr … so both of them ok

I tried it but still not working …
I don’t know why? :frowning:

the Event Log is:

tell application “Printer Setup Utility”
activate
current application
activate
end tell
tell application “System Events”
set frontmost of application process “Printer Setup Utility” to true
count every window of application process “Printer Setup Utility” whose description = “dialog”
0
exists window “Printer List” of application process “Printer Setup Utility”
false
keystroke “l” using command down
click menu item “Add Printer.” of menu 1 of menu bar item “Printers” of menu bar 1 of application process “Printer Setup Utility”
“System Events got an error: NSReceiverEvaluationScriptError: 4”

I am sure it is ok but I don’t know where is the bug
I will try it again

by the the way …
I am useing Printer Setup Utility ver 4
sys 10.4.5
G 5 Dual 2 GHz

thank you
waiting your reply
ahmed alsadr

Aha! Well spotted, Jacques. Not having GUI Scripting enabled would produce precisely that error at that point. It can be enabled, as you say, in System Preferences’ Universal Access pane, or with “AppleScript Utility.app” in the “AppleScript” folder in the main “Applications” folder.

Does that solve the problem for you, ahmed?

Hi all
yes … NG it solve the problem
it is working fine and I did it
hope to publish it for same poor information users like me :wink: so we can know more about the system and solve our problem asap
again thank you all and thank you Jacques & Nigel Garvey
ahmed alsadr