Thursday, September 21, 2017

#1 2016-09-17 08:23:58 am

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

AppleScript UI Scripting Slow?

Thanks for accepting me into your community!

I've been spending a lot of time searching these forums the last few days - it's been INCREDIBLY helpful, so thank you!

I've recently been trying to solve a bunch of problems to make editing in Final Cut Pro (FCPX) better for the end user.

I've been using an awesome scripting tool called Hammerspoon (similar in function to Automator), which uses the Lua scripting engine - however, it also supports AppleScript, which is where I've been doing most of the work.

I'm new to AppleScript, so everything I've been doing has been very much through trial and error. I'm a filmmaker - not a programmer.

Even though it still needs a lot of work - I've already released my scripts on our blog for anyone who's interested, to basically just gauge if people are interested in this kind of thing, and also get user feedback on any bugs that pop up, etc.

https://latenitefilms.com/blog/final-cu … tch-frame/

It all seems to work fine - however I've hit some road-blocks that I'd love your advice on to try and solve.

1) The biggest issue I have is speed.

For example, if I have 10 clips in the FCPX browser, and run the following AppleScript it works instantly:

Applescript:


activate application "Final Cut Pro"
tell application "System Events"
   tell process "Final Cut Pro"
       get position of value indicator 1 of group 1 of scroll area 2 of splitter group 1 of group 7 of splitter group 1 of window "Final Cut Pro"
       display dialog the result as string
   end tell
end tell

However, if there's 800 clips in the FCPX browser, then the above AppleScript takes about 6 seconds to process. If there's 1000's of clips, then it takes ages, which is a big issue for what I'm trying to achieve.

Is there any way to speed this up?

Given that I'm asking AppleScript something very specific, I would have ASSUMED that how many other "groups" are in the program wouldn't make any difference, but apparently not! Any suggestions would be greatly appreciated!

2) Another question is about designing efficient code. For example, I feel like there's definitely a better way to do this (using 'whose' statements for example) - but I couldn't work it out. I feel like this is definitely something you could do in possibly even one line of code. Any suggestions on how I could improve sections of code like the below:

Applescript:


activate application "Final Cut Pro"
tell application "System Events"
   tell process "Final Cut Pro"
       set selectedClipNumber to 0
       set clipCounter to 1
       repeat
           try
               get selected of UI element clipCounter of UI element 1 of scroll area 1 of splitter group 1 of group 6 of splitter group 1 of window "Final Cut Pro"
               set currentClip to the result
               if currentClip then
                   set selectedClipNumber to clipCounter
                   exit repeat
               end if
           on error
               exit repeat
           end try
           set clipCounter to clipCounter + 1
       end repeat
       display dialog selectedClipNumber
   end tell
end tell

3) Another issue I've run into is that there's some UI elements, that I simply can't work out how to access via UI Scripting. I've been using 'UI Browser' and 'Accessibility Inspector' to work out what's what - but there seems to be a few items in FCPX that UI Browser's Screen Reader struggles with - for example:

Screen_Shot_2016_09_17_at_6_59_06_PM.png

What do these null objects mean? How do I deal with this using AppleScript?

4) If anyone's really keen and interested, I would LOVE some advice on how I can improve the overall speed and efficiency of all the AppleScript I've thrown together (you can download the entire Hammerspoon script on the blog listed above).

Any questions let me know! Thanks in advance!

Best Regards, Chris!

Model: MacBook Pro (Retina, 15-inch, Early 2013)
AppleScript: AppleScript 2.5
Browser: Safari 601.7.7
Operating System: Other

Offline

 

#2 2016-09-17 09:08:19 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3192

Re: AppleScript UI Scripting Slow?

I don't own Final Cut (Pro or not).

You may try to use :

Applescript:

