4

Why doesn't this work

tell application "System Events"
    set AppName to name of every process whose background only is false
    tell application (some item of AppName)
        activate
    end tell
end tell

while this

works

global AppName
tell application "System Events"
    set AppName to name of every process whose background only is false
end tell

tell application (some item of AppName)
    activate
end tell

Obviously, it has something to do with scope but I don't understand why or what.

  • 2
    It's not about scope, no, it's about where commands are being directed to and which application or script is handling their execution. You nested one tell application block inside the other in your first script. There are many good reasons to do this in a given situation, but this isn't one of them as System Events can't really issue commands to other applications, and activate is ambiguously targetting potentially both System Events and some other application, so likely ends up activating System Events preferentially. Separating the two tell blocks in your second is what works. – CJK Jun 15 '18 at 18:53
  • 2
    The global AppName declaration is redundant. – CJK Jun 15 '18 at 18:54
5

I think the problem is.. Telling an application to activate, is handled by "Standard Additions", but you are asking "System Events" to handle that action.

tell application "System Events" to set AppName to name of ¬
    every process whose background only is false
tell application (some item of AppName) to activate
  • You don't need my. tell application (some item of AppName) to activate is sufficient. – CJK Jun 15 '18 at 18:47
  • Yes you are correct. However, I do believe if you set the variable within an application tell block, referring to that variable in a tell block of a different application, using "my" or "of it" becomes necessary. I think LOL – wch1zpink Jun 15 '18 at 18:53
  • That's in relation to properties and handlers, not variables, does it not ? – CJK Jun 15 '18 at 19:02
3

Let me tell you a story:

One parent got up and called to three children:

  • Ace - wake up and go bring me an apple
  • Deuce - wake up and go bring me an apple
  • Trey - wake up and go bring me an apple

That parent was happy to have three apples and everyone out of bed.

The other parent said:

  • Trey - wake up and go bring me an apple (or they think they asked Trey - they're not really sure they were specific about what or who was asked)

Does this match what "doesn't work" for you?

If so - awesome. If not, you'll want to step through each script in Script Editor and see where your programs go "off script". I would also encourage a look at https://latenightsw.com Script Debugger - it runs some basic functionality for free, but If you value your time more than you do stepping through scripts in Apple's more basic editor, it may be worth the $$ to you for the full version.

Also, a book on iteration or loops might be a good place to go if everything wasn't clear. Don't feel bad, this is a great place to stumble and learn - once you get loops, you'll be able to do amazing things in any scripting language - not just AppleScript.

  • 1
    I really hope you earned your 145k reputation with answers exactly like this one. This is brilliant. I wish all StackExchange answers were abstract parables. – CJK Jun 15 '18 at 20:27
  • Ouch - I think ;) – bmike Jun 15 '18 at 20:46
  • 2
    No, I genuinely love this answer. I don't necessarily want to +1 it, but I definitely want to ❤ it. – CJK Jun 15 '18 at 20:47
  • 2
    Well @CJK then I really appreciate the kind comment. That's really what drives me and not any specific +1 or -1 and I get that sometimes this abstract an answer really doesn't land well and I get that, too. – bmike Jun 15 '18 at 20:49
  • Love it! :) Storytelling is definitely one of the best ways to teach and/or convey a message! – Monomeeth Jun 16 '18 at 5:56
1

I was trying to click the "OK" button on Kontakt in the background while I worked. This solved my issue:

tell application "System Events"
    if exists of application process "Kontakt 5" then
        tell application "Kontakt 5.6.0" to activate
        delay 0.5
        repeat
            try
                click UI element "OK" of window 1 of application process "Kontakt 5"
            end try
        end repeat
    end if
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .