Friday, December 2, 2022

#1 2022-09-26 10:53:34 pm

JeffB
Member
Registered: 2022-09-25
Posts: 5

Making applescript recognize other boxes

I have a script that I have written that is intended to record an event and shut down at a specific time.  My problem is that I have to ensure that the right audio interface is loaded.

When I open up the program, It will properly respond to "click" commands in that window.  If my action opens up another widow, it will not respond to "click" commands in the new window.

How can I make this window active?

I apologize for my coding.  This is my first attempt.  I am open to all suggestions.

Thanks!

Applescript:

tell application "Digital Performer" to activate
tell application "System Events"
   delay 30
   click at {1204, 810} (this opens the interface window and works as intended)
   delay 5
   click at {790, 530}. (this selects the correct interface in the new window but isn't recognized)
   delay 5
   click at {980, 810}. (this closes the window but isn't recognized)
   delay 20
   key code 31 using command down
   delay 0.5
   tell application "Finder"
       open file "Macintosh HD:Users:JBrown:Desktop:Untitled Project:Untitled.dpdoc"
       delay 45
       tell application "System Events"
           tell application process "Digital Performer"
               key code "85"
               delay 30
               key code "49"
               delay 30
               key code 13 using {command down, shift down}
               delay 10
               click at {860, 480}
               delay 2
               click at {870, 530}
               delay 10
           end tell
       end tell
   end tell
end tell
end

Offline

 

#2 2022-09-27 02:14:49 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1089

Re: Making applescript recognize other boxes

If I understand you correctly. To make a specific window activate... in other words to become front window

You use the activate command
https://developer.apple.com/library/arc … _cmds.html

ex.

Applescript:

tell application "Finder"
activate
end tell


Node-RED makes it easy to automate IoT

Offline

 

#3 2022-09-27 03:18:30 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 616

Re: Making applescript recognize other boxes

You have nested tell blocks, which is never a good idea. And a bare 'end' at the end, which should not be there.
And yes, you should first activate the application you want to control. When telling Finder to open a file it's not certain Digital Performer is still frontmost.

I have no idea if this solves your problem, but here's the amended code:

Applescript:

tell application "Digital Performer" to activate
tell application "System Events"
   delay 30
   click at {1204, 810}
   delay 5
   click at {790, 530}
   delay 5
   click at {980, 810}
   delay 20
   key code 31 using command down
   delay 0.5
end tell

tell application "Finder"
   open file "Macintosh HD:Users:JBrown:Desktop:Untitled Project:Untitled.dpdoc"
   delay 45
end tell

tell application "Digital Performer" to activate

tell application "System Events" to tell application process "Digital Performer"
   key code "85"
   delay 30
   key code "49"
   delay 30
   key code 13 using {command down, shift down}
   delay 10
   click at {860, 480}
   delay 2
   click at {870, 530}
   delay 10
end tell

Oh, and please don't type additional text into script code in your post. It will likely screw up formatting.

Offline

 

#4 2022-09-27 09:07:18 am

estockly
Member
Registered: 2009-01-03
Posts: 158

Re: Making applescript recognize other boxes

alastor933 wrote:

You have nested tell blocks, which is never a good idea. And a bare 'end' at the end, which should not be there.
And yes, you should first activate the application you want to control. When telling Finder to open a file it's not certain Digital Performer is still frontmost.



AppleScript was designed to handle nested tell blocks, that's really not an issue.

Nor does Finder make itself frontmost when a command is executed. Sending an activate command (0r setting a processes frontmost to true in SE) is a good idea if it needs to be frontmost for the script, especially if something else could have been brought to the front by another script or one of those pesky users.

Online

 

#5 2022-09-27 09:22:27 am

estockly
Member
Registered: 2009-01-03
Posts: 158

Re: Making applescript recognize other boxes

JeffB wrote:



Applescript:

click at {790, 530} -- (this selects the correct interface in the new window but isn't recognized)



When using GUI scripting, I find the "click at" commands to be the least reliable. Clicking buttons, selecting menu choices, and key codes all seem to work more reliably.

Also, this is a bit problematic:

tell application "Finder"
        open file "Macintosh HD:Users:JBrown:Desktop:Untitled Project:Untitled.dpdoc"
        delay 45



What application would that open? It would be better if you could send that command to the application (many unscriptable applications will respond to the open command)

if that doesn't work, you could write a routine for System Events to wait until that file's window is open in the application process. That's a little tricky, and we could help you with it, but it's probably worth it to eliminate a 45 second delay.

But, for starters, try this:

Applescript:


tell application "System Events"
   tell application process "Digital Performer"
       set its frontmost to true
       delay 30
       click at {1204, 810} -- (this opens the interface window and works as intended)
       delay 5
       click at {790, 530} -- (this selects the correct interface in the new window but isn't recognized)
       delay 5
       click at {980, 810} -- (this closes the window but isn't recognized)
       delay 20
       key code 31 using command down
       delay 0.5
   end tell
   tell application "Finder" to open file "Macintosh HD:Users:JBrown:Desktop:Untitled Project:Untitled.dpdoc"
       delay 45
       tell application "System Events"
           tell application process "Digital Performer"
               key code "85"
               delay 30
               key code "49"
               delay 30
               key code 13 using {command down, shift down}
               delay 10
               click at {860, 480}
               delay 2
               click at {870, 530}
               delay 10
           end tell
       end tell
   end tell

Online

 

#6 2022-09-27 04:23:31 pm

JeffB
Member
Registered: 2022-09-25
Posts: 5

Re: Making applescript recognize other boxes

Man, I really appreciate the help since I just started learning this stuff so PLEASE tell me when I do goofy crap!

That being said, I cleaned up the code and it does make more logic but the problem still persists.

I had read that clicks are not good, especially since this menu can move and the program remembers its LAST position.  The menu itself IS accessible through the menu....but I can open THAT ONE up with a click with no issues.   The menu that it opens up is only selectable with a mouse the best I can tell. But the click command does not seem to work on that window.   That is why I am assuming that I have to do something to "activate" that window to make click commands recognize it.   Clicks still continue to work on the first menu even if the menu it opens is on top of it.

The file referred to in the "open file" line also will open the application Digital Performer if it is closed

This was really two different scripts put together.  The bottom half where it opens the file till the end works as intended.   It opens a file, starts the recording process, stops it, and closes the file as I want it.  Although I have never tested it beyond 30 seconds.   The real time will be 1 hour and 15 min.....4500 sec.  Is there a smarter way to do that?

My problem starts at the point where it says to click at 790,530.  It just goes silent after that.

As far as some of the delays go,  this is a digital audio recording program that is tracking 54 tracks of audio every Sunday with a ton of plugins.   It just takes time to finish loading.  If that isn't what you're addressing, then help me pls.

Once again,  I am thrilled so many people are trying to help.  If I can make this work,  I will start looking at other uses to make my life easier.

Thanks

Model: Macbook Pro M1 Max
AppleScript: 2.11
Browser: Safari 537.36
Operating System: macOS 12

Offline

 

#7 2022-09-27 06:08:33 pm

estockly
Member
Registered: 2009-01-03
Posts: 158

Re: Making applescript recognize other boxes

JeffB wrote:

Man, I really appreciate the help since I just started learning this stuff so PLEASE tell me when I do goofy crap!

That being said, I cleaned up the code and it does make more logic but the problem still persists.



Go ahead and post your updated script and we can have a look.

I had read that clicks are not good, especially since this menu can move and the program remembers its LAST position.  The menu itself IS accessible through the menu....but I can open THAT ONE up with a click with no issues.   The menu that it opens up is only selectable with a mouse the best I can tell. But the click command does not seem to work on that window.   That is why I am assuming that I have to do something to "activate" that window to make click commands recognize it.   Clicks still continue to work on the first menu even if the menu it opens is on top of it.



For me, it's the "click at" which I find problematic. There are variables like window size that can throw that off.

If you can identify the button itself, by name or index, clicking that is usually better. If you have UI Browser, or Script Debugger, that can help you get the ID of the button. I think you'd find that more reliable.


The file referred to in the "open file" line also will open the application Digital Performer if it is closed
This was really two different scripts put together.  The bottom half where it opens the file till the end works as intended.   It opens a file, starts the recording process, stops it, and closes the file as I want it.  Although I have never tested it beyond 30 seconds.   The real time will be 1 hour and 15 min.....4500 sec.  Is there a smarter way to do that?



There could be. What I was thinking is you were giving the script extra time to make sure the file had fully loaded. Seems like something else.


My problem starts at the point where it says to click at 790,530.  It just goes silent after that.



As far as some of the delays go,  this is a digital audio recording program that is tracking 54 tracks of audio every Sunday with a ton of plugins.   It just takes time to finish loading.  If that isn't what you're addressing, then help me pls.



If it is a finishing loading issue, then the tell application X to open file might help. Otherwise, you can experiment with system events.

Online

 

#8 2022-09-27 07:26:38 pm

JeffB
Member
Registered: 2022-09-25
Posts: 5

Re: Making applescript recognize other boxes

Ok,  Here is the updated script.  The click numbers are different to account for testing on my laptop.  Is it possible to post an image to show my desktop here?

Applescript:

tell application "Digital Performer" to activate
delay 20
tell application "System Events" to tell application process "Digital Performer"
   click at {1203, 808}
   delay 6
   click at {228, 537}
   delay 5
   click at {420, 815}
   delay 10
end tell

tell application "Finder"
   open file "Macintosh HD:Users:JBrown:Desktop:Untitled Project:Untitled.dpdoc"
   delay 20
end tell
tell application "Digital Performer" to activate
tell application "System Events"
   tell application process "Digital Performer"
       key code "85"
       delay 30
       key code "49"
       delay 30
       key code 13 using {command down, shift down}
       delay 10
       click at {860, 480}
       delay 2
       click at {870, 530}
       delay 10
       
   end tell
end tell

end

Offline

 

#9 2022-09-28 07:16:34 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1089

Re: Making applescript recognize other boxes

@JeffB
Other approach could be to use command utility like cliclick https://github.com/BlueM/cliclick

Make sure the target app is frontmost (activated)

You could access the command cliclick with AS command do shell script

This approach could make your code more compact...

Last edited by Fredrik71 (2022-09-28 07:17:31 am)


Node-RED makes it easy to automate IoT

Offline

 

#10 2022-09-28 10:24:22 am

JeffB
Member
Registered: 2022-09-25
Posts: 5

Re: Making applescript recognize other boxes

Not familiar with that at all.  Is that something that works with/inside the applescript?  If so, how would you use it?

Thanks

Offline

 

#11 2022-09-29 01:22:51 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1089

Re: Making applescript recognize other boxes

If you do...

Applescript:

do shell script "/opt/homebrew/bin/cliclick c:20,20"

It will do left click on mouse at Apple icon in menu bar. (Top left)

Ps. I installed the cliclick with brew... so that's why the path maybe look little different.

You also maybe need to make sure the Accessibility in Security & Privacy has access to terminal.

Last edited by Fredrik71 (2022-09-29 01:23:20 pm)


Node-RED makes it easy to automate IoT

Offline

 

#12 2022-09-29 10:08:39 pm

JeffB
Member
Registered: 2022-09-25
Posts: 5

Re: Making applescript recognize other boxes

ok, explain this to me like I am a musician....not a programer....(this is true)

With this line:

do shell script "/opt/homebrew/bin/cliclick c:20,20"

If I were to use this in applescript, how would I write it?   What is the function of "/opt/homebrew/bin" section? 

I get the cliclick c:20,20 is the command to click at location 20,20 correct?

Sorry to be slow...I just thought that writing a script to click on a box on a window would have been so much easier.  The first box is easy,  it's the second box that is causing me fits.

Help me figure out the command pls

Offline

 

#13 2022-09-30 07:55:53 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1089

Re: Making applescript recognize other boxes

JeffB wrote:

do shell script "/opt/homebrew/bin/cliclick c:20,20"


do shell script is AppleScript command to execute unix command.

JeffB wrote:

What is the function of "/opt/homebrew/bin" section?


Its not a function is the path for brew package manager to store binary executable files.
In other words if you have brew you could do: brew install cliclick
The executable file will then be installed /opt/homebrew/bin if you have Apple Silicon CPU
if you have a Intel it will be stored in /usr/local/bin


JeffB wrote:

I get the cliclick c:20,20 is the command to click at location 20,20 correct?


cliclick is the command the rest is parameters
c:20,20 is same as left mouse click : x value coordinate, y value coordinate on the screen.
So x-coordinate of 0 and y-coordinate of 0 is left top corner of your screen.

if you type cliclick -h you could see different options

I'm not suggestion this is a solution for you, but it looks to me from your message you like to click somewhere in the screen and you use x and y coordinate for that. I do not have the target application to try myself. But its always a good praxis to test or do prototypes of some examples to get more knowledge of something.


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)