activate application "Final Cut Pro"
tell application "System Events"
   tell process "Final Cut Pro"
       set frontmost to true # I always do that explicitly
       set everyStatus to get selected of every UI element of UI element 1 of scroll area 1 of splitter group 1 of group 6 of splitter group 1 of window "Final Cut Pro"
       
       set selectedClipNumber to 0
       set clipCounter to 1
       repeat with aStatus in everyStatus
           set clipCounter to clipCounter + 1
           if aStatus then
               set selectedClipNumber to clipCounter
               exit repeat
           end if
       end repeat
       display dialog selectedClipNumber
   end tell
end tell

which is supposed to grab the status of every clip in a single call.
After that, it apply a loop to the list of returned values.


Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) samedi 17 septembre 2016 16:06:19

Offline

 

#3 2016-09-17 07:55:28 pm

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

Re: AppleScript UI Scripting Slow?

Legend, thanks so much Yvan! Very much appreciated!

I had to change "set clipCounter to 0" - but apart from that your code works perfectly!

One of the other big challenges I have with this script is that there's so many different ways users can have the Final Cut Pro interface set up.

I've currently just been using a whole bunch of "try's" to get around this, which I know is not the best way to do this.

Any suggestions on how I can improve and simplify the below? I'm assuming I can use a bunch of "whose" statements, but I haven't been able to make it work.

I'd also still love to work out an answer to my first question in the original post, if anyone has any ideas!

Thanks in advance!

Applescript:


script highlightFCPXBrowserPlayhead
   --------------------------------------------------------------------------------                
   -- Error Messages:
   --------------------------------------------------------------------------------                                    
   set commonErrorMessageStart to "Opps! An error has occurred:" & return & return
   set commonErrorMessageEnd to return & return & "Please take a screenshot and email it to:" & return & "chris@latenitefilms.com" & return & "...so we can try and come up with a fix." & return & return & "Thank you!"
   --------------------------------------------------------------------------------                                    
   -- Talk to FCPX:
   --------------------------------------------------------------------------------                                                                
   activate application "Final Cut Pro"
   tell application "System Events"
       tell process "Final Cut Pro"
           repeat with theIncrementValue from 3 to 9
               try
                   get position of value indicator 1 of group 1 of scroll area 2 of splitter group 1 of group theIncrementValue of splitter group 1 of window "Final Cut Pro"
                   return the result
               on error
                   -- Skip Error
               end try
           end repeat
           repeat with theIncrementValue from 3 to 9
               try
                   get position of value indicator 1 of group 1 of scroll area 1 of splitter group 1 of group theIncrementValue of splitter group 1 of window "Final Cut Pro"
                   return the result
               on error
                   -- Skip Error
               end try
           end repeat
           repeat with theIncrementValue from 3 to 9
               try
                   get position of value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group theIncrementValue of splitter group 1 of window "Final Cut Pro"
                   return the result
               on error
                   -- Skip Error
               end try
           end repeat
           try
               get position of value indicator 1 of group 1 of scroll area 1 of splitter group 1 of group 1 of splitter group 1 of window "Events"
               return the result
           on error
               -- Skip Error
           end try
           try
               get position of value indicator 1 of group 1 of scroll area 2 of splitter group 1 of group 1 of splitter group 1 of window "Events"
               return the result
           on error
               -- Skip Error
           end try
           --------------------------------------------------------------------------------                                                    
           -- If you get to here something went wrong:
           --------------------------------------------------------------------------------                                                    
           display dialog commonErrorMessageStart & "Unable to locate the Browser Playhead." & commonErrorMessageEnd buttons {"Close"} with icon caution
           return "failed"
       end tell
   end tell
end script
run script highlightFCPXBrowserPlayhead

Offline

 

#4 2016-09-17 10:00:56 pm

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

Re: AppleScript UI Scripting Slow?

Based on what I learnt from your example Yvan, I've had a go at re-writing this section of the script without using all the "try" statements.

I think it's probably a lot cleaner and less error-prone, but it still suffers from the big issue of if there's lots of items in the Final Cut Pro browser it runs incredibly slow. I'm also not sure that it's actually any faster than what I had originally, which is annoying.

If anyone has any suggestions on how I could improve performance, let me know. Thank you!

Applescript:


