I have a macro that uses an Execute an AppleScript
action to execute a script file. In a (very small) nutshell, the script:
- reads a text file,
- puts up a pick list using the file's contents with
choose from list
, and - if the script was canceled, executes
return ""
, else - returns the chosen items to KM in a string, one item per paragraph, using
return theItems
.
KM receives the chosen items into a variable, massages the text depending on the app in the front when the macro was invoked, and puts the massaged text into that app.
Works fine, except that the pick list doesn't get input focus. It's a flow-breaker to be merrily keyboarding away and after hitting the hotkey to invoke the script, to have to find the mouse and fiddle around to click on the pick list before I can make my selections.
I've not been able to find any AppleScript workaround to get the pick list to get input focus.
So I saved the script as an app and used an Execute Shell Script
action with the command open '/Users/Me/Path to/My App.app/'
. Works fine, the pick list gets input focus and there's no need to mouse around. But, whereas the script returned the selected items just fine, the app never returns anything. Or maybe KM never gets the return value? Impossible to tell.
But, a display dialog theItems
in the script/app just before return theItems
shows that theItems
has the desired value.
The attached script, screenshot of the macro, and macro, illustrate the problem.
- Copy the script into Script Editor, save it as Script, and save it as Application.
- Download the macro & import into KM.
- In the macro, select the paths to the script & app to reflect where you saved them.
- Run the macro and you'll see that:
- both the script and the app report what they're returning,
- KM receives the return values from the script, and
- KM never receives anything from the app.
I'd really like to use this simple method of just using return theItems
, and at the very least, understand what the issue is, before I resort to workarounds. Maybe I'm doing something stupid or not doing something obvious, that I'm just not seeing? Maybe Mac apps in general, or app-wrapped AppleScripts specifically, never return anything? Or something else?
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set answer to choose from list {"item 1", "item 2", "item 3"} with multiple selections allowed
if answer = false then
set returnValue to "canceled"
else
set returnValue to ""
repeat with anItem in answer
set returnValue to returnValue & anItem & return
end repeat
end if
display dialog "the script/app is returning:" & return & return & "\"" & returnValue & "\"" with title "Message from the script/app" buttons {"OK"} default button "OK"
return returnValue