Wednesday, May 18, 2022

#1 2003-07-27 09:05:42 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

gui multiple menu commands problem...

i'm trying to get this scrpit to select three menu commands but i get an error
"the do_menu handler is specified more than once"
it is, but i'm not sure how to separate each one from the previous one.
is there a command that will allow me to run three consecutive degments of code to select menu commands?
the application i'm usin does not give me a dictionary when i drag it into the script editor, so does this mean i can't script it?
thanks for the help!
--thomas


One mans trash is another mans treasure

Offline

 

#2 2003-07-27 10:01:47 am

jonn8
Member
From:: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: gui multiple menu commands problem...

It sounds like you are confusing a handler (also called a routine, subroutine, subhandler, or command) and a call to a handler. The following code defines a handler for selecting a menu item and calls it three times. You only define the handler once and then you can call it as many times as you wish. The only caveat to it is that if you call it within the tell block of an application, make sure you add "my" before it so that the application knows to use the script's command and not the application's itself (which probably doesn't exist):

my do_menu("Finder", "File", "New Folder")
my do_menu("Finder", "File", "New Folder")
my do_menu("Finder", "File", "New Folder")

on do_menu(app_name, menu_name, menu_item)
     try
          tell application app_name to activate
          tell application "System Events"
               with timeout of 300 seconds -- 5 minutes
                    tell menu bar 1 of process app_name to click menu item menu_item of menu menu_name
               end timeout
          end tell
          return true
     on error error_message
          return false
     end try
end do_menu

--this script was automatically tagged for
--color coded syntax by Script to Markup Code
--written by Jonathan Nathan


Jon

Offline

 

#3 2003-07-27 12:34:42 pm

jonn8
Member
From:: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: gui multiple menu commands problem...

Of course, the above requires that you have installed the GUI Scripting beta which was developed to script apps that don't support native scripting on their own (a category which appears to include the app you're trying to script).

Jon

Offline

 

#4 2003-07-27 12:54:38 pm

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Re: gui multiple menu commands problem...

jonn8,
thanks for the reply.
i am new to this and am a bit confused.
i can't name the handler once, because the menu i am asked to command is different in each of the three commands.
my example is this:
as you can see, the menu functions are different, so my handler must be different for each, right?
thanks again for the help.oh and yes, i am running the beta.
--thomas


tell application "Audio Hijack Pro" to activate
open location "http://cartalk.cars.com/Radio/Show/Audi … RA/all.ram"
end


(* EXECUTE MENU ITEM *)
-- Audio Hijack Pro, File, Open Preset

if my do_menu("Finder", "Finder", "Hide Others") is false then error number -128
if my do_menu("Finder", "View", "Clean Up") is false then error number -128

on do_menu(app_name, menu_name, menu_item)
    try
        -- bring the target application to the front
        tell application app_name
            activate
        end tell
        tell application "System Events"
            with timeout of 300 seconds -- 5 minutes
                tell process app_name
                    tell menu bar 1
                        click menu item menu_item of menu menu_name
                    end tell
                end tell
            end timeout
        end tell
        return true
    on error error_message
        return false
    end try
end do_menu

(* EXECUTE MENU ITEM *)
-- Audio Hijack Pro, Control, Start Hijacking

if my do_menu("Finder", "Finder", "Hide Others") is false then error number -128
if my do_menu("Finder", "View", "Clean Up") is false then error number -128

on do_menu(app_name, menu_name, menu_item)
    try
        -- bring the target application to the front
        tell application app_name
            activate
        end tell
        tell application "System Events"
            with timeout of 300 seconds -- 5 minutes
                tell process app_name
                    tell menu bar 1
                        click menu item menu_item of menu menu_name
                    end tell
                end tell
            end timeout
        end tell
        return true
    on error error_message
        return false
    end try
end do_menu

(* EXECUTE MENU ITEM *)
-- Audio Hijack Pro, Control, Start Recording

if my do_menu("Finder", "Finder", "Hide Others") is false then error number -128
if my do_menu("Finder", "View", "Clean Up") is false then error number -128

on do_menu(app_name, menu_name, menu_item)
    try
        -- bring the target application to the front
        tell application app_name
            activate
        end tell
        tell application "System Events"
            with timeout of 300 seconds -- 5 minutes
                tell process app_name
                    tell menu bar 1
                        click menu item menu_item of menu menu_name
                    end tell
                end tell
            end timeout
        end tell
        return true
    on error error_message
        return false
    end try
end do_menu


One mans trash is another mans treasure


Filed under: System, audio

Offline

 

#5 2003-07-27 10:23:53 pm

jonn8
Member
From:: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: gui multiple menu commands problem...

i can't name the handler once, because the menu i am asked to command is different in each of the three commands.
my example is this:
as you can see, the menu functions are different, so my handler must be different for each, right?


Um, what? No. The whole point of the handler is that it accepts variables so you can send it commands that do things based on variable input. Just so we're straight, the handler is the code starting with "on do_menu..." and ending with "end do_menu". This is the part that you only put in once, usually at the top or bottom of the script. If you include this code only once, your script will compile. There is a larger problem because all you seem to be doing is calling the Finder over and over. This was example code and not meant as the code for you to actually use. Here is a script that I think does what you intended (not sure about the beginning since I don't have Audio Hijack Pro and this may give you some problems):

tell application "Audio Hijack Pro"
     activate
     open location "http://cartalk.cars.com/Radio/Show/Audi … RA/all.ram"
end tell

my do_menu("Audio Hijack Pro", "File", "Open Preset")
my do_menu("Audio Hijack Pro", "Control", "Start Hijacking")
my do_menu("Audio Hijack Pro", "Control", "Start Recording")

on do_menu(app_name, menu_name, menu_item)
     try
          tell application app_name to activate
          tell application "System Events"
               with timeout of 300 seconds -- 5 minutes
                    tell menu bar 1 of process app_name to click menu item menu_item of menu menu_name
               end timeout
          end tell
          return true
     on error error_message
          return false
     end try
end do_menu

--this script was automatically tagged for
--color coded syntax by Script to Markup Code
--written by Jonathan Nathan


It would do you good to read through the AppleScript Language Guide or an AppleScript book just to learn the basics.

Jon

Offline

 

#6 2003-07-28 08:50:54 pm

jonn8
Member
From:: New York
Registered: 2003-05-01
Posts: 1766
Website

Re: gui multiple menu commands problem...

After installing the GUI Scripting beta, did you make sure that “Enable access for assistive devices” is turned on in the Universal Access Preference pane? Without this, GUI Scripting won't be able to control the menus and other interface elements.

Jon

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)