script highlightFCPXBrowserPlayhead
   --------------------------------------------------------------------------------                
   -- Error Messages:
   --------------------------------------------------------------------------------    
   set macOSVersion to system version of (system info)
   set fcpVersion to version of application "Final Cut Pro"
   set commonErrorMessageStart to "I'm sorry, but the following error has occurred:" & return & return
   set commonErrorMessageEnd to return & return & "macOS Version: " & macOSVersion & return & "FCP Version: " & fcpVersion & return & return & "Please take a screenshot of your entire screen and email it to the below address so that we can try and come up with a fix:" & return & return & "chris@latenitefilms.com" & return & return & "Thank you for testing!"
   
   --------------------------------------------------------------------------------                                    
   -- Talk to FCPX:
   --------------------------------------------------------------------------------                                                                
   activate application "Final Cut Pro"
   tell application "System Events"
       tell process "Final Cut Pro"
           set frontmost to true
           if window "Events" exists then
               --------------------------------------------------------------------------------    
               -- Using Second Monitor:
               --------------------------------------------------------------------------------                    
               if group 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of window "Events" exists then
                   --------------------------------------------------------------------------------    
                   -- List View:
                   --------------------------------------------------------------------------------    
                   if value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of window "Events" exists then
                       get position of value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of window "Events"
                       return the result
                   end if
               else
                   --------------------------------------------------------------------------------    
                   -- Filmstrip View:
                   --------------------------------------------------------------------------------    
                   if value indicator 1 of group 1 of last scroll area of splitter group 1 of group 1 of splitter group 1 of window "Events" exists then
                       get position of value indicator 1 of group 1 of last scroll area of splitter group 1 of group 1 of splitter group 1 of window "Events"
                       return the result
                   end if
               end if
           else
               --------------------------------------------------------------------------------    
               -- Using Single Monitor:
               --------------------------------------------------------------------------------    
               set selectedGroup to 0
               repeat with currentGroup from 1 to (count of groups of splitter group 1 of window "Final Cut Pro")
                   if group 1 of last scroll area of splitter group 1 of group currentGroup of splitter group 1 of window "Final Cut Pro" exists then
                       set selectedGroup to currentGroup
                       exit repeat
                   else if group 1 of splitter group 1 of splitter group 1 of group currentGroup of splitter group 1 of window "Final Cut Pro" exists then
                       set selectedGroup to currentGroup
                       exit repeat
                   end if
               end repeat
               if selectedGroup is not 0 then
                   if group 1 of splitter group 1 of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                       --------------------------------------------------------------------------------    
                       -- List View:
                       --------------------------------------------------------------------------------    
                       if value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                           get position of value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro"
                           return the result
                       end if
                   else
                       --------------------------------------------------------------------------------    
                       -- Filmstrip View:
                       --------------------------------------------------------------------------------
                       if value indicator 1 of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                           get position of value indicator 1 of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro"
                           return the result
                       end if
                   end if
               end if
           end if
           --------------------------------------------------------------------------------                                                    
           -- If you get to here something went wrong:
           --------------------------------------------------------------------------------                                                    
           display dialog commonErrorMessageStart & "Unable to locate the Browser Playhead." & commonErrorMessageEnd buttons {"Close"} with icon caution
           return "failed"
       end tell
   end tell
end script
run script highlightFCPXBrowserPlayhead

Offline

 

#5 2016-09-18 02:30:27 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3192

Re: AppleScript UI Scripting Slow?

As I wrote, I don't own FCP.

I'm wondering if you may find an other object allowing you to test the used mode.
Maybe there is a difference in a menu item according to the status List View / Film Strip View

Maybe there is a difference too in a menu according to the status use 2nd monitor / use 1st monitor
If such differences exist, you may build your tests upon them.

About you first question, if you are grabbing the value from a single item, I don't know what would help to reduce the execution time except perhaps if, the relevant data may be reflected in a menu item.


Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 18 septembre 2016 09:30:22

Offline

 

#6 2016-09-18 04:49:53 am

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

Re: AppleScript UI Scripting Slow?

That's an awesome idea Yvan! It didn't even cross my mind to use menu items to work out the screen layout. Thank you! I'll give it a try shortly and see if it speeds things up a tad.

Unfortunately however in regards to question one, there's no menu items that can help. Ignoring Final Cut Pro, I can reproduce the same issue with Finder. If I have a Finder window with THOUSANDS of files displayed - UI Browser grinds to a halt. If there's only a few hundred files, then it works fine. It seems the more "things" you have, the slower the UI Scripting seems to work. If anyone has any ideas of how to workaround, let me know.

Thanks again Yvan!

Offline

 

#7 2016-09-18 05:13:08 am

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

Re: AppleScript UI Scripting Slow?

Sorry another question...

Assuming I can't solve the speed issue... is there anyway I can put a timeout on a "get" function?

I tried:

Applescript:


with timeout of 10 seconds
get value of attribute "AXSelectedChildren" of group groupCounter of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window whichWindow
end timeout

...but it didn't work. Any suggestions?

Offline

 

#8 2016-09-18 07:05:42 am

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

Re: AppleScript UI Scripting Slow?

For anyone who's interested, below is the latest versions of the scripts I'm messing around with.

Unfortunately, I still haven't been able to solve the "slowness" issue with big Final Cut Pro projects.

You can also download the complete Hammerspoon script here: https://latenitefilms.com/blog/final-cu … tch-frame/

If anyone has any suggestions on how to improve to below code, let me know!

Thanks in advance!

Single Match Frame:

Applescript:


