Automator "Watch Me Do" using Command Down transition to AppleScript question.

I am using "Watch Me Do" to click two separate texts while holding down the Command button. It works when I replay the "Watch Me Do" block...in that it selects both texts at the same time:

However, when I drag these commands down to try to modify them (and run them faster) in AppleScript, it doesn't select them both (It doesn't work like it has the Command button held down). It just selects them individually one after the other:

on run {input, parameters}

-- Click the text “DAAF”

delay 0.627221

set timeoutSeconds to 2.0

set uiScript to "click static text 1 of group 9 of UI Element 1 of scroll area 2 of UI Element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window XXXX\": Used by Apps\" of application process \"Safari\""

my doWithTimeout(uiScript, timeoutSeconds)


-- Click the text “DB2G”

delay 1.049824

set timeoutSeconds to 2.0

set uiScript to "click static text 1 of group 11 of UI Element 1 of scroll area 2 of UI Element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window XXXX\": Used by Apps\" of application process \"Safari\""


my doWithTimeout(uiScript, timeoutSeconds)

return input

end run


on doWithTimeout(uiScript, timeoutSeconds)

set endDate to (current date) + timeoutSeconds

repeat

try

run script "tell application \"System Events\"

" & uiScript & "

end tell"

exit repeat

on error errorMessage

if ((current date) > endDate) then

error "Can not " & uiScript

end if

end try

end repeat

end doWithTimeout


This above is all the code it generated when I copied the "Watch Me Do" commands. I have tried running the generated AppleScript code WHILE holding the command key down, but, again, it only selects each text separately.


I can't reveal the site (work thing), and I tried "using {command down}", and "with command key down". I also tried using the command key down with command key up later and took about an hour to figure out how to deal with a "stuck" command key. (Learning the try method now so I have an escape, but I had to open a new "Run AppleScript" with just the line: command key down in the same automator window in case I screwed it up again).


Since the "Watch Me Do" block CAN do this while it knows to hold down the command key, I have to believe that it can be Scripted. I have looked up uiScript and downloaded and started several AppleScript books trying to find this secret. Most of the pages online I have found to be 8-10 years old on topics identifying the "Class" name or ID, but nothing on Command Key.


If anyone has any ideas or can point me in a direction to figure this out, I would appreciate the time you take. Cheers


iMac 21.5″, macOS 10.13

Posted on Jan 30, 2022 9:40 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 31, 2022 12:37 PM

Lots to digest here.


First off, I normally advocate against using UI events whenever possible. They are messy, unreliable, and generally the wrong way to go about solving a problem. Sometimes they are a necessary evil, though.


If you're determined/destined to use UI events to do this, rather than to interact directly with the app (and Safari has an above-average (for Apple) AppleScript dictionary, plus most page-level things can be done via JavaScript, too.


In any case, the crux of your problem is that there is no support for 'command down' (or any modifier keys for that matter) with the click command. click supports one of two parameters - an object to click, or a location on the screen. That's it.


To do what you want you need to wrap your click in the (hidden? secret? deprecated?) commands key down and key up to emulate pressing the command key before the click, and releasing the key afterwards. Finding this right incantation requires some arcane knowledge, but fortunately some of us have already paid the price for you.


For the record I have no idea why you are passing a text representation of your click command to a run script command. Why don't you just run it directly?


tell application "System Events"
   key down 55 -- ssshh! secret code for the command key
   click static text 1 of group 75 of scroll area 2 of tab 7 of window 93 -- or whatever
   key up 55 -- release the command key!
end tell


It's possible that 55 is the incorrect number based on age/language settings/keyboard model, etc. Let me know if this doesn't work and we can dig into how to find the right number.

Similar questions

4 replies
Question marked as Top-ranking reply

Jan 31, 2022 12:37 PM in response to Sparkey33

Lots to digest here.


First off, I normally advocate against using UI events whenever possible. They are messy, unreliable, and generally the wrong way to go about solving a problem. Sometimes they are a necessary evil, though.


If you're determined/destined to use UI events to do this, rather than to interact directly with the app (and Safari has an above-average (for Apple) AppleScript dictionary, plus most page-level things can be done via JavaScript, too.


In any case, the crux of your problem is that there is no support for 'command down' (or any modifier keys for that matter) with the click command. click supports one of two parameters - an object to click, or a location on the screen. That's it.


To do what you want you need to wrap your click in the (hidden? secret? deprecated?) commands key down and key up to emulate pressing the command key before the click, and releasing the key afterwards. Finding this right incantation requires some arcane knowledge, but fortunately some of us have already paid the price for you.


For the record I have no idea why you are passing a text representation of your click command to a run script command. Why don't you just run it directly?


