Benches macOS

12

bring app to front... without activate?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

is there a way to make a current running app the front app without using activate... or to tell activate to NOT open the app if its not open... or... well

heres my problem...

I'll have a few apps on my machine of different versions while testing stuff. messing with Wine apps and X11 and stuff, a lot of times things get thrown to the back, so i want to give a simple command to OSX that can bring it to the front as the active app.

using...

osascript -e "tell application "AppName" to activate"

usually makes it come up front... but the problem is occasionally if I have a different app on the computer that has the same name and preferences file, that it instead opens that app up and tries to run it, throwing the app i wanted in the background...

so basically I want a way to activate an app as the main app simply, but to find the current running app, not try to open any other apps that aren't open...

using osascript I didn't want to make it super complicated checking whats running and names and all kinds of logic just to decide what to activate. I'm thinking there has to be a simpler way to do this, with Applescript or some other way....

any help is appreciated

12 replies

Sign in or join Beachball Help to reply.

  1. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    The delay in the script that I posted was just in order that you could watch what was going on when each copy of the TextEdit application was brought to the front in turn. Anyway, it seems that you know much more about programming than me. I can't help.

  2. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ok... nevermind yall... I finally found the answer. I just don't know much about Applescript, and was having a "duh" moment... a very long moment... but oh well 🙂

    i basically used...

    /usr/bin/osascript -e "if application "$APPNAME" is running then tell application "$APPNAME" to activate end if"

    and it wont start any other apps... every time just the current running app is brought to the front.

  3. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ok well... jumped the gun there.. that doesn't actually work, was just a buncha lucky launches... it actually syntax errors on console... so... still working on it.

  4. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    just had to remove the end if off the end... and it works... well code wise, but it doesnt actualyl fix the problem.

    Message was edited by: doh1231

  5. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    anyone have any idea how to fix the problem? when you have multiple copies of the same app, with the same name and ID... to only bring the running one to the front and not try to launch the one thats not running?

  6. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I do have the correct PID of the Application already... is there some way to use Applescript to tell an application to do something by the PID?

    like...

    Tell Application (unix ID $PIDNUM) to activate

    ?????? something thats very specific that cannot be multiple apps?

  7. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm not sure at all to really understand what you are trying to do. However, maybe you could try this: first create several copies of TextEdit.app, respectively named “TextEdit copy”, "TextEdit copy 2”, “TextEdit copy 3”, etc.; then open a few of these applications; finally launch the following script and see if it does what you are expecting.

    tell application "System Events"

    set theIDList to id of processes whose name is "TextEdit"

    repeat 5 times

    repeat with theID in theIDList

    set (frontmost of processes whose id is theID) to true

    delay 1

    end repeat

    end repeat

    end tell

    Just an idea. Maybe silly. Hope it can help.

  8. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    the problem is timing... and I don't want to use any type of delay/sleep commands, I want it to go as soon as it can... going for speed.

    The problem is... my app has to run multiple other apps and many processes. What I have to do is have an app launch in the background (an X11 build actually, not default X11.app), and then do some other things... then the final results are all running in the X11 app, so I need to bring it back to the front most application. The problem is, the place my script gets to, to wait for the X11 app, the X11 app is not always finished loading completely. If i tell it to activate to bring it back up front, and its fully done loading, then it works fine. Times when its not fully done loading (usually just a second or less away), OSX goes and finds another app with the same name and ID and launches it instead.... sometimes...

    This will lead to rare and annoying bugs with my app... though most of the time it would work fine. On older, slower machines it messes up most of the time.

    I have started using tell application id "$APPIDNUMBER"

    I tried this in the bash script

    /usr/bin/osascript <<END

    repeat

    if application id "$IDNAME" is running

    tell application id "$IDNAME" to activate

    exit repeat

    end if

    end repeat

    END

    so it basically set in a loop until that program was running. The bash script its running out of works fine on 10.5 and 10.6 doing that as far as I could tell, but only when running it directly, which doesn't happen, when it was opened from another process, it gets stuck in the loop forever, no matter what the id does.... it also didn't work at all on 10.4, it apparently doesn't understand "is running"

    so trying to find a way to do this only when the app is actually running, to tell it to activate... without using arbitrary wait states... cuz that leads to problems, especially for portability and longevity....

  9. 0

    Pierre L.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    The delay in the script that I posted was just in order that you could watch what was going on when each copy of the TextEdit application was brought to the front in turn. Anyway, it seems that you know much more about programming than me. I can't help.

  10. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks for trying... I don't have much knowledge in Applescript, and using bash scripts to run things, and calling a few things in Applescript to assist seems to work good...

    I think i found a solution though, and it seems really simple... I just had to tell it the exact application to activate, and not let it try to find it itself.... duh.... gotta test on 10.4 and 10.5, but its working fine on 10.6 with just this simple line....

    /usr/bin/osascript -e "tell application (POSIX path of "$PATHTOAPP/$APPNAME") to activate"

    where... $PATHTOAPP is something like this.... "/Applications/MainApp.app/Contents/Resources"

    and $APPNAME is something like "SecondaryApp.app"

    Message was edited by: doh1231

  11. 0

    twtwtw

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm not really sure what you're trying to do (or why you're running this via osascript when you could just reun it straight from the script menu). but I think the solution you want (in applescript) is something like the following:

    tell application "System Events"
    set theApps to every process whose name contains "AppName"
    try
    set theApp to item 1 of theApps
    set frontmost of theApp to true
    end try
    end tell

    that should bring whatever running app of that name to the front, without triggering any app on the disk to launch.

  12. 0

    doh1231

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    thanks.. not really what I'm looking for... the pervious post I made about what I did, works great... just had to give the specific app with a path to it, instead of leaving it up to OSX to find the app.

    I'm using osascript to do a couple simple applescript commands like this... most of the stuff I write is in bash, and its super easy to run an applescript command or two with osascript with quick ways to choose files, or display dialogs, etc...