script talkToFCPX
   --------------------------------------------------------------------------------                
   -- Error Messages:
   --------------------------------------------------------------------------------                                    
   set scriptVersion to "1.9"
   set macOSVersion to system version of (system info)
   set fcpVersion to version of application "Final Cut Pro"
   set commonErrorMessageStart to "I'm sorry, but the following error has occurred:" & return & return
   set commonErrorMessageEnd to return & return & "macOS Version: " & macOSVersion & return & "FCP Version: " & fcpVersion & return & "Script Version: " & scriptVersion & return & return & "Please take a screenshot of your entire screen and email it to the below address so that we can try and come up with a fix:" & return & return & "chris@latenitefilms.com" & return & return & "Thank you for testing!"
   --------------------------------------------------------------------------------                        
   -- Talk to FCPX:
   --------------------------------------------------------------------------------        
   activate application "Final Cut Pro"
   tell application "System Events"
       tell process "Final Cut Pro"
           set frontmost to true
           set whichWindow to ""
           --------------------------------------------------------------------------------                
           -- Switch to Thumbnail View:
           --------------------------------------------------------------------------------    
           try
               perform action "AXPress" of menu item "as Filmstrips " of menu 1 of menu item "Browser" of menu 1 of menu bar item "View" of menu bar 1
           on error
               display dialog commonErrorMessageStart & "Failed to Switch to Thumbnail View." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
           --------------------------------------------------------------------------------                
           -- Set Thumbnail Duration to All:
           --------------------------------------------------------------------------------
           set thumbnailDurationToAllStatus to false
           if window "Events" exists then
               --------------------------------------------------------------------------------    
               -- Using Second Monitor:
               --------------------------------------------------------------------------------                    
               set whichWindow to "Events"
               set selectedGroup to 1
               if value indicator 1 of slider 1 of group 2 of group 1 of splitter group 1 of window "Events" exists then
                   set value of value indicator 1 of slider 1 of group 2 of group 1 of splitter group 1 of window "Events" to 0
                   set thumbnailDurationToAllStatus to true
               end if
           else
               --------------------------------------------------------------------------------    
               -- Using Single Monitor:
               --------------------------------------------------------------------------------                
               set whichWindow to "Final Cut Pro"
               set selectedGroup to 0
               repeat with currentGroup from 1 to (count of groups of splitter group 1 of window "Final Cut Pro")
                   if group 1 of last scroll area of splitter group 1 of group currentGroup of splitter group 1 of window "Final Cut Pro" exists then
                       set selectedGroup to currentGroup
                       exit repeat
                   else if group 1 of splitter group 1 of splitter group 1 of group currentGroup of splitter group 1 of window "Final Cut Pro" exists then
                       set selectedGroup to currentGroup
                       exit repeat
                   end if
               end repeat
               if value indicator 1 of slider 1 of group 2 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                   set value of value indicator 1 of slider 1 of group 2 of group selectedGroup of splitter group 1 of window "Final Cut Pro" to 0
                   set thumbnailDurationToAllStatus to true
               end if
           end if
           if thumbnailDurationToAllStatus is false then
               display dialog commonErrorMessageStart & "Failed to Set Thumbnail Duration to All." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end if
           --------------------------------------------------------------------------------                
           -- Make sure timeline is active:
           --------------------------------------------------------------------------------                
           try
               perform action "AXPress" of menu item "Timeline" of menu 1 of menu item "Go To" of menu 1 of menu bar item "Window" of menu bar 1
           on error
               display dialog commonErrorMessageStart & "Failed to make timeline active." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
           --------------------------------------------------------------------------------                
           -- Trigger 'Reveal in Browser':
           --------------------------------------------------------------------------------
           try
               click menu item "Reveal in Browser" of menu 1 of menu bar item "File" of menu bar 1
           on error
               display dialog commonErrorMessageStart & "Failed to Reveal in Browser." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
           --------------------------------------------------------------------------------
           -- Find Selected Clip Number:
           --------------------------------------------------------------------------------
           set selectedClipNumber to 0
           set groupCounter to 1
           repeat
               try
                   get value of attribute "AXSelectedChildren" of group groupCounter of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window whichWindow
                   set selectedChildren to the result
                   if selectedChildren as string is not equal to "" then
                       set selectedClipNumber to groupCounter
                       exit repeat
                   end if
               on error
                   exit repeat
               end try
               set groupCounter to groupCounter + 1
           end repeat
           if selectedClipNumber is 0 then
               display dialog commonErrorMessageStart & "Failed to find which group number our match frame belongs to." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end if
           --------------------------------------------------------------------------------
           -- Get text from the selected clip:
           --------------------------------------------------------------------------------                                
           try
               get value of text field 1 of group selectedClipNumber of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window whichWindow
               set titleOfSelectedClip to the result
           on error
               display dialog commonErrorMessageStart & "Failed to get the title of the selected clip." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
           --------------------------------------------------------------------------------
           -- Set the search field to Title of the Selected Clip:
           --------------------------------------------------------------------------------                        
           try
               set value of text field 1 of group 1 of group selectedGroup of splitter group 1 of window whichWindow to titleOfSelectedClip
           on error
               display dialog commonErrorMessageStart & "Failed to enter the search text." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
           --------------------------------------------------------------------------------
           -- Run the search in FCPX:
           --------------------------------------------------------------------------------
           try
               perform action "AXConfirm" of text field 1 of group 1 of group selectedGroup of splitter group 1 of window whichWindow
           on error
               display dialog commonErrorMessageStart & "Failed to trigger the search command." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
           --------------------------------------------------------------------------------
           -- Completed!
           --------------------------------------------------------------------------------
           return "done"
       end tell
   end tell
end script
run script talkToFCPX

Highlight Browser Playhead:

Applescript:


