Apple Intelligence now features Image Playground, Genmoji, Writing Tools enhancements, seamless support for ChatGPT, and visual intelligence.

Apple Intelligence has also begun language expansion with localized English support for Australia, Canada, Ireland, New Zealand, South Africa, and the U.K. Learn more >

You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to launch an application and wait till it exits

I have written the applescript to launch an application and wait till it exits and but it is not working. Here is the code for that

tell application "/Applications/Pages.app" to activate

tell application "/Applications/Pages.app"

repeat until it is not running

end repeat

end tell


But when I tried to close the app, the script is still running. I think the reason is , even though I closed the window, that process is still running. Can you please help me to solve this problem?

Posted on Mar 6, 2015 9:24 AM

Reply
8 replies

Mar 6, 2015 12:31 PM in response to bharath171

Another issue is that when you launch Pages, by default, it will open the Template Chooser, and just sit there waiting for you to pick one. The application will remain open and running indefinitely in this state, until it quits by user interaction, or your script then quits it. I have included some Python code that incorporates twtwtw's AppleScript, and you can see how the Python code runs the AppleScript as a subprocess. Finally, a script completion notification will display. I have used the optional Anaconda Python syntax checker in Sublime Text 3, and there are no Python syntax issues.


I do not write Python code with great whacking gaps in the code, the following is the result of using the Python syntax facility from this editor.


#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys
import subprocess


ascmd = u"""


    on run
        tell application "Pages" to activate
            -- add a longish delay to allow the application to start
            delay 10
        tell application "Pages" to quit
    end run


    on idle
        if application "Pages" is not running then
            -- the application has quit, so go to the 'on quit' handler
            quit
        end if
    end idle


    on quit
        set notificationTitle to "AppleScript Quit Handler"
        set notificationSubTitle to "Pages Status"
        set notificationMsg to "Not Running"
        display notification notificationMsg with title notificationTitle ¬
                        subtitle notificationSubTitle
        continue quit
    end quit


    """


ascmd_done = u"""
        set notificationTitle to "Python/Osascript Test"
        set notificationSubTitle to "Test Status"
        set notificationMsg to "Finished"
        display notification notificationMsg with title notificationTitle ¬
                        subtitle notificationSubTitle
    """




def main():


    try:
        result = subprocess.check_output(['osascript', '-e', ascmd])
        # print("\nResult: %s\n" % result)
        if 'Cancel' not in result.decode('utf-8'):
            subprocess.check_output(['osascript', '-e', ascmd_done])
    except subprocess.CalledProcessError as e:
        print('Python error: [%d]\n%s\n' % e.returncode, e.output)


if __name__ == '__main__':
    sys.exit(main())


Here is what the formatted Python script looks like:


User uploaded file

Mar 6, 2015 9:33 AM in response to bharath171

Trying to make this kind of thing work from the script editor is going to give you headaches. A better approach is to create a script application. Copy the following code into the Script Editor, and then save it as an application with the "Stay open after run handler" checkbox checked. Then run the resulting script application directly.


on run

tell application "Pages" to activate


-- add a longish delay to allow the application to start


delay 5

end run


on idle

if application "Pages" is not running then


-- the application has quit, so go to the 'on quit' handler


quit

end if

end idle


on quit


-- do whatever you want to do after the application has quit


-- allow the app to quit itself

continue quit

end quit

Mar 6, 2015 11:22 AM in response to bharath171

The problem you're going to face is that Applescript - when run as a script - is not particularly respectful of other processes. It is designed to work straight through from beginning to end, and while it communicates well with applications it was never made to sit around and wait for them. Rethinking the script as a script application makes it much more flexible in terms of waiting, since it can use the idle loop to poll an application of particular states.


Without knowing more about what you're trying to do, I can't be more specific. All I can really do is point out that what you are trying to do works against Applescript's grain. It's not necessarily impossible (and with a little more information from you maybe we can work it out), but it is likely to produce headaches.

Applescript to launch an application and wait till it exits

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.