Need help with AppleScript repeat command

  1. 8 months ago

    Roger M

    16 Feb 2017 Santa Rosa, CA.

    Hi All,

    I am still working on my AppleScript capabilities in Xojo. I have the following code with converts Keynote files to PowerPoint files. It works well, except for handling the errors supplied by Keynote. I seem to get three error windows at different times for the same document. I can handle one of the error windows with my script, but I just can't quite figure out how to do it using the repeat command in AppleScript. Everything I have tried seems to put the app into an infinite loop.

    I am on MacOS Sierra and am using Keynote 7.0.5. Any help would be appreciated.

    lines.Append " set current_file to POSIX file """ + Listbox1.Cell(i, 0) + """"
    lines.Append " set file_name to name of (info for current_file)"
    lines.Append " set dest_folder to POSIX path of """ + primaryDestPathLabel.text + "/"""
    lines.Append " set pptx_file to text 1 thru -4 of file_name & ""pptx"""
    lines.Append " set out_file to dest_folder & pptx_file"
    lines.Append " tell application ""Keynote"""
    lines.Append " set thisdoc to open current_file"
    lines.Append " close access(open for access out_file)"
    lines.Append " try"
    lines.Append " export thisdoc to POSIX file out_file as Microsoft PowerPoint with skipped slides"
    lines.Append " close thisdoc saving no"
    lines.Append " on error"
    lines.Append " if (exists application ""Keynote"") then"
    lines.Append " activate"
    lines.Append " tell application ""System Events"""
    lines.Append " tell process ""Keynote"""
    lines.Append " set frontmost to true"
    lines.Append " delay 1"
    lines.Append " keystroke return"
    lines.Append " end tell"
    lines.Append " end tell"
    lines.Append " end if"
    lines.Append " end try"
    lines.Append " end tell"

    Roger

  2. Beatrix W

    16 Feb 2017 Pre-Release Testers Europe (Germany)

    Please post the AppleScript only. The way you post the script I almost can't see anything.

    What are you trying to do in the error handler?

    if (exists application "Keynote") then
    activate
    tell application "System Events"
    tell process "Keynote""
    set frontmost to true
    delay 1
    keystroke return
    end tell
    end tell
    end if

    What do you mean with the repeat command? There is no repeat in your code. If there is an error the best thing you can do is to return the error to your Xojo app and handle it there.

  3. Roger M

    16 Feb 2017 Santa Rosa, CA.

    Hi Beatrix,

    I am trying to handle a dialog window that pops up when there is a problem with a file and Keynote can't handle it. It reads "Bad-File.key" can't be opened for some reason and provides a button of OK. However, when I click OK, the same message box will appear at least 2 more times. I would like to be able to click the OK button after each box appears and have the script proceed exporting the rest of the files.

    I left any attempts to use the repeat command out because it was a real mess and didn't work.

    How do I return an error to my Xojo app to see what is going on?

    Roger

  4. Roger M

    16 Feb 2017 Santa Rosa, CA.

    Beatrix,

    I got my app to display the following error message: Keynote got an error: Can't make missing value into type document with a Cancel and OK button.

    Hope that helps.

    Roger

  5. Beatrix W

    16 Feb 2017 Pre-Release Testers Europe (Germany)

    Showing an error 3 times is very unusual. Where does your error come from? Because this sounds like a problem with your script. Are you sure that Listbox1.Cell(i, 0) and primaryDestPathLabel.text always have the correct values?

  6. Michel B

    17 Feb 2017 Pre-Release Testers, Xojo Pro
    Edited 8 months ago by Michel B

    Roger, instead of appending line after line which create a big pile, won't you simply enter the script into a constant, or even better, enter the script into Mac's Script Editor, and drag it into the project, then call it simply by its name.

    As it stands it is not only ugly, but also difficult to debug.

    BTW, when you post code, please select it and click the code icon -image- above the editor, so it makes it legible :

    lines.Append " set current_file to POSIX file """ + Listbox1.Cell(i, 0) + """"
    lines.Append " set file_name to name of (info for current_file)"
    lines.Append " set dest_folder to POSIX path of """ + primaryDestPathLabel.text + "/"""
    ...
  7. Roger M

    17 Feb 2017 Santa Rosa, CA.

    Hi Beatrix,

    I am sure my Listbox and label have the correct values because I am able to also convert Numbers to Excel and Pages to Word and handle the errors correctly using identical code. So I can't figure out why i am getting 3 error messages with Keynote, because neither Numbers or Pages does that.

    Kinda at a loss here.

    Roger

  8. Roger M

    17 Feb 2017 Santa Rosa, CA.

    Hi Michael,

    Thanks for the info and suggestions. I will work toward that in the future. I am still pretty new to Xojo and only get intermittent chances to do app development for my company.

    I will work harder at cleaning up my code and using more of the functionality of Xojo in the future.

    Roger

  9. James S

    18 Feb 2017 Pre-Release Testers, Xojo Pro

    I would break it into several separate scripts in Xojo. You can get the reply from the attempt to do whatever causes the error and decide if you need to loop the script to dismiss it several times in xojo code just calling smaller parts of the script. I would return the error code from the script into xojo, and then have a separate little script that dismissed the error box until you no longer get the error.

or Sign Up to reply!