script highlightFCPXBrowserPlayhead
   --------------------------------------------------------------------------------                
   -- Error Messages:
   --------------------------------------------------------------------------------    
   set scriptVersion to "1.9"
   set macOSVersion to system version of (system info)
   set fcpVersion to version of application "Final Cut Pro"
   set commonErrorMessageStart to "I'm sorry, but the following error has occurred:" & return & return
   set commonErrorMessageEnd to return & return & "macOS Version: " & macOSVersion & return & "FCP Version: " & fcpVersion & return & "Script Version: " & scriptVersion & return & return & "Please take a screenshot of your entire screen and email it to the below address so that we can try and come up with a fix:" & return & return & "chris@latenitefilms.com" & return & return & "Thank you for testing!"
   --------------------------------------------------------------------------------                                    
   -- Talk to FCPX:
   --------------------------------------------------------------------------------                                                                
   activate application "Final Cut Pro"
   tell application "System Events"
       tell process "Final Cut Pro"
           set frontmost to true
           if window "Events" exists then
               --------------------------------------------------------------------------------    
               -- Using Second Monitor:
               --------------------------------------------------------------------------------                    
               if group 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of window "Events" exists then
                   --------------------------------------------------------------------------------    
                   -- List View:
                   --------------------------------------------------------------------------------    
                   if value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of window "Events" exists then
                       get position of value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group 1 of splitter group 1 of window "Events"
                       return the result
                   end if
               else
                   --------------------------------------------------------------------------------    
                   -- Filmstrip View:
                   --------------------------------------------------------------------------------    
                   if value indicator 1 of group 1 of last scroll area of splitter group 1 of group 1 of splitter group 1 of window "Events" exists then
                       get position of value indicator 1 of group 1 of last scroll area of splitter group 1 of group 1 of splitter group 1 of window "Events"
                       return the result
                   end if
               end if
           else
               --------------------------------------------------------------------------------    
               -- Using Single Monitor:
               --------------------------------------------------------------------------------    
               set selectedGroup to 0
               repeat with currentGroup from 1 to (count of groups of splitter group 1 of window "Final Cut Pro")
                   if group 1 of last scroll area of splitter group 1 of group currentGroup of splitter group 1 of window "Final Cut Pro" exists then
                       set selectedGroup to currentGroup
                       exit repeat
                   else if group 1 of splitter group 1 of splitter group 1 of group currentGroup of splitter group 1 of window "Final Cut Pro" exists then
                       set selectedGroup to currentGroup
                       exit repeat
                   end if
               end repeat
               if selectedGroup is not 0 then
                   if group 1 of splitter group 1 of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                       --------------------------------------------------------------------------------    
                       -- List View:
                       --------------------------------------------------------------------------------    
                       if value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                           get position of value indicator 1 of group 1 of splitter group 1 of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro"
                           return the result
                       end if
                   else
                       --------------------------------------------------------------------------------    
                       -- Filmstrip View:
                       --------------------------------------------------------------------------------
                       if value indicator 1 of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro" exists then
                           get position of value indicator 1 of group 1 of last scroll area of splitter group 1 of group selectedGroup of splitter group 1 of window "Final Cut Pro"
                           return the result
                       end if
                   end if
               end if
           end if
           --------------------------------------------------------------------------------                                                    
           -- If you get to here something went wrong:
           --------------------------------------------------------------------------------                                                    
           display dialog commonErrorMessageStart & "Unable to locate the Browser Playhead." & commonErrorMessageEnd buttons {"Close"} with icon caution
           return "failed"
       end tell
   end tell
end script
run script highlightFCPXBrowserPlayhead

Match Frame Then Highlight Browser Playhead:

Applescript:


script matchFrameThenHighlightFCPXBrowserPlayhead
   --------------------------------------------------------------------------------                
   -- Error Messages:
   --------------------------------------------------------------------------------                                    
   set scriptVersion to "1.9"
   set macOSVersion to system version of (system info)
   set fcpVersion to version of application "Final Cut Pro"
   set commonErrorMessageStart to "I'm sorry, but the following error has occurred:" & return & return
   set commonErrorMessageEnd to return & return & "macOS Version: " & macOSVersion & return & "FCP Version: " & fcpVersion & return & "Script Version: " & scriptVersion & return & return & "Please take a screenshot of your entire screen and email it to the below address so that we can try and come up with a fix:" & return & return & "chris@latenitefilms.com" & return & return & "Thank you for testing!"
   --------------------------------------------------------------------------------                                    
   -- Talk to FCPX:
   --------------------------------------------------------------------------------                                                            
   activate application "Final Cut Pro"
   tell application "System Events"
       tell process "Final Cut Pro"
           set frontmost to true
           try
               --------------------------------------------------------------------------------
               -- Reveal in Browser:
               --------------------------------------------------------------------------------                        
               click menu item "Reveal in Browser" of menu 1 of menu bar item "File" of menu bar 1
           on error
               display dialog commonErrorMessageStart & "Unable to trigger Reveal in Browser." & commonErrorMessageEnd buttons {"Close"} with icon caution
               return "failed"
           end try
       end tell
   end tell
