Wednesday, May 22, 2019

#1 2019-03-14 06:39:34 am

Registered: 2019-03-03
Posts: 125

Performing click with option down using AppleScript

"It's currently not possible to click with a key held down using AppleScript. Key down actions only apply to other key press actions, since the AppleScript click action doesn't actually perform a ‘click’, but rather directly actions the element."

Is the previous statement true, which can often be found on the net?
Is this problem solvable without third-party applications?

My answer to the first question is NO, not all true.
The answer to the second question - Mac OS X is everything we need. You just need to be able to look good.

In this topic I will show you how to do this to open the extended view of your Wi-Fi info, but this procedure is suitable for programming any click with the pressed "option" or "command" or other key.

Step 1: Open application Automator, сhoose "Create new"-->"Application
Step 2: Click button "Record"
Step 3: Holding key "option" on keyboard, click WiFi icon on extra menu of menu bar
Step 4: Stop "Record"

What have we done so far? We have created a new action that can be saved as a new service or as a new application. For example, select "save as application". So:

Step 5: Select "Save" -> "As application" in the menu, give it the name WiFi_Extended_View, and save it. Save better in the "Applications" directory, but уou can choose other too.

Step 6: Now run the following sentence in the Script Editor, select Wifi_Extended_View and copy the reference from the Results window. We need it later:

      set myAction to choose file

For example, my reference is alias \"Χωρίς τίτλο\"

Step 7: And here is a script that will open up for us an extended view of WiFi info. Delete previous code line and paste from here this script. Remember to replace reference to action-file with your alias!!!:

set timeoutSeconds to 2.0
set uiScript to "tell application \"Finder\"
                set myApplication to alias \"Χωρίς τίτλο\"
            open myApplication    
              end tell"
my doWithTimeout(uiScript, timeoutSeconds)

on doWithTimeout(uiScript, timeoutSeconds)
    set endDate to (current date) + timeoutSeconds
            run script uiScript
            exit repeat
        on error errorMessage
            if ((current date) > endDate) then
                error "Can not " & uiScript
            end if
        end try
    end repeat
end doWithTimeout

Step 8: Оп the System Settings, be sure to allow the Automator application to control your computer. That is all. Now you can use this script as you want.

Note: you can use the script in a simpler form, without professional control over the execution of a command for errors and without using a script object:

tell application \"Finder\"
                set myApplication to alias \"Χωρίς τίτλο\"
            open myApplication    
              end tell

The more complex variant above is simply 1) creating an script object 2) wrapping it with shell subroutine (doWithTimeout) to verify the execution error and the expiration execution time of the script object (I set 2 seconds maximum). Actually, this is the most correct way to write any script command. If a command in a large script fails, this shell subroutine (doWithTimeout) throws an error indicating the exact place where something went wrong. It is just right.

Of course, only for opening Wi-Fi Info you could use the action-application itself (by clicking it). But in a large script where other actions are performed, you will need paste a similar Apple-script snippet. In addition, you can save large script as a application-bundle and put the action to its folder Resourses.

Using Automator, you can create incredible custom services. These custom services (workflows) you can found then in ~/Library/Services. What is impossible for an application or Applescript, is possible for core application Automator. I love Automator

Last edited by KniazidisR (2019-03-14 01:13:10 pm)

macOS Mojave -- version 10.14.4
Safari -- version 12.1



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)