Tuesday, September 27, 2022

#1 2022-02-18 01:54:16 pm

jtucker
Member
Registered: 2022-02-18
Posts: 2

Giving Focus to Automator Application

I'm a perennial beginner with Automator and AppleScript. I created an application that, when launched, shows a list (via Applescript within Automator) from which the user chooses one item.

That list does not have the focus by default, so I added "tell application...activate" at the front to give it the focus. However, I hard-coded the application name because I don't know how to provide the application name programmatically. Can somebody please teach me how?

Here is a stripped-down version of the relevant code:

Applescript:

# This is an Automator-built application
# Its name is "Foobar"
# This script works, but it would be more elegant (and future-proof) to refer to the application
# programmatically, rather than by hard-coding the name into the script, but how??

tell application "Foobar"
   activate
end tell

# Do some other stuff

Offline

 

#2 2022-02-18 02:56:26 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5563

Re: Giving Focus to Automator Application

Hi. Welcome to MacScripter.

If the workflow's saved as an application, you should be able to use just:

Applescript:

activate

… or:

Applescript:

tell me to activate

… or:

Applescript:

tell current application to activate


NG

Offline

 

#3 2022-02-18 03:46:21 pm

jtucker
Member
Registered: 2022-02-18
Posts: 2

Re: Giving Focus to Automator Application

Thanks, Neil!
The third suggestion worked. The first two did not work for me.
What primer or other resource would you recommend I study, so I can know these things for myself?

Offline

 

#4 2022-02-19 06:23:51 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5563

Re: Giving Focus to Automator Application

jtucker wrote:

The third suggestion worked. The first two did not work for me.


Yes. That makes sense. Sorry.  'me' is the script itself, while 'current application' is the application running it. The two can be the same when a script's saved as an application, but apparently not when it's part of an Automator workflow saved as such.

What primer or other resource would you recommend I study, so I can know these things for myself?


Apple's own AppleScript Language Guide is probably still the best resource for telling you what's actually in the language. It's mention of the 'current application' constant is here.


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)