tell application "System Events"
   key down 55 -- ssshh! secret code for the command key
   click static text 1 of group 75 of scroll area 2 of tab 7 of window 93 -- or whatever
   key up 55 -- release the command key!
end tell


It's possible that 55 is the incorrect number based on age/language settings/keyboard model, etc. Let me know if this doesn't work and we can dig into how to find the right number.

Feb 1, 2022 1:31 PM in response to Sparkey33

> The original script came from dragging the recorded commands down into a Run AppleScript window in Automator and that was just the way the recorder saw to make the required clicks.


I recognize the 'watch me do' nature of your original post. I rarely find these create viable long-term strategies.


> Can AppleScript find this on the page without all of the "scroll area2 of UI element 1 of scroll area 1..." if it knows to look for "DAAF" specifically?


It is hard to get AppleScript to interact directly with page content in Safari. The expectation is that JavaScript is used at this level. AppleScript can interact with the high-level application data, but web page content is one level removed.


You can use AppleScript to extract the page source (in HTML), and use AppleScript's text parsing capabilities to process the text (e.g. extract text data from it), but this may or may not reflect the visual content (e.g. stylesheet data, JavaScript actions, etc.). If the page content is pretty static, though, then this may be an easier path.


For example, this script will grab the HTML source of the front document and grab all the text between the two keywords:


set firstKeyword to "DAAF"
set secondKeyword to "D2BG"

tell application "Safari"
	-- get the raw HTML of the front document
	set thePageContent to source of document 1
	set {oldTID, my text item delimiters} to {my text item delimiters, firstKeyword}
	text item 2 of text items of thePageContent
	set myText to text item 2 of text items of thePageContent
	set my text item delimiters to secondKeyword
	set myText to text item 1 of text items of myText
	set my text item delimiters to oldTID
	return myText
	
end tell


This completely avoids UI scripting and doesn't care about groups, scroll areas, etc., etc. and is a better way of leveraging AppleScript.

Feb 1, 2022 8:45 AM in response to Camelot

First off, Camelot, thank you for taking the time to reply!


The original script came from dragging the recorded commands down into a Run AppleScript window in Automator and that was just the way the recorder saw to make the required clicks.

When I play recorded commands as is, it will select both things with the command key down (highlighting both), however, running the exact same code (dragged into a Run AppleScript box) will not...it just clicks each selection as if the command key wasn't down. I used your suggestions and got it to click on the actual static text 1 using this next code, however, even with the key down 55, it did not highlight both, but just switched to the next selection as well. I even tried using command key down (in place of key down 55), as well as "using {command down}" at the end of the click command. Good news, the click command worked (after I removed the pesky \'s from the generated code), so there is hope to have this run in System Events.

Ideally, I am trying to be able to have the code search for the selection using it's name in place of static text 1...in this case, find the word "DAAF", click on it with the Cmd key down, then find the word "DB2G" and click on it with the Cmd key down-- highlighting both of them. This is so it can look later on for a different text instead of just clicking that same area. I have been looking at Javascript to try to get it to ID the text using ClassName, but to no avail.


Also, I locked down my command key again, so I now have a failsafe (or I could us a try) "Run AppleScript" at the bottom of the Automator window just in case:

I had to run this when the first script failed at the first "click" command and key down 55 had already been activated. That is a quandary as it highlights everything everywhere you move your mouse or click since it thinks your command key is down... , but just running the key up script resolves it quickly :)


Thanks again, I know this can be done other ways (I have used Katalon Recorder as well as iMacros (before it devolved into worthless) to find and select these elements, but AppleScript is interesting to work with and I see it as a challenge to make it work).

Cheers

Feb 1, 2022 9:14 AM in response to Sparkey33

More info:

This is the UI Browser report for one of the elements I am trying to select. It is truncated down, but I would like to have AppleScript identify it by the attribute value: string: "DAAF" (or use Javascript to do it as well, but I don't know if there is a findValue script)...that way, I could modify the value to the next one, say, "DB2G" and have it select it based on that property.

Can AppleScript find this on the page without all of the "scroll area2 of UI element 1 of scroll area 1..." if it knows to look for "DAAF" specifically? I know Ctrl-F will find this on the page, so I know it's possible...just getting it coded to duplicate that action directly (versus coding Ctrl-F and manually searching...which seems wonky).


Thanks again, I know you guys have done all of the hard work stomping through code until you finally get that "Aha!" moment when it does what you tell it to do! I do appreciate that and I like that aspect of coding, however, my Fortran and Cobol programming from the 80's and 90's only get in my way...the newer languages are easier, but have their distinct syntaxes...


Cheers



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator "Watch Me Do" using Command Down transition to AppleScript question.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.