Tell launch issue

Discussion in 'Mac Programming' started by iConcept, Mar 6, 2015.

  1. iConcept macrumors newbie


    Sep 1, 2009

    I'm trying to create an Applescript which runs as an application. When run, it prompts the user for a number of hours, then waits this number of hours to launch a specific application, for instance, TextEdit.

    For some reason (I think since Yosemite), each first time I open or compile in Applescript, or the application once saved, it starts the target application immediately, before it's even prompted the user. The process repeats if Applescript is closed down and opened again. It never used to do that prior to Yosemite, then seemed to start all of a sudden.

    Code is as follows:

    set x to text returned of (display dialog "Open TextEdit in {x} hours?" with title "Input value..." default answer "0" buttons {"Cancel", "Start"} default button 2)
    set y to x
    set x to (x * 60) * 60
    if x > 0 then
    	set myDate to current date
    	set myNewDate to myDate + (y * hours)
    	set myTime to time string of myNewDate
    	set myParts to words of myTime
    	set myDisplay to (item 1 of myParts) & ":" & (item 2 of myParts)
    	display dialog "TextEdit will start at " & myDisplay with title "Starting at..." buttons {"Cancel", "Confirm"} default button 2
    	display notification "Starting TextEdit at " & myDisplay
    	set input to x
    	delay input
    	tell application "TextEdit" to launch
    	display dialog "Cannot be 0 or null." with title "Error..." with icon stop buttons {"Ok"} default button 1
    end if
    I've tried using a block tell/end tell and using activate rather than launch but I get the same thing happening. Also I've tried using a sub-routine with the same result. If I comment out the tell statement, the problem naturally doesn't occur.

    I only want the application to launch once the user has submitted the input and assumed the if statement would not execute until this was done. Can anyone advise if/where I am going wrong? I'm aware the code may be messy, and there are likely better ways to do things, but I've not got a lot of experience with Applescript.

    Thanks in advance for any help.
  2. kryten2 macrumors 6502a

    Mar 17, 2012
    You can try :

    launch application "TextEdit"
    do shell script "open -a /Applications/"
  3. iConcept thread starter macrumors newbie


    Sep 1, 2009
    Thanks Kryten2, you're a smeggin' genius. The first option seems to have worked, so didn't bother with the second.

    Just shows that simpler is sometimes better.
  4. iConcept thread starter macrumors newbie


    Sep 1, 2009
    Thought I had another issue. When saved as an application, the above code would execute immediately, ignoring the delay. Seems this is a bug with Yosemite which accepts the delay when run as a script, but ignores it when run as an application.

    As a workaround, I found the following from Chris Page on StackExchange:

    on delay duration
    	set endTime to (current date) + duration
    	repeat while (current date) is less than endTime
    		tell AppleScript to delay duration
    	end repeat
    end delay
    This has done the trick so I thought I'd share in case anyone else has a similar problem.
  5. Red Menace macrumors 6502

    Red Menace

    May 29, 2011
    Littleton, Colorado, USA
    The problem is a bug with the delay command itself (it immediately times out if you move the mouse, for example), so you are better off not using it at all. You also need to make sure not to lock the script into a tight repeat loop, since the application's user interface will get blocked.

    A workaround from the mailing list that keeps the UI responsive:
    on delay delayTime
    	set theRepeat to delayTime * 10 div 1
    	repeat theRepeat times
    		do shell script "sleep 0.1"
    	end repeat
    end delay

Share This Page