Program Runs Incorrectly When Triggered via AppleScript, fine when opened normally.

Discussion in 'Mac Apps and Mac App Store' started by JonHimself, May 27, 2013.

  1. JonHimself macrumors 68000

    JonHimself

    Joined:
    Nov 3, 2004
    Location:
    Toronto, Ontario
    #1
    I am trying to remotely enable (and stop) AirPlay mirroring from a Mac mini on my network. I was able to use Automator to create a program (all it does is run an AppleScript) that does the following:

    Code:
    tell application "System Events"
    tell process "SystemUIServer"
    click (menu bar item 1 of menu bar 1 whose description contains "Displays, AirPlay Off")
    click menu item "Downstairs TV" of menu 1 of result
    end tell
    end tell
    There is another code for "Bedroom TV" and then one that turns off the AirPlay mirroring. I saved three programs and when I run them by double clicking on them, they work.

    I came across a post (don't have the link) that can trigger things based on notes in the Notes.app (with the idea being that by adding a note on my phone or ipad, it will sync back to the computer that is also running the Notes.app and trigger something). The code for that is as follows:

    Code:
    on run
    -- just here to get things started...
    end run
    
    
    on idle
    tell application "Notes"
    if exists note "AirPlay upstairs" then
    delete note "AirPlay upstairs"
    tell application "~/prog.AirPlayBedroom.app"
    activate
    end tell
    else if exists note "AirPlay downstairs" then
    delete note "AirPlay downstairs"
    tell application "~/prog.AirPlayDownstairs.app"
    activate
    end tell
    else if exists note "AirPlay off" then
    delete note "AirPlay off"
    tell application "~/prog.AirPlayOff.app"
    activate
    end tell
    else if exists note "Shut down" then
    delete note "Shut down"
    tell application "Finder" to quit
    else if exists note "Go to sleep" then
    delete note "Go to sleep"
    tell application "Finder" to sleep
    else if exists note "Restart my mac" then
    delete note "Restart my mac"
    tell application "Finder" to restart
    end if
    end tell
    return 1
    end idle
    Ignore the "~/", I've just removed the full path to shorten it up.

    The Shut Down, Go to sleep, and Restart my my mac commands all work. The three airplay commands work in that they successfully trigger the app to run. I can see the Airplay Menu open and I can see the Downstairs TV item get highlighted. The problem is that nothing happens (as if it's not making that second click) for all three of my programs.

    Would anyone know why that would happen?
     
  2. JonHimself thread starter macrumors 68000

    JonHimself

    Joined:
    Nov 3, 2004
    Location:
    Toronto, Ontario
    #2
    I found a solution, not sure why it works... but it does. Adding 'delay 5' (capitalized in the code below) to the end of the code made this work.

    Code:
    tell application "System Events"
    tell process "SystemUIServer"
    click (menu bar item 1 of menu bar 1 whose description contains "Displays, AirPlay Off")
    click menu item "Downstairs TV" of menu 1 of result
    DELAY 5
    end tell
    end tell
    
     

Share This Page