How to make an Applescript Window become the main/highlighted window?

I’ve set up some applescript code to run on the launch and quit of an application.

The applescript code simply displays a choose in list window to choose a setting.
When the application is launched, the window pops up, but is not highlighted or focussed, meaning I have to click it once to bring it into focus and choose my setting.

I’d like to be able to just hit enter on the default setting when the window pops up but since it is not in focus I cannot. Is there a piece of applescript code to include in my code or is there a maestro setting?

Thank you

Hey There,

You need your app to launch.

Then your AppleScript needs a tell-block with that application-name.

Launch BBEdit (or your-app) with KM.

Then your AppleScript:

set l to {"one", "two", "three", "four", "five", "six"}
tell application "BBEdit"
  set myChoice to choose from list l
end tell

If you don’t tell the choose-from where its context is then the context is the app that runs the AppleScript - in this case the Keyboard Maestro Engine - which is a faceless app.

If you needed the Keyboard Maestro Engine to be the agent for the choose-from you would tell it to activate.

In this case though that would leave the app you want to work with in the background.


Best Regards,
Chris

1 Like

YES thank you SO much!
I did what you said for the script that runs on the application launch, but for the quit, I told Finder to do it.
This solved it!
Now I’ve come across another issue:

tell application "Finder"
set asrc to (choose from list {"Internal Speakers", "Soundflower (2ch)"} with title "Sound Control" default items {"Internal Speakers"}) as text
if result is "false" then return

tell application "System Preferences"
	reveal anchor "output" of pane id "com.apple.preference.sound"
	activate
	
	tell application "System Events"
		tell process "System Preferences"
			select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is asrc)
		end tell
	end tell
	quit
end tell
end tell

The purpose of the code is to switch between sound outputs in System Preferences. On application quit I’d like to switch back to Internal Speakers EXCEPT WHEN the option “Headphones” is available in which case “Internal Speakers” is not and “Headphones” should be chosen. I’m sure theres a simple try catch or if statement code but I am unfamiliar with applescript.

What would solve this problem?

Hey There,

In general it’s better to not embed tell-blocks inside tell-blocks.

I’ve broken that rule of the thumb once below for a specific reason.

tell application "System Preferences"
  if not running then launch # Works around an occasional problem with activate.
  activate
  reveal anchor "output" of pane id "com.apple.preference.sound"
end tell

tell application "System Events" to tell process "System Preferences"
  tell window "Sound"
    tell table 1 of scroll area 1 of tab group 1
      set outputList to value of its text field 1 of rows
      set outputList to text of outputList
      tell application "System Preferences" to set outputChoice to choose from list outputList
      if outputChoice = false then
        beep
        return
      else
        set outputChoice to first item of outputChoice
      end if
      select (first row where value of its text field 1 contains outputChoice)
    end tell
  end tell
end tell

This is a generally better method for doing your task, because it pulls from the actual output methods rather than using a predetermined list.

Of course it may not function exactly as you want it to. If not then let me know what’s off about it, and I’ll fix it.

-ccs

Something line this will probably work.

tell application "System Events"
  tell process "System Preferences"
    tell window "Sound"
      tell table 1 of scroll area 1 of tab group 1
        set rowToSelect to rows where value of its text field 1 is "Headphones"
        if rowToSelect = {} then
          set rowToSelect to rows where value of its text field 1 is "Internal Speakers"
          if rowToSelect ≠ {} then
            select item 1 of rowToSelect
          end if
        end if
      end tell
    end tell
  end tell
end tell

NOTE: This is a snippet and not a fully fleshed-out script.

You should now have enough clues to finish this yourself, but let me know if you get stuck.

-ccs

Yes this is quite close.
Firstly, having the System Preferences window close on close of the applescript window is a must and secondly I’d only like to take the first two listings for the output in the Sound window. I’d also like the first listing to always be highlighted.

See Switch Audio Source, which uses the command-line program SwitchAudioSource.