end script
run script matchFrameThenHighlightFCPXBrowserPlayhead

Offline

 

#9 2016-09-19 05:46:53 am

Chris Hocking
Member
From:: Australia
Registered: 2016-09-17
Posts: 9
Website

Re: AppleScript UI Scripting Slow?

For anyone who's interested, I've been chatting with Bill Cheeseman over at PFiddlesoft (pfiddlesoft.com), who make UI Browser, and he had some really interesting things to say (reproduced with his permission).

In regards to question one:

At it's heart, this is in fact an issue with the accessibility API.

In accessibility, there is a way to make it faster. I have done that in an upgrade to UI Browser that won't see the light of day for a while yet. However, in GUI Scripting, I don't at this time believe there will be any way to make it faster.

The solution in the accessibility API requires using a function that retrieves only a fraction of the UI elements in a large array at a time. If I only get the items that are currently visible in UI Browser, the number of items is relatively small. Then I can get the remaining items, a chunk at a time, in the background while the user is looking at the visible items. Once the user starts scrolling, the rest of them might have finished being fetched.

I don't see an easy way to do that in GUI Scripting, but you might want to play around with the idea to see what you can come up with.


If anyone has any ideas if you can actually pull this off using AppleScript, let me know!

In regards to question three:

Yes. Accessible applications -- which should be every application on the Mac -- have a hierarchy of UI elements stretching from the so-called "root" application UI element to the "leaf" UI element that you see on the screen, such as a button. The hierarchy should be identical no matter which direction you look. In GUI Scripting and in the browser view of UI Browser, you start at the root application element and travel along the hierarchy via "child" elements until you get to the leaf element, as in 'tell application "finder" to tell window 3 to tell button 2....'. In a screen reader like the screen reader view in UI Browser, to the contrary, you start at the leaf element on the screen and travel along the hierarchy via "parent" elements until you get back to the root application element.

For any UI element that has children, each child should have that element as its parent. When an application fails to satisfy this condition, it is a bug in the application known as a "mismatch." Many applications contain mismatch accessibility bugs like this.

It is important for scripters to know about mismatches. To write a GUI Scripting script, for example, you might use UI Browser's screen reader to find the hiearchy of a button, then attempt to script the button as 'button 3 of window 2 of application "Finder"' because the screen reader hierarchy says that window 3 is the parent of button 2 and that implies that button 2 is the child of window 3. But, since the screen reader looks in the opposite direction from GUI Scripting, it may be giving you misinformation that won't work when you write the script. For example, when looking in UI Browser's browser view, window 3 might think that it has no children that are buttons, but only a "group" UI element as its only child, and the "group" element might think that the buttons are its children. Your script won't work unless you look at the browser view in UI Browser, instead of the screen reader, where you might see this mismatch. This would tell you to write your script instead as 'button 3 of group 1 of window 2 of application "finder"', and it would work.

UI Browser shows the "mismatch" legend to let you know that you might have to explore these possibilities in order to make your script work. In the example in the previous paragraph, the "group" UI element was present in the hierarchy looking in one direction, but it was absent from the hierarchy looking in the other direction.

UI Browser is also a utility to assist developers of applications in testing their applications for compliance with accessibility requirements. This "mismatch" legend helps them to find bugs in their applications. I have used it over the years to tell Apple about a number of "mismatch" bugs in Apple's own applications, and they have fixed all of those bugs very, very quickly. I have noticed over the years that Adobe applications tend to have a lot of these mismatch bugs, too.

I don't use Final Cut Pro so I can't test it myself to prepare an appropriate bug report. If you know how to use Apple's developer bug reporter, you might want to do that yourself.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)