How Do I Make AppleScript Window Have Focus?

The below macro is triggered by a typed string.
When I type the string into, say TextWrangler, the following window is shown, but does NOT have focus:

I need the window to have focus so the user can respond using only the keyboard.

MACRO: AppleScript Choose List Test with Typed String Trigger [TEST]

AppleScript Choose List Test with Typed String Trigger [TEST].kmmacros (3.1 KB)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set myList to {"one", "two", "three"}
set oAns to choose from list myList ¬
  with title ¬
  "Choose One" with prompt ¬
  "This window should have focus" default items ¬
  "one" OK button name ¬
  "Select" cancel button name ¬
  "Cancel" multiple selections allowed false ¬
  without empty selection allowed

set resultStr to item 1 of oAns

return resultStr

When I use an AppleScript prompt like you seem to be wanting, it’s done using System Events.

tell application "System Events"
set frontProcess to the name of every process whose frontmost is true
end tell

tell application "System Events"
	activate
	set theList to {"InDesign", "Photoshop", "Acrobat Pro", "----------", "Font Guide", "PMS Chart 1", "PMS Chart 2", "----------", "Colorate", "ColorSchemer", "----------", "Shrink It"}
	choose from list theList with prompt "Design selections:" default items {"InDesign"}
	set listchoice to result as text
end tell

if listchoice is "InDesign" then
	do shell script "open -a 'Adobe InDesign CS5.5.app'"
	    
else if listchoice is "Photoshop" then
	do shell script "open -a 'Adobe Photoshop CS5.1.app'"
	    
else if listchoice is "Acrobat Pro" then
	do shell script "open -a 'Adobe Acrobat Pro.app'"
	
else if listchoice is "PMS Chart 1" then
	tell application "Finder" to open POSIX file "/path/to/file"
	
else if listchoice is "PMS Chart 2" then
	tell application "Finder" to open POSIX file "/path/to/file"
    
else if listchoice is "Colorate" then
	tell application "Colorate"
		activate
	end tell
	
else if listchoice is "ColorSchemer" then
	tell application "ColorSchemer Studio 2"
		activate
	end tell
	
else if listchoice is "Shrink It" then
	tell application "ShrinkIt"
		activate
	end tell
	
else if listchoice is "Font Guide" then
	tell application "Finder" to open POSIX file "/path/to/file"
	
    else
   	activate frontProcess
	
end if
end
1 Like

Hey JM,

I’m shocked you actually got a dialog…

You used to have to tell-app-block <Interactive AppleScript Code> end-block in order to get a dialog to show up when running AppleScript from Keyboard Maestro.

Using System Events isn’t really a good idea, because it leaves System Events as the active app. Since System Events is a faceless background app the user is left in limbo. Although it appears they are actively in the “frontmost” app in reality they’re stuck in System Events.

A better method is to use the frontmost app itself for the interactive call, since this dumps the user right back where they started.

------------------------------------------------------------
try
  
  set frontApp to path to frontmost application as text
  
  set myList to {"one", "two", "three"}
  
  tell application frontApp
    set oAns to choose from list myList ¬
      with title ¬
      "Choose One" with prompt ¬
      "This window should have focus" default items ¬
      "one" OK button name ¬
      "Select" cancel button name ¬
      "Cancel" multiple selections allowed false ¬
      without empty selection allowed
  end tell
  
  set resultStr to item 1 of oAns
  
  return resultStr
  
on error e number n
  if n = -128 then
    # NULL
  else
    error e number n # Build your own error-handler.
  end if
end try
------------------------------------------------------------

-Chris

2 Likes

Chris!! You are, once again, the man!! :thumbsup:

Your script works perfectly!

Thanks.